[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie: how to dynamically add methods?

Stefan Lang

2/16/2006 3:14:00 PM

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


--
Posted with http://De.... Sign up and save your time!


5 Answers

Wilson Bilkovich

2/16/2006 3:49:00 PM

0

On 2/16/06, Jeff Cohen <devlists-ruby-talk@devlists.com> wrote:
> 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/seeingMetaclassesCl...

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.


dblack

2/16/2006 4:56:00 PM

0

Mark Volkmann

2/16/2006 5:54:00 PM

0

On 2/16/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> On 2/16/06, Jeff Cohen <devlists-ruby-talk@devlists.com> wrote:
> > 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/seeingMetaclassesCl...

I wonder if the stiff server is down now. That URL isn't working, but
Google knows about it.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


Stefan Lang

2/16/2006 9:58:00 PM

0

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.



--
Posted with http://De.... Sign up and save your time!


pesachzon

2/17/2006 8:07:00 PM

0

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