Accessing the default variable inside the Struct creation block doesn't seem to work in this case:
default = 'test'
A = Struct.new(:a, :b) do
def initialize(*args)
super(*args)
self.b ||= default
end
end
It is throwing the following error:
'initialize': undefined local variable or method `default' for #<struct A a=2, b=nil> (NameError)
Can someone explain why this is happening and whether there is a work around?
(Tested on Ruby 1.9.3 and 2.1.2)
defaultvariable in another scope.