Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumRubydefine_methodSingle-choice MCQ

Why can a block passed to define_method read a local variable from the surrounding scope, while an equivalent def cannot, yet this fails inside a class ... end body?

x = "outer" Thing = Class.new Thing.define_method(:via_dm) { x } # works -> "outer" class Thing2 define_method(:via_dm) { x } # NameError: x not visible end