Newbie: how to dynamically add methods?

J

Jeff Cohen

The PickAxe doesn't seem to cover details of how to dynamically create
class or add methods to existing classes. For example, sometimes I see
people doing things like this:

class << self
...

and I'm wondering exactly what's going on.

Anybody know of a good book or online tutorial for this kind of thing?

Thanks!
Jeff
 
W

Wilson Bilkovich

The PickAxe doesn't seem to cover details of how to dynamically create
class or add methods to existing classes. For example, sometimes I see
people doing things like this:

class << self
...

and I'm wondering exactly what's going on.

Anybody know of a good book or online tutorial for this kind of thing?

This may help:
http://www.whytheluckystiff.net/articles/seeingMetaclassesClearly.html

class << self is usually used for things that are a bit beyond a basic
method definition.
For example, if you just want to define a new class method called
blah, you could write:
def self.blah
....
end

However, if you want to do alias_method on a class method, you can't do:
alias_method :self.blah, etc, etc.
You need to put it inside a class << self section.
 
D

dblack

Hi --

The PickAxe doesn't seem to cover details of how to dynamically create
class or add methods to existing classes. For example, sometimes I see
people doing things like this:

class << self
...

and I'm wondering exactly what's going on.

The "<< obj" notation takes you into the singleton class of obj, which
is where that particular object's singleton methods (i.e., those that
only that object can call) are stored.

Except for some minor differences that usually don't matter, these two
things are equivalent:

def a.meth
end

and

class << a
def meth
end
end

In both cases, you're defining a method that only the object a can
call.
Anybody know of a good book or online tutorial for this kind of thing?

http://www.rubygarden.org/ruby?SingletonTutorial goes over singleton
classes and methods, and also the other meaning of "singleton" in
Ruby (not closely related, except by coincidence of name, but good to
know about *because* of the coincidence of name).


David

--
David A. Black ([email protected])
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
J

Jeff Cohen

And how does class_eval and instance_eval play a role here?

I think I'm almost getting it but feel like I need one or two more dots
to connect.
 
P

pesachzon

As far as I remember,
class_eval evaluates a block of code within the context of a class. So,
if you have a code which adds methods, they will be added to the class
and will be available to all instances of this class.

instance_eval is evaluated in the context of an object which is an
instance of some class. If you add methods they will be available only
to this object.

Cheers

Eugene Vahlis
Department of Computer Science
University of Toronto
 

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

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top