ruby, fun May 2, 2006 8:43 pm (Save post)
(post in english for LiquidDevelopment compat
)
Chiaroscuro is having fun with ruby metaprogramming recipes and so will I.
My version does not contain cool tricks such as allow_method_chaining but I think it is fun anyway, so I here it is.
I’ll start straight with some numeric stuff:
class Integer
names = %w[ one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen
sixteen seventeen eighteen nineteen ]
NAMES=Hash[*names.zip((1..15).to_a).flatten]
end
Yeah, I know I could hack the 11..19 group with a map, but I think it is not that nice.
Now, replace the ugly $global with a constant, and make @ingredients readable:
class Recipe
KNOWN={}
attr :ingredients
The proposal is to make something like this work:
Recipe.new \"breakfast\" {
one of cappuccino
two of brioche
}
so we have to handle one/two etc as methods. Obviously we even have to handle of and then get the rest with method_missing, so the basic code for initialize will need to call instance_eval:
def initialize(recipe_name , &block)
@name = recipe_name
@ingredients = {}
instance_eval &block
KNOWN[recipe_name]=self
end
Notice the ultrasimple handling of registered recipes
Now, we can define of as a simple method that returns it’s argument:
def of(thing)
thing
end
and handle ingredients via method_missing:
# XXX: we could raise if args.size > 0
def method_missing(sym,*args)
sym.to_s
end
Finally, the code for the numbers/methods may be something like:
for name in Integer::NAMES.keys
eval %{
def #{name}(*values)
first= values.shift #coffee
values.push \"#{name}\" # [ \"macchiato\", \"one\"]
@ingredients[first]=values
end
}
end
There are two things to say here: first, we need to store the name of the ingredient as the key and put the number as a value in it, together with any optional thing. Second, we can’t use for or each with define_method and we have to fall back on eval.
This happens because if we used a block with it, it would be a closure, and thus the last value assigned to name would be carried on by all the methods.
Mh.. is this perfect?
Not really, as of now this code allows you to write
Recipe.new(n) do
one coffee, macchiato
ten of saccottino
end
But it will fail miserably with one of coffee, macchiato.
It is quite easy to handle this case if you choose to disallow commands with missing of, by redefining it to take variable arguments (and changing the number-methods accordingly), but to make it work fine in both cases there is the need for ugly checks with and this is left as an exercise for the reader
Finally, you have to set $VERBOSE to nil so that the intepreter does not spit out warnings when it tries to interpret foo bar baz, quux, which it recognizes as foo(bar(baz,quux)).
Oh, by the way, I did not convert numbers to integers, since I found it was useless, but you can safely use the Hash that I set up for this.
Thanks for reading till here, now get the full code with my minimal test suite
PS
chiaroscuro, ho cancellato un tuo vecchio commento qui sopra per sbaglio, scusami


that was a deadly uppercut for me. Anybody else taking the deadly meta-breakfast challenge?
Comment by Chiaroscuro — May 2, 2006 @ 9:13 pm
Consider using define_method(name) {|*values| … instead of eval %{ def #{name} (values) …
Your syntax-highlighting editor will thank you for it.
Comment by Dave Burt — May 3, 2006 @ 12:34 pm
Thanks for your comment dave, but that won’t work.
As I wrote, if you use define_method you’d get a closure, thus the code for the defined method will bring a reference to the last value of “name” and you’ll get the same result for all the methods:
Comment by gabriele — May 3, 2006 @ 3:26 pm
Gabriel: you can still use define_method if you do it like this:
>> class C
>> %w(one two).each do |i|
>> define_method i do i end
>> end
>> end
=> ["one", "two"]
>> C.new.one
=> "one"
this is because the scope of the environment in which i is defined, and which define_method closes over, differs in the two versions. When you use the block, a new scope/environment is created just for that block, containing i, whereas in your version with foreach, i remains in the same wider scope throughout…
(confusing, yes)
Comment by Matthew — May 4, 2006 @ 12:09 am
matthew: ah, yes, great point thank you
Comment by gabriele — May 4, 2006 @ 7:15 am
These comments have been invaluable to me as is this whole site. I thank you for your comment.
Comment by Rosie — April 30, 2007 @ 1:52 pm
Wow! Great tinkhing! JK
Comment by River — June 3, 2011 @ 2:24 pm
8tWtuK ylolrwmhvpcs
Comment by hnyupnfe — June 4, 2011 @ 7:11 am
fUTf3q fkshohfsluao
Comment by cmdpjpbqdw — June 6, 2011 @ 7:32 am
My version does not contain nhl jerseys cheapcool tricks such as allow_method_chaining but I think it is fun anyway, so I here it is.
Comment by cheap nfl jerseys — August 27, 2011 @ 9:37 am
One way to think about taxation is that the government have an option on any income. So, the government’s position is always the most stable of any institution in the economy, as long as it can maintain it’s monopoly of coercion. But, this doesn’t mean what some think it does.
Comment by cheap jerseys online — September 5, 2011 @ 8:20 am
I performed searches on the topic and found most people will agree with your blog. Thanks for sharing this information.http://www.realsaleshop.com
Comment by Cheap Jerseys — September 9, 2011 @ 9:34 am
One way to think about taxation is that the government have an option on any income. So, the government’nfl jerseys cheaps position is always the most stable of any institution in the economy, as long as it can maintain it’s monopoly of coercion. But, this doesn’t mean what some think it does.
Comment by wholesale jerseys — September 16, 2011 @ 8:48 am
My version does not contain cool tricks such as allow_method_chaining but I cheap jerseys from chinathink it is fun anyway, so I here it is.
Comment by cheap jerseys from china — September 28, 2011 @ 7:13 am
I truly appreciate all your work furthermore providing plenty of worthwhile info for the audience.
http://www.nfljerseysoutlets.us/nfl-jerseys-c-1.html (Cheap NFL Jerseys)
Comment by Cheap NFL Jerseys — October 7, 2011 @ 2:50 am
GOOD, IT IS VERY NICE. I LIKE IT.following time I learn a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to learn.
Comment by True Religion Outlet — October 12, 2011 @ 7:58 am
I definitely enjoyed reading it, you may be a great author.I will make sure to bookmark your blog and will often come back someday.
Comment by ugg outlet — October 17, 2011 @ 3:51 am
thank you for share the good knowlege. So beautiful posts,all I like,hope to be better then, and enjoy yourself,good luck in your life. Thanks for your ideas to something. Great post, thanks very much, please write more and more about this.
Comment by Art Monk Jersey — October 17, 2011 @ 6:38 am
Your blog is very informative.This is obviously one great post.i keep on reading articles from here.thanks for sharing..
Comment by Jason Campbell Jersey — October 17, 2011 @ 6:38 am
I really like this website, And hope you will write more ,thanks a lot for your information.
Comment by John Kuhn Jersey — October 17, 2011 @ 6:39 am
I’m still learning from you, but I’m improving myself. I definitely liked reading everything that is written on your blog.Keep the information coming. I liked it!
Comment by Jahvid Best Jersey — October 17, 2011 @ 6:40 am
i love it very much.shop the Burberry Bags online is wonderful. http://www.cheapmonclerjacketsell.com/ugg-boots-c-6.html (Cheap Ugg Boots)
Comment by NFL Jerseys Wholesale — October 18, 2011 @ 2:17 am
My version does not contain nfl jerseys cheapcool tricks such as allow_method_chaining but I think it is fun anyway, so I here it is.
Comment by cheap jerseys from china — October 22, 2011 @ 8:03 am
Very useful articles. 传奇私服 I and my friend to do some research. I am very glad to see such information, I find the very long period of time, was finally I found, thank you.
Comment by cheap nfl jerseys — October 25, 2011 @ 5:58 pm
Hi there! Awesome topic, however internet site is unable to display screen around FF4 try out. Really does the web server neighborhood this Ip? Thank you.
Comment by ugg boots sale — October 26, 2011 @ 3:34 am
Originality is not seen in single words or even sentences. Originality is the sum total of a man’s thinking or his writing.
Comment by Uggs Boots Shop — October 26, 2011 @ 3:35 am
Thank u for your sharing. 传奇私服 Your blog has a unique feature that can make the people who reads become happy.After
reading your blog,I feel happy.
Comment by cheap nfl jerseys — October 26, 2011 @ 9:57 am
I truly appreciate all your work furthermore providing plenty of worthwhile info for the audience.
Comment by cheap jerseys — November 1, 2011 @ 11:50 am
Howdy! Very good subject matter, your internet site fails to display screen on FF4 toy with. Does indeed any equipment prohibit the IP address? Appreciate it.
Comment by ugg boots — November 4, 2011 @ 9:27 am
His times using the Bears started to be numbered using the signing of Marion Barber, and any attempts to business him went nowhere. but do not exercise using the team. He is anticipated to participate in Wednesday’s workout and be available when Arizona opens its time period Sunday at house against the Carolina Panthers.yangchengbin/201111
Comment by NFL jerseys2011-12 — November 5, 2011 @ 8:01 am
I’m still learning from you, but I’m improving myself.
Comment by Aaron Rodgers Jersey — November 8, 2011 @ 6:43 am
I definitely liked reading everything that is written on your blog.Keep the information coming. I liked it!
Comment by Charles Woodson Jersey — November 8, 2011 @ 6:44 am
Hey, your website is quite interesting.. And it was something I can definitely connect with Ill
constantly visit in your blog therefore i hope you continue making fun and interesting posts such as this one…
Comment by B.J. Raji Jersey — November 8, 2011 @ 6:45 am
There are no secrets to success. It is the result of preparation, hard work, and learning from failure.
Comment by Clay Matthews Jersey — November 8, 2011 @ 6:45 am
Simply wish to say your article is as surprising. The clarity in your post is simply excellent and i can
assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up
to date with forthcoming post. Thanks a million and please carry on the rewarding work.
Comment by Donald Driver Jersey — November 8, 2011 @ 6:45 am
Simply wish to say your article is as surprising. The clarity in your post is simply excellent and i can
assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up
to date with forthcoming post. Thanks a million and please carry on the rewarding work.
Comment by Donald Driver Jersey — November 8, 2011 @ 6:46 am
Very interesting and imformative website.Thanks post,i like reading your website so much.
Comment by cheap ugg boots — November 10, 2011 @ 11:16 am
MBT healthful shoes using the rub, need to loose the shoes above the sludge removal, and allow your
shoes dried out naturally. Soiled shoes are recommended for cleanse pace determined.
Comment by MBT Sport Shoes — November 18, 2011 @ 7:49 am
Put on Atlanta Falcons Red Fitted Hat,support your favorite team. Offering a flat visor flex with a unique 2-in-1 shape, the hat delivers a remarkably comfortable feel. It features a raised team drawings, including logo and an embroidered team name. Atlanta Falcons Red Fitted Hatis the best way to show off your favorite team with a perfect fit, just for you! yangchengbin/201111
Comment by NFL Accessories — November 21, 2011 @ 7:06 am
Put on Atlanta Falcons Red Fitted Hat,support your favorite team. Offering a flat visor flex with a unique 2-in-1 shape, the hat delivers a remarkably comfortable feel.It features a raised team drawings, including logo and an embroidered team name. Atlanta Falcons Red Fitted Hatis the best way to show off your favorite team with a perfect fit, just for you! yangchengbin/201111
Comment by NFL Accessories — November 21, 2011 @ 7:07 am
it’s amazing , l ove it .
Comment by Wholesale jerseys — November 22, 2011 @ 1:47 am
The latest achieve miraculous Fendi repeatedly (Fendi) by 2010,
Wholesale Air Max Shoes qiu dong series welcomed the downy tone: teach enchanted orange,
Louis Vuittion Clothing green, navy blue water duck and bone brown, pieced wine red, blue, gray and black, grey extracted
Comment by Louis Vuittion Clothing — November 25, 2011 @ 1:54 am
Thank you for sharing, this information is useful to me. good quality
http://www.cheapshoes-handbags.us/mens-shoes-c-1361.html?zenid=0208bc2cad5c68bdb9896e95e91aef2a
Comment by Cheap Nike Shoes — November 28, 2011 @ 11:58 am
Thank you for sharing, this information is useful to me. good quality
Comment by Cheap Nike Shoes — December 7, 2011 @ 3:34 am
I can see that you are an expert in this area. I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
Comment by oakley outlet — December 8, 2011 @ 11:39 pm
Thank you for sharing, this information is useful to me. good quality
Comment by Cheap Nike Shoes — December 14, 2011 @ 8:59 am
Thank you for sharing, this information is useful to me. good quality
Comment by Cheap Nike Shoes — December 21, 2011 @ 4:48 am
Pretty! This was a really wonderful post. Thank you for your provided information.
Comment by Cheap Jerseys Wholesale — December 30, 2011 @ 1:25 am
Nice one! Never thought of it. I used to add on delicious, but your idea is more better.
Comment by Cheap Nike Shoes — December 30, 2011 @ 1:26 am
I love the great info, excellent post.
Comment by nfl jerseys — January 16, 2012 @ 10:30 am
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality informations.
Comment by oakley outlet — January 18, 2012 @ 12:22 am