suresh suresh - 3 years ago 128
Ruby Question

Implementation of attr_accessor

Excuse me for the noob question.Please explain me outputs of the below ruby programme for implementing attr_accessor.

class SimpleService

attr_accessor :name

def initialize(name)
@name = name
end

def process
if false # some condition met
name = 'Akshay'
end
name
end
end


When I execute this class

SimpleService.new('John Doe').process
=> nil


Why is the result nil?

when I use self explicitly to name

def process
if false # some condition met
self.name = 'Akshay'
end
name
end


Now the output is

SimpleService.new('John Doe').process
=> "John Doe"


why is the result now "John Doe"?

I am a beginner in ruby.

Thanks in advance!

Answer Source

The thing is when you call name = you implicitly declare new local variable. Try this:

def process
  name = 'Akshay'
  puts local_variables.inspect
end

Why is it that way is a complicated question, discussed many times there and here. The setter always requires in explicit receiver. Period.

Once you have the line name = 'Akshay' inside a method, you introduce a new local variable and this method’s scope gets extended with new local variable name, despite how is was declared. It’s basically done by ruby parser.

And local variables take precedence over instance methods. That is why what is returned in the last line is a local variable. That was apparently not set, due to falsey condition above. Hence nil.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download