On Dec 17, 8:15 pm, MonkeeSage [email protected] wrote:
difference between private/protected if private methods could be
called with explicit the self receiver (see the example output in
Jamis’ article, also the link in the fifth comment). Maybe I’m missing
something though.
There would still be this difference:
class Foo
def call_prot( someone_else )
someone_else.prot
end
def call_priv( someone_else )
someone_else.priv
end
protected
def prot; “prot”; end
private
def priv; “priv”; end
end
f1 = Foo.new
f2 = Foo.new
p f1.call_prot( f2 )
#=> “prot”
p f1.call_priv( f2 )
#=> NoMethodError: private method ‘priv’ called for #Foo:0x281e50
Other instances of the same class can still call protected methods on
you, while only you yourself can call private methods. Paul’s proposal/
question would be if this works already (which it does):
class Foo
def call_bar_set( val )
self.bar = val
end
private
def bar=( val ); “yay”; end
end
Foo.new.call_bar_set( 42 )
then why not allow this to work:
class Foo
def call_bar
self.bar
end
private
def bar; “yay”; end
end
Foo.new.call_bar
I suppose it might be a small extra burden on the runtime to allow
this:
class Foo
def call_bar( someone_else )
someone_else.bar
end
private
def bar; end
end
…as the runtime would have to check if ‘someone_else’ was the same
as ‘self’ inside call_bar. Since it already has to check if the ‘self’
inside call_bar is of the same class as the class as ‘someone_else’,
this doesn’t seem particularly burdensome, however.