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 ?
daz
Chorus: <i>Quit clownin', goof</i>