tag:blogger.com,1999:blog-19626531.post813089301026336076..comments2023-11-02T08:32:39.646+01:00Comments on Ola Bini: Programming Language Synchronicity: Ruby Metaprogramming techniquesOla Binihttp://www.blogger.com/profile/15793488672952593953noreply@blogger.comBlogger27125tag:blogger.com,1999:blog-19626531.post-54781068933946612972008-01-02T23:22:00.000+01:002008-01-02T23:22:00.000+01:00Solved my own problem -- I can reproduce it if I u...Solved my own problem -- I can reproduce it if I use "def" (as you wrote), but I was using define_method. It seems the only way to use define_method to dynamically define methods is via the singleton class.Anonymoushttps://www.blogger.com/profile/08160479191349243750noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-19140110573306934942007-12-30T22:06:00.000+01:002007-12-30T22:06:00.000+01:00"If you use String.instance_eval and do a def foo ..."If you use String.instance_eval and do a def foo inside, this will be available as String.foo, but if you do the same thing with module_eval you'll get String.new.foo instead."<BR/><BR/>No matter how hard I try, I can't reproduce this behavior -- i.e. I can't find a way to define a String.foo using instance_eval (or class_eval, for that matter). What am I missing?Anonymoushttps://www.blogger.com/profile/08160479191349243750noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-77501862657972079362007-10-23T18:23:00.000+02:002007-10-23T18:23:00.000+02:00Hi, great post. A small comment for point 4 on met...Hi, great post. A small comment for point 4 on method_missing. The code you used here when use try to call x.foo will return nil.<BR/><BR/>To return :baz (the value in the hash), the code should be:<BR/><BR/>class Hash<BR/> def method_missing(m,*a)<BR/> if m.to_s =~ /=$/<BR/> self[$`] = a[0]<BR/> elsif a.empty?<BR/> <B>self[m.to_s]</B><BR/> else<BR/> raise NoMethodError, "#{m}"<BR/> end<BR/> end<BR/>endAnonymoushttps://www.blogger.com/profile/03175472007729700493noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-25774829239078817832007-05-16T02:07:00.000+02:002007-05-16T02:07:00.000+02:00Thank you very much for this inspiration!Thank you very much for this inspiration!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-21732363444091730652006-10-27T11:49:00.000+02:002006-10-27T11:49:00.000+02:00I meant that the behaviour is switched when changi...I meant that the behaviour is switched when changing self[m] to self[m.to_s], not reversed.Michelehttps://www.blogger.com/profile/04184897818528674940noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-48601440209078399042006-10-27T11:26:00.000+02:002006-10-27T11:26:00.000+02:00The hash method doesn't work for string keys, only...The hash method doesn't work for string keys, only for symbol keys.<br /><br />a = {:name => "arne anka", "drinks" => "beer"}<br /><br />p a.name # -> "arne anka"<br />p a.drinks # -> nil<br /><br />Changing self[m] to self[m.to_s] reverses the behaviour.Michelehttps://www.blogger.com/profile/04184897818528674940noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-81725029453271652502006-10-06T21:59:00.000+02:002006-10-06T21:59:00.000+02:00is there a javsscript equivalent of each of these ...is there a javsscript equivalent of each of these techniques ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-49022462437896968732006-10-04T22:34:00.000+02:002006-10-04T22:34:00.000+02:00Nice!
I actually use a couple of the techniques a...Nice!<br /><br />I actually use a couple of the techniques already, and I can't wait to try the rest.chunterhttps://www.blogger.com/profile/13799849410463537428noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-51685303466074611922006-10-03T18:26:00.000+02:002006-10-03T18:26:00.000+02:00You should also use the extra two arguments to eva...You should also use the extra two arguments to eval calls: file and line number. This will allow you to get useful debug information when something breaks inside your meta-program. see my article on Meta Programming and Stack Traces http://ghouston.blogspot.com/2006/06/ruby-meta-programming-and-stack-traces.htmlMov 0 To 1https://www.blogger.com/profile/10710797803413263120noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-13476513389801937652006-09-23T19:41:00.000+02:002006-09-23T19:41:00.000+02:00Hi,
i submit story to digg
If you like the post, p...Hi,<br />i submit story to digg<br />If you like the post, please add your diggs<br /> Ruby Metaprogramming techniques<br />http://www.digg.com/programming/Top_11_Ruby_Metaprogramming_TechniquesAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-38835324846621874872006-09-23T14:54:00.000+02:002006-09-23T14:54:00.000+02:00theres a small bug in the Hash / method_missing co...theres a small bug in the Hash / method_missing code example, <i>self[m]</i> should be <i>self[m.to_s]</i>.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-8447184039434232502006-09-23T14:14:00.000+02:002006-09-23T14:14:00.000+02:00Inersting post I'm still trying digest the whole s...Inersting post I'm still trying digest the whole singleton class bit. Will probably take a few more reads.<br /><br />I'm a bit rusty on my ruby but I think you might need to do this or the traced methods might not get cleaned up if the inner block does a return or a break.<br /><br />def trace(*mths)<br />...add_tracing(*mths)<br />...begin<br />......yield<br />...ensure<br />......remove_tracing(*mths)<br />...end<br />endmonkeeboihttps://www.blogger.com/profile/18237661934796749186noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-61419995647023720392006-09-23T08:18:00.000+02:002006-09-23T08:18:00.000+02:00If you're going to replace methods, consider captu...If you're going to replace methods, consider <a href="http://split-s.blogspot.com/2006/01/replacing-methods.html">capturing the old method in a closure</a> in order to keep it, rather than aliasing it and risking collision and leaving behind a mess.<br /><br />-BrianAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-216892695926004452006-09-22T18:09:00.000+02:002006-09-22T18:09:00.000+02:00Ola,
Thanks, that's just what I was looking for. ...Ola,<br /><br />Thanks, that's just what I was looking for. This is a really great resource on ruby metaprogramming and I hope you'll continue to discuss this in your blog and to add examples as you go along.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-27117857328183898312006-09-22T17:27:00.000+02:002006-09-22T17:27:00.000+02:00Ola, thanks for taking the time to clear that up.
...Ola, thanks for taking the time to clear that up.<br /><br />I am happy now ;-)Marianohttps://www.blogger.com/profile/04576024486566426295noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-31892123697787061752006-09-22T15:06:00.000+02:002006-09-22T15:06:00.000+02:00Hi,
Yes, that method would work in that case, but...Hi,<br /><br />Yes, that method would work in that case, but notice that you have no syntax safety at all in what you're doing here. If you instead use singleton_class to get at the metaclass, and use define_method on the singleton_class, you would get rid of the class_eval. (You do know that you could send a block to class_eval too?)<br /><br />So, the short answer, the singleton class is necessary to make what we're doing more explicit.Ola Binihttps://www.blogger.com/profile/15793488672952593953noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-73089151487239520962006-09-22T14:54:00.000+02:002006-09-22T14:54:00.000+02:00Btw. Any idea what method to use instead of the ug...Btw. Any idea what method to use instead of the ugly class_eval here?Marianohttps://www.blogger.com/profile/04576024486566426295noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-75282441016668333932006-09-22T14:53:00.000+02:002006-09-22T14:53:00.000+02:00>>Having the ActiveRecord user write
[..] instead ...>>Having the ActiveRecord user write<br />[..] instead of<br />set_table_name "products"<br />makes quite a difference for the readability.<br /><br />Yeah, sure got that. I was more thinking along the lines, why you alias the method in the singleton class of Product, rather than defining it on Product itself.<br /><br />And why would you alias the existing method? It is of no use anymore, is it?<br /><br />Btw. I am not trying to be a smartass, I am just trying to understand it.<br /><br />Here is my line of thinking:<br /><br />module ActiveRecord<br /> class Base<br /> <br /> def self.set_table_name(name)<br /> self.class_eval "def self.table_name; \"#{name}\"; end"<br /> end <br /> end <br />end<br /><br />class Product < ActiveRecord::Base; set_table_name "produce"; end<br /><br />puts Product.table_name # => "produce"<br /><br />The interface for the Product developer doesn't change, s/he can still use set_table_name, but I believe now the class Product has been extended instead of the singleton.<br /><br />And that is my original question.Marianohttps://www.blogger.com/profile/04576024486566426295noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-29909089384855732092006-09-22T14:05:00.000+02:002006-09-22T14:05:00.000+02:00Mariano!
Well, a few reasons. First, I wanted to ...Mariano!<br /><br />Well, a few reasons. First, I wanted to show the technique of DSL's. Having the ActiveRecord user write<br />def self.table_name<br /> "products"<br />end<br />instead of <br />set_table_name "products"<br />makes quite a difference for the readability.<br /><br />The base class doesn't really know about the Product class, though, and in the generic case the singleton class is the only safe place for such a method.Ola Binihttps://www.blogger.com/profile/15793488672952593953noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-46432422871946745142006-09-22T13:33:00.000+02:002006-09-22T13:33:00.000+02:00Hey Ola,
really great stuff.
Still trying t...Hey Ola,<br /><br /> really great stuff. <br /><br /> Still trying to get my head around the metaprogramming stuff and learned a lot from your post.<br /><br /> Regarding 2. why do you use the singleton class? Why can't you just define table_name on Product? Because you don't have a reference to the Product class inside of define_attribute_method?<br /><br />Cheers,<br />MarianoAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-74569224991739302482006-09-21T20:07:00.000+02:002006-09-21T20:07:00.000+02:00Do you sleep? :) Keep up the posts, your blog is o...Do you sleep? :) Keep up the posts, your blog is one of the most visited Ruby/Rails blogs in my collection.Unknownhttps://www.blogger.com/profile/12319925813980496997noreply@blogger.comtag:blogger.com,1999:blog-19626531.post-59286227976580032212006-09-21T19:43:00.000+02:002006-09-21T19:43:00.000+02:00Ola,
This is an interesting and great post. Could...Ola,<br /><br />This is an interesting and great post. Could you elaborate more on the trace example? I can't see how the add_tracing and remove_tracing would actually be implemented even though I know what they *should* do.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-52848395136368681122006-09-21T10:07:00.000+02:002006-09-21T10:07:00.000+02:00I think some Ruby metaprogramming snippets can als...I think some Ruby metaprogramming snippets can also be found here: http://bigbold.com/snippets/ !Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-40598048151938149532006-09-21T06:25:00.000+02:002006-09-21T06:25:00.000+02:00Thanks Ola. I've found some great meta programming...Thanks Ola. I've found some great meta programming nuggets scattered in blogs, mailing lists etc. but seeing a whole bunch of them together is a treat. I hope you'll continue to add to these or elaborate existing ones in future.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-19626531.post-73595558550075033262006-09-20T20:09:00.000+02:002006-09-20T20:09:00.000+02:00I have been thinking much about Metaprogramming la...<i>I have been thinking much about Metaprogramming lately. I have come to the conclusion that I would like to see more examples and explanations of these techniques.</i><br /><br />Have a look at <a href="http://jayfields.blogspot.com/">Jay Fields Thoughts</a>. Great post!Dave Hooverhttps://www.blogger.com/profile/11416140599591449857noreply@blogger.com