Add methods to a predefined class: on the class instance or metaclass?

Discussion in 'Ruby' started by Eustaquio Rangel de Oliveira Jr., Jul 4, 2005.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi.

    When I do
    - -----------------------------------------------------------------------=
    ----
    class Foo
    ~ def bar
    ~ puts "bar!"
    ~ end
    end
    - -----------------------------------------------------------------------=
    ----
    and then
    - -----------------------------------------------------------------------=
    ----
    class Foo
    ~ def another_bar
    ~ puts "another bar!"
    ~ end
    end
    - -----------------------------------------------------------------------=
    ----
    am I adding another_bar method to the Foo class instance or on it's metac=
    lass?

    I was reading

    http://www.rubygarden.org/ruby?GavinSinclair/MetaClassDiscussion
    "Ruby classes are themselves objects, being instances of the metaclass
    ~ Class. . . . The class Object is at the root of the hierarchy. . . .
    ~ Object itself is the only object without a superclass."

    and then

    http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/40537
    where Matz says that
    ">I've been trying to understand metaclasses
    You don't have to, because there's no such a thing in Ruby."

    and

    http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/40548
    where Matz call them as meta-objects.

    So, seems that there is some meta(class|object) around there (specially i=
    f
    you think about the graph here
    http://www.rubygarden.org/ruby?ClassInstanceVariables, where explain the
    class methods), but I'm curious of when adding a new method as described
    above, where it is added, on the metaclass or on the class instance, to
    reflect the new method on all current and future instances of Foo.

    Thanks!

    - ----------------------------
    Eust=E1quio "TaQ" Rangel

    http://beam.to/taq
    Usu=E1rio GNU/Linux no. 224050
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFCyV2Wb6UiZnhJiLsRAoQxAJ43dBExQqvejAkYhPDhKNxLWeIBkwCfaEiB
    IW2O52Q3+r7EMd7hg1Mbrc4=3D
    =3DtSqS
    -----END PGP SIGNATURE-----
     
    Eustaquio Rangel de Oliveira Jr., Jul 4, 2005
    #1
    1. Advertising

  2. Eustaquio Rangel de Oliveira Jr. <> wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Hi.
    >
    > When I do
    > -
    > ---------------------------------------------------------------------------
    > class Foo ~ def bar
    > ~ puts "bar!"
    > ~ end
    > end
    > -
    > ---------------------------------------------------------------------------
    > and then -
    > ---------------------------------------------------------------------------
    > class Foo ~ def another_bar
    > ~ puts "another bar!"
    > ~ end
    > end
    > -
    > ---------------------------------------------------------------------------
    > am I adding another_bar method to the Foo class instance or on it's
    > metaclass?


    You're adding them to class Foo so instances of Foo can use them. If you
    want them available as methods of Foo you have to do any of these:

    class Foo
    def Foo.bar1() "bar1" end

    def self.bar2() "bar2" end

    class <<self
    def bar3() "bar3" end
    end

    class <<Foo
    def bar4() "bar4" end
    end
    end

    Kind regards

    robert
     
    Robert Klemme, Jul 4, 2005
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi.

    | You're adding them to class Foo so instances of Foo can use them. If
    | you want them available as methods of Foo you have to do any of these:
    | class Foo
    | def Foo.bar1() "bar1" end
    | def self.bar2() "bar2" end
    | Kind regards
    | robert

    Thanks again, Robert. :)

    - ----------------------------
    Eust=E1quio "TaQ" Rangel

    http://beam.to/taq
    Usu=E1rio GNU/Linux no. 224050
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFCyXfnb6UiZnhJiLsRAktGAJ0RcoTOPrIWVLw6KGZzIfJi/hYbugCdEYiQ
    eQHMMFg7OTMBVCd95AsS534=3D
    =3DrzHI
    -----END PGP SIGNATURE-----
     
    Eustaquio Rangel de Oliveira Jr., Jul 4, 2005
    #3
  4. Eustaquio Rangel de Oliveira Jr.

    Jacob Fugal Guest

    On 7/4/05, Robert Klemme <> wrote:
    >=20
    > class Foo
    > def Foo.bar1() "bar1" end
    >=20
    > def self.bar2() "bar2" end
    >=20
    > class <<self
    > def bar3() "bar3" end
    > end
    >=20
    > class <<Foo
    > def bar4() "bar4" end
    > end
    > end


    Note that the last form (class << Foo) doesn't need to be inside the
    reopened class:

    $ cat > a.rb
    class Foo; end

    class << Foo
    def test
    puts "Hello, world!"
    end
    end

    Foo.test

    $ ruby a.rb
    Hello, world!

    Jacob Fugal
     
    Jacob Fugal, Jul 5, 2005
    #4
  5. Jacob Fugal wrote:
    > On 7/4/05, Robert Klemme <> wrote:
    >>
    >> class Foo
    >> def Foo.bar1() "bar1" end
    >>
    >> def self.bar2() "bar2" end
    >>
    >> class <<self
    >> def bar3() "bar3" end
    >> end
    >>
    >> class <<Foo
    >> def bar4() "bar4" end
    >> end
    >> end

    >
    > Note that the last form (class << Foo) doesn't need to be inside the
    > reopened class:
    >
    > $ cat > a.rb
    > class Foo; end
    >
    > class << Foo
    > def test
    > puts "Hello, world!"
    > end
    > end
    >
    > Foo.test
    >
    > $ ruby a.rb
    > Hello, world!


    The same is true for variant 1 also. I prefer the nested versions with
    "self" because they are immune to name changes of the class.

    Kind regards

    robert
     
    Robert Klemme, Jul 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irmen de Jong
    Replies:
    5
    Views:
    385
    Irmen de Jong
    Sep 28, 2004
  2. ironfroggy
    Replies:
    16
    Views:
    440
    Michele Simionato
    Jun 3, 2005
  3. Ksenia Marasanova
    Replies:
    2
    Views:
    329
    Ksenia Marasanova
    Sep 9, 2005
  4. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    307
Loading...

Share This Page