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

  • Thread starter Eustaquio Rangel de Oliveira Jr.
  • Start date
E

Eustaquio Rangel de Oliveira Jr.

-----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
(e-mail address removed)
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-----
 
R

Robert Klemme

Eustaquio Rangel de Oliveira Jr. said:
-----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
-

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
 
E

Eustaquio Rangel de Oliveira Jr.

-----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
(e-mail address removed)
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-----
 
J

Jacob Fugal

=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
 
R

Robert Klemme

Jacob said:
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
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top