Object private methods

P

Paolino

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
 
R

Robert Klemme

Paolino said:
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
 
D

daz

Paolino said:
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 ? ;)


daz

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

Robert Klemme

daz said:
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
 
H

Han Holl

------=_Part_1355_26015289.1127898653938
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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
=3D> nil
irb(main):003:0>

So the receiver _must_ be an implicit self.

Cheers,

Han Holl

------=_Part_1355_26015289.1127898653938--
 
P

Paolino

Robert said:
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
 
T

ts

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

yes,

P> 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
 
P

Paolino

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

yes,

P> 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
 
T

ts

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

Too complex what you do :)

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
A#a
moulon%


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top