Hi!
There''s something I don''t understand yet about static/private
methods. I
hope someone can explain...
E.g.:
class SayHello
def hello(who)
puts "Hello, " + who + "!"
end
def self.say(words)
puts words
end
end
hello = SayHello.new
hello.hello "World" : Hello World!
hello.say "hi" : Error
SayHello.say "hi" : hi
OK, this is clear to me, but now...
class SayHello
private
def hello(who)
puts "Hello, " + who + "!"
end
def self.say(words)
puts words
end
end
Both methods are private now.
hello = SayHello.new
hello.hello "World" : Error because the method is private
hello.say "hi" : Error because the method is static
SayHello.say "hi" : hi
The last one I don''t understand. How come it returns "hi"
eventhough
it''s a private method? Can someone shed some light?
Thanks,
Mischa.
Mischa Berger wrote:> class SayHello > private > def hello(who) > puts "Hello, " + who + "!" > end > def self.say(words) > puts words > end > end > > Both methods are private now.Nope private, protected and public modifiers have only effect on instance methods (that have an implicit "self" as receiver). If you want to define a private class method you need to make available that implicit "self": class SayHello private def hello(who) puts "Hello, " + who + "!" end class << self private def say(words) puts words end end end Now both methods are private. You can only call them on self, this means that SayHello.say is only callable by another class method. zsombor -- Company - http://primalgrasp.com Thoughts - http://deezsombor.blogspot.com
On Tue, 2006-01-17 at 11:29 +0200, Dee Zsombor wrote:> class SayHello > private > def hello(who) > puts "Hello, " + who + "!" > end > class << self > private > def say(words) > puts words > end > end > end >You can''t do this though: class SayHello self.private def self.say(words) puts words end end as "private" is itself a private method :) NoMethodError: private method `private'' called for SayHello:Class
Dee Zsombor wrote:> Mischa Berger wrote: > >>class SayHello >> private >> def hello(who) >> puts "Hello, " + who + "!" >> end >> def self.say(words) >> puts words >> end >>end >> >>Both methods are private now. > > > Nope private, protected and public modifiers have only effect on instance > methods (that have an implicit "self" as receiver). If you want to define > a private class method you need to make available that implicit "self":Well, actually I was asking cause I saw a code sample in the "Agile Web Development with Rails" book (pp. 510: private def self.hash_password) that didn''t make sense to me. I don''t mind my methods not being private, so I guess I''ll just leave out "private". Thanks for clearing things up. Mischa.
>>> class SayHello >>> private >>> def hello(who) >>> puts "Hello, " + who + "!" >>> end >>> def self.say(words) >>> puts words >>> end >>> endAn even simpler way is to use ''private_class_method'' class SayHello def self.say(words) puts words end private_class_method :say end Zsombor -- Company - http://primalgrasp.com Thoughts - http://deezsombor.blogspot.com