P
Patrick Gundlach
Hello Rubyists,
I am slightly confused with calling a private method:
--------------------------------------------------
class Foo
def without_self
x=5
end
def with_self
self.x=5
end
def call_priv_with_self
self.priv
end
private
def x=(value)
puts "x=#{value}"
end
def priv
puts "priv"
end
end
Foo.new.without_self # no output
Foo.new.with_self # output "x=5"
Foo.new.call_priv_with_self # bang
--------------------------------------------------
I guess the first call is just an assignment to the variable x. But
from my understanding of pickaxe2, 329, Variable/Method Ambiguity this
should not be the case, because Ruby did not encounter an assignment
before.
The second call works, although it is a private method and I learned
that private methods must not have a receiver, as 'proved' in line
three.
So my two questions are: why is there no method call in
Foo#without_self? And why does self.x= report an error?
Patrick
I am slightly confused with calling a private method:
--------------------------------------------------
class Foo
def without_self
x=5
end
def with_self
self.x=5
end
def call_priv_with_self
self.priv
end
private
def x=(value)
puts "x=#{value}"
end
def priv
puts "priv"
end
end
Foo.new.without_self # no output
Foo.new.with_self # output "x=5"
Foo.new.call_priv_with_self # bang
--------------------------------------------------
I guess the first call is just an assignment to the variable x. But
from my understanding of pickaxe2, 329, Variable/Method Ambiguity this
should not be the case, because Ruby did not encounter an assignment
before.
The second call works, although it is a private method and I learned
that private methods must not have a receiver, as 'proved' in line
three.
So my two questions are: why is there no method call in
Foo#without_self? And why does self.x= report an error?
Patrick