calling private accessors

D

Davy Brion

[Note: parts of this message were removed to make it a legal post.]

Hi,

i bumbed into something weird today, and would like to know what's going
on... here's a contrived example:

class Test
attr_reader :name

def set_the_name(value)
name = value
end

private

def name=(value)
@name = value
end
end

test = Test.new
test.set_the_name "some name"
p test

the output of this on ruby 1.8.7 (as wel as on 1.9.2) is:

#<Test:0x7fef6dc8>

the name accessor wasn't called, but there was no error either

if i change the set_the_name method to this:

def set_the_name(value)
self.name = value
end

then the output changes to this:

#<Test:0x7fef6da0 @name="some name">

there are two things i don't quite understand about this:
1) i thought private methods should never be called through self, and that
they are implicitely called on self. calling a non-accessor private method
on self does raise the expected error. why does the accessor need to be
called using self?
2) when calling the private accessor without using self, its implementation
clearly isn't executed but no exception is raised either... why is that?
 
B

Brian Candler

Davy said:
def set_the_name(value)
name = value
end

To Ruby, that's an assignment to a local variable, which then drops out
of scope. That's why you have to do "self.name= value" to call the
method.

If it really were private, you'd need

send:)name=, value)

although being private rather negates the purpose of an 'accessor' which
is to make values accessible :)
when calling the private accessor without using self, its
implementation
clearly isn't executed but no exception is raised either... why is that?

Because it's perfectly legal to assign a value to a local variable :)
 
D

Davy Brion

[Note: parts of this message were removed to make it a legal post.]

To Ruby, that's an assignment to a local variable, which then drops out
of scope. That's why you have to do "self.name= value" to call the
method.

d'oh! of course :)

If it really were private, you'd need

send:)name=, value)

although being private rather negates the purpose of an 'accessor' which
is to make values accessible :)
in this case, i only want the accessor to be acessible to the class and its
subclasses, mainly because the subclasses need to be able to override the
'storage mechanism' (ie: backing field vs hash or something)... it's a bid
of an edge-case though :)

Because it's perfectly legal to assign a value to a local variable :)

obviously :p
 
A

ara.t.howard

in this case, i only want the accessor to be acessible to the class and its
subclasses, mainly because the subclasses need to be able to override the
'storage mechanism' (ie: backing field vs hash or something)... it's a bid
of an edge-case though :)

that is called 'protected' not 'private' - just that change should do
it.

-a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top