Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?
···
--
Posted via http://www.ruby-forum.com/.
Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?
--
Posted via http://www.ruby-forum.com/.
everything because they dont have common stuff:
private methods are not needed to be singleton
and singleton methods are not needed to be private
you can define private singleton methods
you should look at wikipedia first what is the difference between
"public, protected and private" methods and what are singleton methods
...
you mix two totally different things together
(its like to compare the color of object A with the size of object B, it
does not make sense)
--
Posted via http://www.ruby-forum.com/.
Your question doesn't even make any sense.
Stop that.
Am 02.03.2013 14:09, schrieb Kumar R.:
Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?
Private methods are methods defined in a class that only instances of that
class can see and use.
Singleton methods are methods added on to an individual instance that
other instances of the same class will not have. Only THAT instance will
have that method. (You can even define a singleton on a class, but the
*instances* of that class won't have the singleton method added. It
applies to that class (which if you know ruby you know a class is also an
object in its own right) object only.
On 3/2/13 8:09 AM, "Kumar R." <lists@ruby-forum.com> wrote:
Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?--
Posted via http://www.ruby-forum.com/\.
@Eric ~~ Thank you very much to you.
--
Posted via http://www.ruby-forum.com/.
unknown wrote in post #1099843:
Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?Your question doesn't even make any sense.
Stop that.
You are carrying wrong perceptions on me, that causes you to keep away
to understand the core of my question has.
Obviously it is not my problem.
But here I found one such valuable information from the BLACK's book,
after a long search :
Private and singleton are different
It’s important to note the difference between a private method and a
singleton method.
A singleton method is “private” in the loose, informal sense that it
belongs to
only one object, but it isn’t private in the technical sense. (You can
make a singleton
method private, but by default it isn’t.) A private, non-singleton
instance method, on
the other hand, may be shared by any number of objects but can only be
called under
the right circumstances. What determines whether you can call a private
method isn’t
the object you’re sending the message to, but which object is self at
the time you
send the message.
Enjoy!
D. Deryl Downey wrote in post #1099844:
Private methods are methods defined in a class that only instances of
that
class can see and use.
(which if you know ruby you know a class is also an object in its own
right)
Really I don't know Ruby at all. But trying to learn it and I will do
definitely.
You guys know it and that's why ir-respecting me continuously. Still I
will take that blow,as I DON'T know Ruby.
Lastly Thanks for sharing your knowledge, after giving too much curse to
me.
--
Posted via http://www.ruby-forum.com/\.
Private methods are methods defined in a class that only instances of that
class can see and use.
Not true in Ruby. In Ruby, a private method can only be called on
self. And an explicit receiver (even if it is `self`) is not allowed,
*unless* the method is a setter. E.g.:
1.9.3p194 :001 > class Foo
1.9.3p194 :002?> attr_accessor :bar
1.9.3p194 :003?> private :bar=
1.9.3p194 :004?>
1.9.3p194 :005 > def set_bar_private(obj)
1.9.3p194 :006?> self.bar = obj
1.9.3p194 :007?> end
1.9.3p194 :008?> private :set_bar_private
1.9.3p194 :009?>
1.9.3p194 :010 > def set_bar_public_using_accessor(obj)
1.9.3p194 :011?> self.bar = obj
1.9.3p194 :012?> end
1.9.3p194 :013?>
1.9.3p194 :014 > def set_bar_public_using_private_without_receiver(obj)
1.9.3p194 :015?> set_bar_private(obj)
1.9.3p194 :016?> end
1.9.3p194 :017?>
1.9.3p194 :018 > def set_bar_public_using_private_with_self_as_receiver(obj)
1.9.3p194 :019?> self.set_bar_private(obj)
1.9.3p194 :020?> end
1.9.3p194 :021?> end
=> nil
1.9.3p194 :022 > f = Foo.new
=> #<Foo:0x94f4d0>
1.9.3p194 :023 > f.bar = 42
NoMethodError: private method `bar=' called for #<Foo:0x94f4d0>
from (irb):23
from /Users/eric/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3p194 :024 > f.set_bar_private 42
NoMethodError: private method `set_bar_private' called for #<Foo:0x94f4d0>
from (irb):24
from /Users/eric/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3p194 :025 > f.set_bar_public_using_accessor 42
=> 42
1.9.3p194 :026 > f.bar
=> 42
1.9.3p194 :027 > f.set_bar_public_using_private_without_receiver 43
=> 43
1.9.3p194 :028 > f.bar
=> 43
1.9.3p194 :029 > f.set_bar_public_using_private_with_self_as_receiver 44
NoMethodError: private method `set_bar_private' called for
#<Foo:0x94f4d0 @bar=43>
from (irb):19:in `set_bar_public_using_private_with_self_as_receiver'
from (irb):29
from /Users/eric/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3p194 :030 >
On Sat, Mar 2, 2013 at 10:09 AM, D. Deryl Downey <me@daviddwdowney.com> wrote:
Singleton methods are methods added on to an individual instance that
other instances of the same class will not have. Only THAT instance will
have that method. (You can even define a singleton on a class, but the
*instances* of that class won't have the singleton method added. It
applies to that class (which if you know ruby you know a class is also an
object in its own right) object only.On 3/2/13 8:09 AM, "Kumar R." <lists@ruby-forum.com> wrote:
Can anyone help me to understand the difference between `private
methods` and `singleton methods` in Ruby?--
Posted via http://www.ruby-forum.com/\.