Object private methods

Hello

  I noticed that Object private methods are available everywhere.
This I noticed exploring 'main' instance defining a method

Can I have some explanation on this form of globalizing ?

TIA

Paolino

···

___________________________________ Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB http://mail.yahoo.it

Paolino wrote:

Hello

  I noticed that Object private methods are available everywhere.
This I noticed exploring 'main' instance defining a method

Can I have some explanation on this form of globalizing ?

Dunno what exactly you are looking for...

Typically these methods reside in Kernel and are defined like this:

module Kernel
private
  def foo() "something" end
end

This works since all objects eventually inherit Kernel:

18:45:30 [~]: ruby -e 'p String.ancestors'
[String, Enumerable, Comparable, Object, Kernel]

What else do you want to know?

Kind regards

    robert

Paolino wrote:

Hello

  I noticed that Object private methods are available everywhere.
This I noticed exploring 'main' instance defining a method

Can I have some explanation on this form of globalizing ?

It's useful to have some methods available almost anywhere ...

   $_ = 'A B C'
   puts split # there are two of the private methods
   #-> A
   #-> B
   #-> C

.... but not *everywhere* ...

   puts 3.split

   #-> ... private method `split' called for 3:Fixnum (NoMethodError)

There is no 'split' method for Fixnum.
Fixnum inherits from Object and a 'split' was found but, fortunately,
a sensible error message was produced before disaster struck.

We can define a (silly) split for Fixnum:

   class Fixnum
     def split
       self / 2.0
     end
   end

   puts 3.split
   #-> 1.5

.... and we can make it private:

   class Fixnum
     private :split
   end

   puts 3.split
   #-> ... private method `split' called for 3:Fixnum (NoMethodError)

.... but from within Fixnum, it's not private:

   class Fixnum
     def splitter
       split # <-----XXXXX
     end
   end

   puts 5.splitter
   #-> 2.5

So when we use 'puts' or 'split', or any other of the Kernel methods,
without a dot in front of them ("no receiver"), our homeland is a
place provided for us where all of those methods are non-private.
(A bit like standing to the left of the arrow <-----XXXXX
  ... right inside an object) <ROCK>

How warm and fluffy do you feel now ? :wink:

daz

Chorus: <i>Quit clownin', goof</i>

Also interesting:
irb(main):001:0> self.puts 'aa'
NoMethodError: private method `puts' called for main:Object
from (irb):1
irb(main):002:0> puts 'aa'
aa
=> nil
irb(main):003:0>

So the receiver _must_ be an implicit self.

Cheers,

Han Holl

daz wrote:

Paolino wrote:

Hello

  I noticed that Object private methods are available everywhere.
This I noticed exploring 'main' instance defining a method

Can I have some explanation on this form of globalizing ?

It's useful to have some methods available almost anywhere ...

   $_ = 'A B C'
   puts split # there are two of the private methods
   #-> A
   #-> B
   #-> C

... but not *everywhere* ...

   puts 3.split

   #-> ... private method `split' called for 3:Fixnum (NoMethodError)

There is no 'split' method for Fixnum.

Not exactly: Fixnum inherits split from Kernel and it's just not
accessible with a receiver because it's private:

09:24:18 [Oracle]: ruby -e '$_="a,b,c"; p 1.instance_eval { split(/,/) };
p 1.send(:split, /,/)'
["a", "b", "c"]
["a", "b", "c"]

Fixnum inherits from Object and a 'split' was found but, fortunately,
a sensible error message was produced before disaster struck.

Well you could argue about the sensibility (?) of the error message.
Privacy is just a means to make it *look* like a global function.

Kind regards

    robert

Robert Klemme wrote:

Paolino wrote:

Hello

I noticed that Object private methods are available everywhere.
This I noticed exploring 'main' instance defining a method

Can I have some explanation on this form of globalizing ?

Dunno what exactly you are looking for...

Typically these methods reside in Kernel and are defined like this:

module Kernel
private
  def foo() "something" end
end

This works since all objects eventually inherit Kernel:

What else do you want to know?

My question was about something different probably.

But why/how private messages are not accepting a receiver ?
Is this their actual definition?

I suppose 'main' does some tricks when I define a method "there".
It pushes it in Object private space.

Am I defining it in its singleton?

Where exactly I find myself in the "global" ?I see self.is_a? Class is false,so I suppose I'm not in the sigleton space.

Am I inside a method definition (o_O) defining a nested method?

Thanks for answers

Paolino

···

___________________________________ Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB http://mail.yahoo.it

But why/how private messages are not accepting a receiver ?
Is this their actual definition?

yes,

I suppose 'main' does some tricks when I define a method "there".

no, not really. ruby has self and *internally* ruby_class which give it
where the method must be defined.

For example :

  * at top level it has : self = main, ruby_class = Object

    when you define a method at top level this will be an Object method

  * in the class A, it has : self = A, ruby_class = A

    when you define a method in A, this will be an instance method for A

Guy Decoux

ts wrote:

"P" == Paolino <paolo_veronelli@tiscali.it> writes:

> But why/how private messages are not accepting a receiver ?
> Is this their actual definition?

yes,

> I suppose 'main' does some tricks when I define a method "there".

no, not really. ruby has self and *internally* ruby_class which give it
where the method must be defined.

For example :

  * at top level it has : self = main, ruby_class = Object

    when you define a method at top level this will be an Object method

  * in the class A, it has : self = A, ruby_class = A

    when you define a method in A, this will be an instance method for A

This is the first bad hack I meet in the Ruby machine.
Wasn't enough to operate inside a singleton and define a method_added hook to paste the added method in Object private space?

Anyway thanks a lot for explanations .

Paolino

···

___________________________________ Yahoo! Messenger: chiamate gratuite in tutto il mondo http://it.messenger.yahoo.com

Wasn't enough to operate inside a singleton and define a method_added
hook to paste the added method in Object private space?

Too complex what you do :slight_smile:

and don't forget that main, for ruby, is an object also it need the
distinction between self and ruby_class at least for this case

moulon% cat b.rb
#!/usr/bin/ruby
class A
end

A.class_eval do
   p self
   def a
      puts "A#a"
   end
end

A.instance_eval do
   p self
   def a
      puts "A::a"
   end
end

A.a
A.new.a

moulon%

moulon% ./b.rb
A
A
A#a
moulon%

Guy Decoux

···

A::a