D
dusty
I'm trying to understand the difference between implicitly and
explicitly calling a private method and am hoping someone could shed
some light on this for me.
class Tester
def public_hello_good
say_hello
end
def public_hello_bad
self.say_hello
end
private
def say_hello
"hello"
end
end
irb(main):001:0> require 'tester'
=> true
irb(main):002:0> a = Tester.new
=> #<Tester:0x8c294>
irb(main):003:0> a.public_hello_good
=> "hello"
irb(main):004:0> a.public_hello_bad
NoMethodError: private method `say_hello' called for #<Tester:0x8c294>
from ./tester.rb:6:in `public_hello_bad'
from (irb):4
from :0
What I'm wondering is why does this not work when I call
self.say_hello, but does work when I call say_hello? Since self is
the receiver in both cases, it seems that they would both work.
Anyone have any insight that they'd be willing to share on why it
works like that?
Thanks
explicitly calling a private method and am hoping someone could shed
some light on this for me.
class Tester
def public_hello_good
say_hello
end
def public_hello_bad
self.say_hello
end
private
def say_hello
"hello"
end
end
irb(main):001:0> require 'tester'
=> true
irb(main):002:0> a = Tester.new
=> #<Tester:0x8c294>
irb(main):003:0> a.public_hello_good
=> "hello"
irb(main):004:0> a.public_hello_bad
NoMethodError: private method `say_hello' called for #<Tester:0x8c294>
from ./tester.rb:6:in `public_hello_bad'
from (irb):4
from :0
What I'm wondering is why does this not work when I call
self.say_hello, but does work when I call say_hello? Since self is
the receiver in both cases, it seems that they would both work.
Anyone have any insight that they'd be willing to share on why it
works like that?
Thanks