R
Ryan Lewis
What's the difference between a regular method and a singleton method?
What's the difference between a regular method and a singleton method?
NoMethodError: undefined method `second' for [1, 2, 3]:Arraya = [1,2,3] => [1, 2, 3]
a.second
NoMethodError: undefined method `second' for [4, 5, 6]:Arrayclass <<a
def second
self[1]
end
end => nil
a.second => 2
[4, 5, 6].second
Gary said:What's the difference between a regular method and a singleton method?
From the perspective of a particular object, singleton methods are
those methods that are defined within the singleton class of
the object.
Regular methods are methods that are defined within the normal
inheritance tree of the object's class.
Method lookup is done by first checking the singleton class of an
object and if not found there by checking the normal inheritance tree.
By default objects don't have a singleton class. The singleton class
is created when it is referenced:
$ irbNoMethodError: undefined method `second' for [1, 2, 3]:Arraya = [1,2,3] => [1, 2, 3]
a.second
from (irb):2
from :0NoMethodError: undefined method `second' for [4, 5, 6]:Arrayclass <<a
def second
self[1]
end
end => nil
a.second => 2
[4, 5, 6].second
from (irb):9
from :0
In this example, I opened up the singleton class associated
with 'a' and defined a new method, second, that returns the
second element of the array. As you can see, this method
is only available to that one particular array and not to
all array's in general.
Gary Wright
Just out of curiosity, is there a difference between
def a.second
...
end
and
class << a
?
Gary said:What's the difference between a regular method and a singleton method?
From the perspective of a particular object, singleton methods are
those methods that are defined within the singleton class of
the object.
Regular methods are methods that are defined within the normal
inheritance tree of the object's class.
Method lookup is done by first checking the singleton class of an
object and if not found there by checking the normal inheritance tree.
By default objects don't have a singleton class. The singleton class
is created when it is referenced:
$ irbNoMethodError: undefined method `second' for [1, 2, 3]:Arraya = [1,2,3] => [1, 2, 3]
a.second
from (irb):2
from :0NoMethodError: undefined method `second' for [4, 5, 6]:Arrayclass <<a
def second
self[1]
end
end => nil
a.second => 2
[4, 5, 6].second
from (irb):9
from :0
In this example, I opened up the singleton class associated
with 'a' and defined a new method, second, that returns the
second element of the array. As you can see, this method
is only available to that one particular array and not to
all array's in general.
Gary Wright
Now, this is talking about class methods, not singleton methods on
arbitrary
objects, but I think the idea still applies because class methods are
methods on a singleton, in this case the object Parser (which
happens to be
a Class).
So defining the 'second' method like so:
def Array.second
self[1]
end
is the same is making the singleton class like below.
Class << Array
def second
self[1]
end
end
Gary said:So defining the 'second' method like so:
def Array.second
self[1]
end
is the same is making the singleton class like below.
Class << Array
def second
self[1]
end
end
No.
Your examples are different than mine.
# case 5
def Array.foo
end
[...]
# still case 5
class <<Array
def foo; end
end
So defining the 'second' method like so:
def Array.second
self[1]
end
is the same is making the singleton class like below.
Class << Array
def second
self[1]
end
end
No.
<snip tons of stuff>
Gary said:So defining the 'second' method like so:
def Array.second
self[1]
end
is the same is making the singleton class like below.
Class << Array
def second
self[1]
end
end
No.
Yes, actually.
Your examples are different than mine.
They are. They also don't make much sense. But they're still
equivalent
to each other.
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.