Can anyone explain to me what’s going on here?
#!/usr/bin/env ruby -w
class Thingy
attr_accessor :name
def test
puts “self.name: #{self.name}”
puts “name: #{name}”
name ||= 'default value'
puts "self.name: #{self.name}"
puts "name: #{name}"
end
end
thing1 = Thingy.new
thing1.name = ‘custom value’
thing1.test
$ ruby dditest.rb
self.name: custom value
name: custom value
self.name: custom value
name: default value
I thought calling a method without a receiver (i.e. name, name=) within
an instance method would always call that method on the current object.
Is this not true for setters (i.e. Thingy#name=)?
Additionally, why is the first instance of name interpreted as a method
call and the last instance interpreted as (I think) a local variable?
Thanks,
Chris