Forum: Ruby on Rails Question about self and private

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mischa B. (Guest)
on 2006-01-17 02:24
(Received via mailing list)
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.
Dee Z. (Guest)
on 2006-01-17 12:06
(Received via mailing list)
Mischa B. wrote:
> 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
Tore Torell (Guest)
on 2006-01-17 14:00
(Received via mailing list)
On Tue, 2006-01-17 at 11:29 +0200, Dee Z. wrote:
>   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
Mischa B. (Guest)
on 2006-01-17 23:11
(Received via mailing list)
Dee Z. wrote:
>>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.
Dee Z. (Guest)
on 2006-01-20 12:26
(Received via mailing list)
>>> class SayHello
>>>  private
>>>  def hello(who)
>>>    puts "Hello, " + who + "!"
>>>  end
>>>  def self.say(words)
>>>    puts words
>>>  end
>>> end

An even simpler way is to use 'private_class_method'

class SayHello
  def self.say(words)
    puts words
  end
  private_class_method :say
end

Zsombor
This topic is locked and can not be replied to.