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

Blogger Lai Weizhou said...

chung cư tháp doanh nhân
tháp doanh nhân / tháp doanh nhân hà đông / dự án tháp doanh nhân hà đông / chung cư the legend / chung cư the legend 109 nguyễn tuân
chung cư 360 giải phóng / dự án 360 giải phóng / chung cư vietracimex / chung cư 201 minh khai / chung cư thanh xuân tower / chung cư 35 lê văn thiêm
chung cư eco green city / chung cu eco green city / chung cư eco green city nguyễn xiển / chung cu eco green city nguyen xien / eco green city / eco green city nguyễn xiển / dự án eco green city / toà ct4 eco green city / toà ct3 eco green city / toà ct1 eco green city

11:09 AM, May 21, 2016

Blogger Zhenhong Bao said...

longchamp handbags
coach outlet
true religion canada
oakley sunglasses
mulberry handbags
reebok shoes
burberry outlet
ralph lauren outlet
ralph lauren polo
ralph lauren
hermes belt for sale
versace sunglasses on sale
fitflops clearance
nike air max 90
thomas sabo outlet
pandora outlet
tory burch outlet
coach outlet
kobe shoes
oakley sunglasses
ghd hair straighteners
ray ban sunglasses
coach outlet online
tory burch shoes
coach outlet
ralph lauren outlet
mulberry handbags
michael kors outlet online
discount oakley sunglasses
burberry sunglasses on sale
ralph lauren pas cher
replica watches
oakley sunglasses
chrome hearts outlet online
longchamp pliage

6:20 PM, May 30, 2016

Blogger hoa pikey said...

A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
duck life 3 | Slither io |Red Ball 3 |
red ball 2| gmail sign in| Gmail login|

3:13 AM, June 22, 2016

Blogger Regina Hilary said...

The war between humans, orcs and elves continues. Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castles. Researching and upgrading wisely will be crucial to your success!
slitherio | unfair mario 2 | age of war 2
The game controls are shown just under . Movement mechanisms primarily include acceleration and tilting controls.
cubefield | tank trouble | happy wheels |earn to die 1 |
earn to die 6 |earn to die 6| duck life

7:26 PM, July 06, 2016

Blogger Linda Rose said...

This is a great article. It gave me a lot of useful information. thank you very much.
Baixar Facebook | whatsapp baixar | Facebook Baixar | Baixar Facebook Gratis
|baixar whatsapp | baixar whatsapp gratis
|Traffic Rider | Traffic Rider Jogo | Traffic Rider Baixar
|Facebook Lite | Baixar Facebook Lite |

9:08 PM, July 28, 2016

Blogger Hua Cai said...

true religion jeans
snapbacks wholesale
michael kors outlet
nike air max 90
juicy couture outlet
ugg boots
air max 2015
fred perry polo
ralph lauren uk
nike roshe run
air max 90
fitflop sale
ugg outlet
nike roshe run shoes
nike roshe run
nike air max
oakley sunglasses
true religion jeans
longchamp handbags
tiffany jewellery
ugg outlet
cheap nfl jerseys
air max 90
nike outlet online
cheap soccer jerseys
nike air force 1
tory burch outlet online
rolex watches
tiffany jewelry
coach outlet online
chicago blackhawks jerseys
louis vuitton handbags outlet
lululemon outlet
reebok trainers
michael kors outlet

10:42 PM, August 03, 2016

Blogger Fangyaya said...

abercrombie and fitch
tod's shoes
ugg boots uk
jordan 13
michael kors handbags
ugg outlet
uggs uk
toms shoes
michael kors outlet
canada goose jackets
coach outlet store online clearances
michael kors outlet clearance
louis vuitton outlet
canada goose outlet
michael kors handbags
oakley vault
louis vuitton handbags
ugg boots
michael kors outlet
the north face jackets
gucci outlet
louis vuitton outlet
louis vuitton outlet stores
michael kors handbags
celine handbags
christian louboutin outlet
true religion outlet
adidas nmd

6:56 PM, September 07, 2016

Blogger John said...

ugg outlet
burberry bags
cheap ugg boots
cheap oakley sunglasses
ugg pas cher
polo ralph lauren outlet online
ysl handbags
oakley sunglasses outlet
ed hardy outlet
michael kors outlet canada

6:23 PM, September 26, 2016

Blogger chenlina said...

ugg australia
oakley sunglasses
coach outlet
tods shoes
oklahoma city thunder jerseys
fitflops uk
michael kors outlet
ed hardy clothing
designer handbags
coach outlet store online

7:51 PM, October 17, 2016

Blogger chenmeinv0 said...

canada goose jackets
ugg canada
michael kors outlet online
uggs outlet
nike roshe run shoes
true religion jeans
ray ban glasses
hogan sito ufficiale
true religion
ralph lauren polo

10:22 PM, January 03, 2017

Blogger Unknown said...

adidas nmd r1
dolce and gabbana
kansas city chiefs jerseys
golden state warriors
michael kors handbags
cheap ray bans
birkenstock sandals
coach handbags
nike blazer pas cher
jacksonville jaguars jersey

12:30 AM, March 16, 2017

Blogger shengda xu said...

longchamp sale
yeezy shoes
tods outlet
coach factory outlet online
true religion jeans outlet
michael kors
ralph lauren
polo ralph lauren outlet
kate spade purses
louis vuitton

6:44 AM, April 18, 2017

Blogger shengda xu said...

moncler jackets
birkenstock shoes
michael kors outlet online
fake rolex watches
mlb jerseys
discount oakley sunglasses
ray ban glasses
air max 90
nike flyknit

6:44 AM, April 18, 2017


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


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