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

Anonymous Anonymous 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

Blogger shengda xu said...

pandora soldes
yeezy boost
yeezy shoes
mulberry bags
clarks shoes outlet
coach factory outlet online
toms outlet
cheap rolex replica watches
nike sneakers
coach factory outlet online

2:45 AM, July 14, 2017

Blogger Quang Đào Duy said...

Tọa lạc tại vị trí đắc địa Chung cư 378 Minh Khai được mệnh danh là " Viên Ngọc" giữa lòng thành phố với vị trí không thể thuận tiện hơn tại nút giao thông huyết mạch nối liền các trung tâm Chính trị, Văn hóa,Trường học, Bệnh viện, Sân gold của thủ đô Hà Nội

Dự án Green Pearl tọa lạc tại số 378 Minh Khai , Hai Bà Trưng, Hà Nội là một vị trí đắc địa thuộc cửa ngõ phía Nam của Thủ đô, nơi tập trung các tuyến giao thông huyết mạch của TP Hà Nội (Đường Minh Khai - Cầu Vĩnh Tuy). Dự án bao gồm nhà ở liền kề, biệt thự và căn hộ cao cấp Green Pearl mở ra không gian sống xanh, trong lành, KHÔNG ồn ào, KHÔNG khói bụi ngay trong lòng đô thị.

Dự án 378 Minh Khai hiểu rõ vui chơi là hình thức tốt nhất giúp trẻ phát triển tư duy. Từ đó chúng tôi xây dựng trường học hiện đại, khu vui chơi trẻ em với những trò vui chơi giải trí, trí tuệ giúp trẻ phát triển cả tư duy sáng tạo và thể chất.

Chung cư Green Pearl
Chung cư Green Pearl Không chỉ là giải pháp đáp ứng nhu cầu nhà ở chất lượng cao, sứ mệnh của Green Pearl là cùng bạn trải nghiệm cuộc sống" bình yên trọn vẹn "tách biệt với cuộc sống ngột ngạt ồn ào bên ngoài. Không gian yên tĩnh trong lành được khai thác tối đa trên những con đường tản bộ xanh mát sẽ giúp bạn cảm thấy thư thái nhằm tái tạo năng lượng hiệu quả nhất.
Mặt bằng chung cư Green Pearl
Tiện ích chung cư Green Pearl
Giá bán chung cư Green Pearl
Chung cư Green Pearl

8:21 PM, January 12, 2018

Anonymous mahesh said...

I enjoy reading a post that can make men and women think. Also, thanks for permitting me to comment! best spy camera app , hidden camera detector app

9:57 PM, January 29, 2018

Anonymous naresh said...

You need to take part in a contest for one of the greatest websites on the internet. I most certainly will recommend this website! pseg customer service ,pseg power outage

8:43 PM, January 31, 2018

Anonymous google said...

how to change skins in minecraft pc

create your minecraft skin

download your minecraft

change skins minecraft

minecraft create your own skins

3:36 AM, March 10, 2018

Anonymous Pengobatan Hilangkan Batu Kandung Kemih said...

The article is very interesting and very understood to be read, may be useful for the people

Pengobatan Untuk Hilangkan Mata Ikan
Pengobatan Atasi Luka Bakar
Cara Ampuh Mengobati Radang Usus
Pengobatan Atasi Radang Tenggorokan
Obat Hilangkan Cairan Di Paru-Paru
Obat Untuk Menyembuhkan Disentri

12:02 AM, March 12, 2018

Anonymous google said...

dogs food
dogs health
dogs heat cycle
dog food
dog can have it

9:57 PM, March 19, 2018

Anonymous varaprasad said...

click here
uber customer care chennai
uber chennai customer care
know more about here
uber customer care

2:08 AM, March 21, 2018

Anonymous Obat infeksi rahim said...

may be useful for all, helpful article once and pardon me permission to share also here :

Obat gabagen ampuh
Khasiat apel hijau untuk menurunkan berat badan
Cara mengatasi aliran darah tersumbat
Jian Xin Capsule obat spesialis jantung
Obat kanker hati
Cara menyembuhkan radang usus
Cara menyembuhkan pneumonia

6:10 PM, March 23, 2018

Blogger Cipry Cipro said...

I am typically to running a blog and i really admire your content. The article has actually peaks my interest. I am going to bookmark your site and maintain checking for brand new information. I wonder how a lot attempt you place to create this type of wonderful informative web site. Thanks!
Visit friv 2 online the best free online games

1:34 AM, March 25, 2018

Blogger Kartono said...

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks cara mengobati luka diabetes agar cepat keringobat ginjal bengkak di apotikcara menyembuhkan rayapan

11:48 PM, March 29, 2018

Blogger Denisa Cretu said...

This is a great web site. Good sparkling user interface and very informative blogs. I will be coming back in a bit, thanks for the great article. I found so many interesting stuff in your blog especially its discussion. Thanks!
here is the friv page if you want to have fun.

6:52 AM, March 30, 2018

Anonymous manik said...

click here
know more
can run it
can my pc run it
system requirements

10:43 PM, March 30, 2018

Blogger Andri said...

It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing.
obat sakit kencing
obat ginjal bengkak
obat maag kronis tradisional
obat pengapuran tulang belakang
obat tradisional salpingitis
obat kencing manis
obat perut begah
obat tuba falopi tersumbat

8:27 PM, April 02, 2018

Anonymous manik said...

Hi do check out this latest post with our New Blog Post and I hope you would have enjoyed all our post tylenol dosing for dogs

1:01 AM, April 03, 2018

Blogger Hermawan Alfaqih said...

I am happy that you simply shared this helpful information with us. Please stay us informed like this. Thank you for sharing. cara menyembuhkan bronkitis secara alami obat infeksi saluran kencing

8:51 PM, April 04, 2018

Anonymous feruz said...

Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here. flipkart customer care number toll free

1:45 AM, April 06, 2018

Anonymous ekshith said...

I am typically to running a blog and i really admire your content. The article has actually peaks my interest. I am going to bookmark your site and maintain checking for brand new information. unblocked games run 3

4:01 AM, April 17, 2018

Blogger pooja rani said...

Mobile USSD Codes
Jio USSD Code
jio sim port to other number
jio balance transfer code
check jio number
jio loan number code
jio internet data usage
jio toll caller
jio caller tune number
jio dnd start

3:29 AM, April 27, 2018

Blogger qqqqqqqqqqqq said...

lebron 14
jordan 13
nike free run
oakley sunglasses for men
nike free
air max 2018
adidas outlet

10:36 PM, May 18, 2018

Blogger Fahrezy agra said...

If you have a history of illness that is difficult to recover, maybe our next article will help you to recover

Suplemen Penambah Nafsu Makan Anak
Cara Menghilangkan Hemangioma
Cara Mengatasi Sakit Pada Buah Zakar
Cara mengobati penyakit Lyme
Cara Mengobati Radang Telinga

5:53 PM, May 21, 2018

Blogger jing dong said...

moncler outlet
cheap nba jerseys
goyard bags
nike air amx 270
michael jordan shoes
michael kors uk
yeezy 700
burberry outlet
adidas outlet

1:01 AM, May 26, 2018

Anonymous Jelly Gamat Qnc said...

It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing.
obat keloid
obat ginjal bengkak
obat thalasemia
obat limpa bengkak
obat kencing manis

6:03 PM, June 07, 2018

Blogger Deden Hidayat said...

may be useful for all, helpful article once and pardon me permission to share also here :

Cara menyembuhkan sariawan di vagina
Cara menyembuhkan eksim
Cara menyembuhkan emfisema
Obat tradisional tipes
Cara menyembuhkan infeksi lambung
Cara menyembuhkan tumor rahim
Obat radang panggul

8:30 PM, June 07, 2018

Blogger ben daigege said...

michael kors handbags
coach outlet
kate spade outlet
michael kors outlet
armani exchange
air max 97
karen millen dresses
stussy clothing
minnetonka outlet
canada goose coats

6:31 PM, June 18, 2018

Anonymous Anonymous said...

anime streaming sites
watch anime online websites
best sites watch anime
best free anime sites
best site to watch anime

2:30 AM, July 05, 2018

Blogger Freedom Apk said...

new iphone no service
iphone 6 says no service
Download Now
iphone 6 searching for service
iphone 6 no service

2:21 AM, July 12, 2018

Blogger Stjsrty Xtjsrty said...

nike shoes
louboutin shoes
canada goose outlet
nike huarache femme
football pas cher
jordan 4
ugg boots clearance
supreme shirt
prada shoes

7:03 PM, July 15, 2018

Blogger Zahra Julia said...

thank you very useful information admin, and pardon me permission to share articles here may help Cara menyembuhkan asam urat

12:14 AM, August 06, 2018

Anonymous Cara menyembuhkan bronkitis kronis said...

thank you very useful information admin, and pardon me permission to share articles here may help :

Obat tenggorokan terluka
Obat kebas
Cara menyembuhkan vaginitis
Obat gagal jantung kongestif
Obat kardiomegali ampuh
Obat liver bengkak ampuh
Walatra sehat sendi

6:54 PM, August 10, 2018

Anonymous Cara menyembuhkan rematik said...

may be useful for all, helpful article once and pardon me permission to share also here :

Obat dispepsia
Obat stretch mark
Obat batuk kering
Obat asam lambung ampuh
Obat penyakit gerd
Obat tradisional miom
Cara mengatasi sinusitis

10:51 PM, August 19, 2018

Blogger Master Judi said...

Agen Judi Bola Resmi

Agen Bola Resmi

1:07 AM, August 24, 2018

Blogger Amanda Aulia said...

Bismillah Hopefully given the health and sustenance abundant amen :))
pusat herbal QnC Jelly gamat
khasiat QnC Jelly gamat
Cara Memesan Herbal QnC Jelly Gamat
Obat Luka Bakar Alami
Obat Batu ginjal Alami
Obat Herbal Jelly Gamat QnC
distributor herbal terpercaya

11:36 PM, August 24, 2018

Blogger jellygamat murah said...

Hopefully, sustenance will be easy and simplified in all matters :-)
cara mengobati ambeien
obat jantung berdebar
obat penyakit hipertensi
obat gondongan alami

9:28 PM, August 27, 2018

Blogger 5689 said...

nike huarache
canada goose outlet
kate spade outlet
kate spade outlet
yeezy boost 350 v2
ecco shoes
ugg boots clearance
ed hardy clothing
supreme new york
polo ralph lauren outlet

6:22 PM, September 01, 2018

Blogger Uje No Limit said...

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much obat benjolan di selangkangan paha

11:49 PM, September 05, 2018

Blogger 洛噷 said...

2018906 leilei3915
ugg boots outlet
louboutin shoes
coach outlet
polo ralph lauren
michael kors outlet online
pandora jewelry
coach factory outlet
ray ban sunglasses
fitflops sale
uggs outlet

12:01 AM, September 06, 2018

Blogger nuy keroppi915 said...

thank you very useful information admin, and pardon me permission to share articles here may help Cara mengobati radang panggul

2:36 AM, September 07, 2018

Blogger shinta agustian said...

Hopefully, sustenance will be easy and simplified in all matters :-)
cara melancarkan bab secara alami
obat penyakit ispa
cara menurunkan leukosit leukositopenia tinggi obat penumbuh Sel kulit baru

11:05 PM, September 12, 2018

Blogger risa icha said...

Hello, we are from the health site admin, want to share our latest articles :

obat benjolan di pipi
obat radang lambung
obat benjolan di kelopak mata
obat batu empedu
obat kencing nanah

1:36 AM, September 18, 2018

Blogger Lutfi Kurniawan said...

Sharing information about herbal treatment

Cara Memperbaiki Kondisi Mata Kabur Atau Buram
Cara Mengobati Luka Bernanah Pada Penderita Diabetes
Bahaya Benjolan Di Leher Dan Cara Menghilangkannya
Cara Alami Menghilangkan Rasa Perih Pada Lambung
Manfaat Qnc Jelly Gamat Untuk Kesehatan Tubuh
Cara Menghilangkan Gatal Dan Benjolan Bernanah Pada Kulit Anak-anak Dan Orang Dewasa

7:16 PM, September 20, 2018

Blogger Lutfi Kurniawan said...

Good and well-organized articles, thank you for sharing this information

Obat Sakit Tenggorokan
Obat Kelenjar Getah Bening Di Leher
Cara Cepat Menyembuhkan Patah Tulang
6 Cara Mencegah Dan Mengatasi Pengeroposan Tulang
Cara Mengatasi Nyeri Tulang Dan Sendi Paling Ampuh

7:17 PM, September 20, 2018

Blogger Unknown said...

Given article is very helpful and very useful for my admin, and pardon me permission to share articles here hopefully helped :

Obat dompo
Obat kutil kelamin pria
Obat radang amandel tanpa operasi
Obat radang tenggorokan pada bayi
Obat benjolan di kepala

12:38 AM, September 29, 2018

Blogger risa icha said...

I am happy to be here and this wonderful article. I have found here lots of interesting information for my knowledge I need. all the details you provide to us, it was very helpful and useful, thanks for sharing this post.

khasiat obat herbal
Obat penyakit kulit
obat benjolan
toko obat herbal
obat gondok beracun
risa herbal

1:49 AM, October 06, 2018

Blogger Walisongo Herbal said...

thank you very useful information admin, and pardon me permission to share articles here may help :

Obat nyeri telapak kaki
Obat tumor rahang
Obat radang paru paru
Obat angin duduk tradisional
Obat lambung bengkak
Obat abses payudara
Obat polip hidung tanpa operasi

9:00 PM, October 09, 2018


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


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