Add e method "inline" to an object?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIEkga25vdyB0aGF0IEkgY2FuIGV4dGVuZCBhIENsYXNzIGluc3RhbmNlIChhbiBvYmplY3Qp
IHdpdGggYQptZXRob2QocykgYnkgZG9pbmc6CgogIG9iamVjdC5leHRlbmQgTW9kdWxlCgpidXQg
Y2FuIEkgZG8gaXQgaW4gYSAiaW5saW5lIiB3YXkgd2l0aG91dCB0aGUgbmVlZCBvZiBhIG1vZHVs
ZT8KSSBqdXN0IHdhbnQgc29tZXRoaW5nIGFzIGZvbGxvd3M6CgogIHRpbWUgPSBUaW1lLm5vdwog
IG9iamVjdC5hZGRfbWV0aG9kICdkZWYgc2F5X3RpbWUoKTsgcmV0dXJuIHRpbWU7IGVuZCcKCiAg
aXJiPiBvYmplY3Quc2F5X3RpbWUKICAtLT4gdGltZQoKTm90ZSB0aGF0IEkgKipkb24ndCoqIHdh
bnQgdG8gaW5jbHVkZSB0aGUgImFkZF9tZXRob2QiIGluIG9iamVjdCBjbGFzcwpvciBpbiBPYmpl
Y3QgY2xhc3MuIFRoaXMgaXMsIEkgZG9uJ3Qgd2FudCB0byAiZGlydHkiIG9iamVjdCBjbGFzcy4K
CkFsc28gbm90ZSB0aGF0IEkgY2FuJ3QgdXNlICJvYmplY3QuZXh0ZW5kIE1vZHVsZSIgc2luY2Ug
dGhlIG1ldGhvZAoic2F5X3RpbWUiIG11c3QgcmV0dXJuIGEgdmFyaWFibGUgInRpbWUiIHZhbHVl
IHRoYXQgSSBwYXNzIGFzIGFuCmFyZ3VtZW50LgoKSXMgdGhlcmUgc29tZSB3YXk/IFRoYW5rcyBh
IGxvdC4KCgotLSAKScOxYWtpIEJheiBDYXN0aWxsbwo8aWJjQGFsaWF4Lm5ldD4K
 
F

Florian Gilcher

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

I'll explain it by example:

a =3D "a"
#=3D> "a"
class << a
def fooify
return "foo #{self}"
end
end

a.fooify
#=3D> "foo a"
b =3D "b"
#=3D> "b"
b.fooify
#NoMethodError: undefined method `fooify' for "b":String
# from (irb):9
# from :0


Hi, I know that I can extend a Class instance (an object) with a
method(s) by doing:

object.extend Module

but can I do it in a "inline" way without the need of a module?
I just want something as follows:

time =3D Time.now
object.add_method 'def say_time(); return time; end'

irb> object.say_time
--> time

Note that I **don't** want to include the "add_method" in object class
or in Object class. This is, I don't want to "dirty" object class.

Also note that I can't use "object.extend Module" since the method
"say_time" must return a variable "time" value that I pass as an
argument.

Is there some way? Thanks a lot.


--=20
I=F1aki Baz Castillo
<[email protected]>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkisT1kACgkQJA/zY0IIRZYMfgCgsA7iBHQbirDuP+91V90C32pu
EdEAmwUg+0ueHuCgdesl4jvwnMFjNc7Q
=3DQgJ9
-----END PGP SIGNATURE-----
 
M

Michael Granger

Hi, I know that I can extend a Class instance (an object) with a
method(s) by doing:

object.extend Module

but can I do it in a "inline" way without the need of a module?
I just want something as follows:

time =3D Time.now
object.add_method 'def say_time(); return time; end'

irb> object.say_time
--> time

You can do this with singleton methods:

irb(main):001:0> object =3D Object.new
=3D> #<Object:0x304af8>

irb(main):002:0> def object.say_time; Time.now; end
=3D> nil

irb(main):003:0> object.say_time
=3D> Wed Aug 20 10:13:29 -0700 2008
 
I

Iñaki Baz Castillo

El Mi=E9rcoles, 20 de Agosto de 2008, Michael Granger escribi=F3:
You can do this with singleton methods:

irb(main):001:0> object =3D Object.new
=3D> #<Object:0x304af8>

irb(main):002:0> def object.say_time; Time.now; end
=3D> nil

irb(main):003:0> object.say_time
=3D> Wed Aug 20 10:13:29 -0700 2008


Thanks to both :)


=2D-=20
I=F1aki Baz Castillo
 

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

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top