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

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?

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 if
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áquio "TaQ" Rangel
eustaquiorangel@yahoo.com
http://beam.to/taq
Usuário GNU/Linux no. 224050

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

···

Eustaquio Rangel de Oliveira Jr. <eustaquiorangel@yahoo.com> 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?

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. :slight_smile:

- ----------------------------
Eustáquio "TaQ" Rangel
eustaquiorangel@yahoo.com

Usuário GNU/Linux no. 224050

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

···

On 7/4/05, Robert Klemme <bob.news@gmx.net> 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

Jacob Fugal wrote:

···

On 7/4/05, Robert Klemme <bob.news@gmx.net> 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