Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Ruby - Advanced Metaprogramming
What will this code print?
class Framework
  def self.after_save(&block)
    @after_save = block
  end

  def self.save
    puts 'Saving data'
    @after_save.call if @after_save
  end
end

Framework.after_save { puts 'Data saved!' }
Framework.save
ASaving data\nData saved!
BData saved!\nSaving data
CSaving data
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the save method

    It prints 'Saving data' then calls the after_save block if set.
  2. Step 2: Check the assigned after_save block

    The block prints 'Data saved!'. So both lines print in order.
  3. Final Answer:

    Saving data\nData saved! -> Option A
  4. Quick Check:

    After hook runs after main action = C [OK]
Quick Trick: After hooks run after main method actions [OK]
Common Mistakes:
  • Mixing order of printed lines
  • Forgetting to call the block
  • Expecting no output without explicit call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes