Hello I’m trying to learn ruby blocks. But I have a trouble to get over this construction:
class SomeApp
attr_accessor :load_listener
def on_load(&block)
@load_listener = block
end
def load(x)
@load_listener.call(x) if @load_listener
end
end
app = SomeApp.new
app.on_load { |x| puts 'on load #{x}'}
app.load(5)
I don’t understand why result of this code is ‘on load #{x}’, instead of ‘on load 5’
Any help is appreciated.
You’re using single quotes (
') instead of double quotes ("). String interpolation (#{...}) does only work with double quotes.Change
puts 'on load #{x}'toputs "on load #{x}".