Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby struct creation block cannot access variable outside the block

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)

like image 235
eymlo Avatar asked Jul 24 '26 21:07

eymlo


1 Answers

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"
like image 87
Marek Lipka Avatar answered Jul 26 '26 12:07

Marek Lipka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!