help me understand this ruby idiom: class << self; alias_method :x :y end

K

Kelly Felkins

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

I'm trying to understand classes and objects. The Pick axe book points out=
=20
the "once" method in class Date and how it is defined using the object=20
singleton technique of "class << self". Actually Date uses this in several=
=20
places. Up until seeing Date I thought the reason for this idiom was to=20
add/replace methods in classes *when you don't have convenient access to th=
e=20
class definition* -- basically, use this when it's not your class you need=
=20
to enhance. But the fact that Date uses it several times indicates there is=
=20
another reason to use it.

Here are some examples from Date:
class << self; alias_method :leap?, :gregorian_leap? end
class << self; alias_method :new0, :new end

I just noticed that every use of class << self in Date contains an=20
alias_method.

Please enlighten me.

-Your humble pupil
Kelly

------=_Part_11290_2765134.1123597850288--
 
R

Robert Klemme

Kelly said:
I'm trying to understand classes and objects. The Pick axe book
points out the "once" method in class Date and how it is defined
using the object singleton technique of "class << self". Actually
Date uses this in several places. Up until seeing Date I thought the
reason for this idiom was to add/replace methods in classes *when you
don't have convenient access to the class definition* -- basically,
use this when it's not your class you need to enhance. But the fact
that Date uses it several times indicates there is another reason to
use it.

As you guessed the reasons are different: typically you use "class
<<something...end" if you have to define several methods or if you need a
class context for things to work (for example "alias").

These are equivalent:

class Foo
class <<self
def m1() "foo" end
end

class <<Foo
def m2() "foo" end
end

def self.m3() "foo" end
def Foo.m4() "foo" end
end

class <<Foo
def m5() "foo" end
end

def Foo.m6() "foo" end

x = Foo
def x.m7() "foo" end

class <<x
def m8() "foo" end
end

Now you have methods m1 to m8 as instance methods of Foo. You can do
Foo.m1(), Foo.m2 etc.

More technically, with class <<something ... end you make definition for
the so called "singleton class" the class instance responsible for the
single instance at hand (which happens to be a class object in this case;
but you can repeat the example above with any object instead of Foo). You
cannot create instances from this class:

irb(main):001:0> class Foo
irb(main):002:1> p new
irb(main):003:1> end
#<Foo:0x10192d58>
=> nil
irb(main):004:0> class <<Foo
irb(main):005:1> p new
irb(main):006:1> end
TypeError: can't create instance of virtual class
from (irb):5:in `new'
from (irb):5
Here are some examples from Date:
class << self; alias_method :leap?, :gregorian_leap? end
class << self; alias_method :new0, :new end

I just noticed that every use of class << self in Date contains an
alias_method.

Please enlighten me.

Hope, I could help.

Kind regards

robert
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top