Forum: Ruby Newbie: how to dynamically add methods?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Jeff Cohen (Guest)
on 2006-02-16 16:16
(Received via mailing list)
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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-02-16 16:49
(Received via mailing list)
On 2/16/06, Jeff Cohen <devlists-ruby-talk@devlists.com> wrote:
>
This may help:
http://www.whytheluckystiff.net/articles/seeingMet...

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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-02-16 17:56
(Received via mailing list)
Hi --

On Fri, 17 Feb 2006, Jeff Cohen 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.

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 (dblack@wobblini.net)
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
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2006-02-16 18:08
(Received via mailing list)
On 2/16/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
>      end
>    end
>
> In both cases, you're defining a method that only the object a can
> call.

It also might be worth pointing out that if 'a' is a class, you'll be
adding a class method.

so the notation

class << self
 ...

is often used within a class to defind a class method.  This type of
usage is described on page 34 in PickAxe 2 (the sidebar thing).

Cameron
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-02-16 18:56
(Received via mailing list)
On 2/16/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> > Anybody know of a good book or online tutorial for this kind of thing?
> >
>
> This may help:
> http://www.whytheluckystiff.net/articles/seeingMet...

I wonder if the stiff server is down now. That URL isn't working, but
Google knows about it.
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Jeff Cohen (Guest)
on 2006-02-16 22:58
(Received via mailing list)
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.
2192cf661c817335d093b5402eb8820b?d=identicon&s=25 pesachzon (Guest)
on 2006-02-17 21:08
(Received via mailing list)
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
This topic is locked and can not be replied to.