Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Thursday, August 03, 2006

Dynamically Add Methods to Classes Through their Objects in Ruby

Playing with the implementation of a new library I am working on called starfish (I will blog about it shortly), I came up with this fun little Ruby hack that makes Ruby seem more like a prototype-based language than it already does.

class Foo
def method_missing(name, *args)
if name.to_s =~ /(.*)=$/ && args[0].is_a?(Proc)
self.class.instance_eval do
define_method($1, args[0])

f = Foo.new
f.greet = lambda {|t| "Hello #{t}!"}
f.greet "Lucas Carlson" # => Hello Lucas Carlson!

j = Foo.new
j.greet "World" # => Hello World!

Hope you enjoy it as much as I do and I can't wait to show you how I am using it!

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!


Anonymous Anonymous said...

What's wrong with require "ostruct" ?

9:20 AM, August 04, 2006

Blogger Lucas Carlson said...

OpenStruct is a great library, but does not add arbitrary methods at run time. For example:

require 'ostruct'
x = OpenStruct.new
x.bar = lambda{|x|puts x}
x.bar(1) # => #Proc:0x013e8b34@(irb):8

That is not what you would expect, you want it to print out 1.

10:34 AM, August 04, 2006

Anonymous Anonymous said...

That's pretty cool. As I do not seem to be very imaginative today, could you give an example of where this technique may be useful?

2:32 PM, August 04, 2006

Blogger Lucas Carlson said...

I am using it a library I am about to announce, so you will see a great use in a few days.

2:34 PM, August 04, 2006

Anonymous Anonymous said...

if the only thing is invoking that lambda in a method, just:

2:58 PM, August 04, 2006

Anonymous Anonymous said...

oops, I didn't escape the & :|

3:00 PM, August 04, 2006

Blogger Lucas Carlson said...

Good point, I have changed that in the code example, thanks.

4:01 PM, August 04, 2006

Blogger jChris said...

that's fun!

4:19 AM, August 05, 2006

Anonymous Anonymous said...

a really good idea, oh i've just realised that you could call self.class.send to get to the private method rather than use instance_eval, I suppose it's a little bit shorter.

if name.to_s =~ /(.*)=$/ && args[0].is_a?(Proc)
self.class.send(:define_method, $1, args[0])

1:53 AM, August 06, 2006

Blogger Lucas Carlson said...

I have posted the way I am using this in a new blog post.

2:07 PM, August 23, 2006

Blogger geirfugl said...

Really good stuff. Thanks!

8:57 AM, January 18, 2007

Anonymous Anonymous said...

i buy hydrocodone at buy hydrocodone - can't find any cheaper

5:48 AM, January 28, 2007

Anonymous Ramonita said...

This is fantastic!

2:31 AM, September 26, 2013

Blogger lina123 Chen said...

toms shoes
air max shoes
jordan 11 concord
louis vuitton outlet
kids lebron james shoes
michael kors outlet
ralph lauren sale
burberry bags
ray ban sunglasses
ralph lauren outlet
hollister clothing store
michael kors bag
red timberland boots
michael kors uk
celine handbags
michael kors handbags
coach outlet
concords 11
air max 95
coach factory outlet
coach outlet online
coach outlet store online
true religion jeans
polo outlet
louis vuitton uk outlet
tod's shoes
louboutin shoes
pandora bracelets
abercrombie and fitch
louis vuitton
coach outlet
michael kors
michael kors
cheap toms sale for kis
jordan 13 shoes
louis vuitton
ray ban sunglasses outlet
ray ban sunglasses
chanel outlet online

6:21 PM, June 25, 2015

Blogger Nesma Ali said...

شركة جلي بلاط شرق الرياض
شركة تنظيف مسابح شرق الرياض
شركة تنظيف خزانات شرق الرياض
شركة عزل خزانات شرق الرياض
شركة كشف تسربات المياه شرق الرياض
شركة تسليك مجارى شرق الرياض
شركة تنظيف موكيت شرق الرياض
شركة تنظيف مجالس شرق الرياض
شركة نقل اثاث شرق الرياض
شركة شفط بيارات شرق الرياض
شركة تنظيف شرق الرياض
شركة تنظيف منازل شرق الرياض
شركة عزل الاسطح شرق الرياض
‪شركة رش المبيدات شرق الرياض
شركة مكافحة الحشرات شرق الرياض
شركة تنظيف قصور شرق الرياض
شركة مكافحة الفئران شرق الرياض
شركة لمكافحة النمل الابيض شرق الرياض

شركة تنظيف فلل شرق الرياض
شركة مكافحة الصراصير شرق الرياض

1:27 AM, September 02, 2015

Blogger Da Shuai Guo said...

oakley sunglasses, http://www.oakleysunglassescanada.com/
ray ban sunglasses outlet, http://www.raybansunglass.us.com/
cheap jordans, http://www.cheapjordanshoes.in.net/
coach outlet online, http://www.coachoutletstores.com.co/
mbt shoes outlet, http://www.mbtshoesoutlet.us.com/
michael kors factory outlet, http://www.michaelkorsfactoryoutlet.us.org/
coach outlet store, http://www.coachoutletonline-store.us.com/
asics, http://www.asicsisrael.com/
nike roshe run, http://www.rosherunshoessale.com/
iphone 6 cases, http://www.iphonecase.name/
ralph lauren uk, http://www.ralphlaurenoutletuk.org.uk/
real madrid jersey, http://www.realmadridjerseystore.com/
bears jerseys, http://www.chicagobearsjerseys.us/
ralph lauren outlet, http://www.ralphlaurenoutletuk.org.uk/
christian louboutin outlet, http://www.christianlouboutinoutlet.name/
polo ralph lauren, http://www.poloralphlauren.us.org/
louis vuitton outlet store, http://www.louisvuittonoutlet.in.net/
tory burch outlet, http://www.toryburch.in.net/
dolphins jerseys, http://www.miamidolphinsjersey.com/
replica handbags,replica handbags outlet,cheap handbags,purses and handbags,wholesale replica handbags,replica bags,bags outlet,replica handbags wholesale,cheap purses,discount handbags,handbags sale,wholesale purses,handbags and purses,replica purses
basketball shoes, http://www.basketballshoes.us.com/
coach outlet, http://www.coach-outlet-store.us.com/
true religion jeans, http://www.truereligionoutletstore.us.com/
ralph lauren outlet, http://www.ralphlaurenoutlet.in.net/
herve leger, http://www.herveleger.us.com/

1:07 AM, September 15, 2015

Blogger Minko Chen said...

michael kors outlet
barbour jackets
tiffany and co
ed hardy clothing
miami dolphins
toms outlet store
coach handbags outlet
true religion outlet
nfl jerseys
snapbacks wholesale
true religion outlet
pittsburgh steelers
oakley sunglasses wholesale
cyber monday deals
mac cosmetics
canada goose outelt
canada goose jackets
abercrombie and fitch
michael kors outlet
michael kors factory online

9:07 PM, November 13, 2015

Blogger Xiaozhengm 520 said...

2016-3-19 xiaozhengm
ray ban
rolex submariner
rolex watches
kate spade outlet
omega watches
michael kors uk
ray ban sunglasses
toms outlet
coach factory outlet
coach outlet online
calvin klein underwear
ed hardy outlet
kate spade outlet online
nike tn
michael kors outlet
ray bans
michael kors outlet online
michael kors outlet
prada outlet
ghd hair straighteners
ray ban outlet
ralph lauren outlet
true religion outlet
ugg boots
true religion jeans
cartier watches
michael kors handbags
nike air max shoes
coach outlet
vans sk8 hi
ralph lauren uk
christian louboutin uk
ralph lauren outlet
oakley sunglasses
vans shoes
michael kors outlet
michael kors outlet
polo outlet
michael kors handbags

5:05 AM, March 18, 2016


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


If you like this blog, you might also like top photography schools.