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)
It's because def keyword starts new local variables scope, so default local variable isn't visible inside of it. The workaround is to use define_method, because the block you pass into it is closure:
default = 'test'
A = Struct.new(:a, :b) do
define_method(:initialize) do |*args|
super(*args)
self.b ||= default
end
end
a = A.new
a.b
# => "test"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With