-
Website
http://drawohara.com/ -
Original page
http://drawohara.com/post/48758159 -
Subscribe
All Comments -
Community
-
Top Commenters
-
adityagholap
1 comment · 1 points
-
avdi
2 comments · 4 points
-
somethingsomething
1 comment · 1 points
-
Michael Guterl
14 comments · 1 points
-
bbebop
1 comment · 3 points
-
-
Popular Threads
-
drawohara - Ski tu-tu. You?
2 weeks ago · 2 comments
-
drawohara - Lancelot.
1 week ago · 1 comment
-
drawohara - Ski tu-tu. You?
Thanks for all that you contribute to the ruby community!
or simpler, how to become a better rubyist.
Just two remarks:
By using an instance variable in Methods.for it looks like you wanted to create the anonymous module only once, but you create a new one every time (actually two modules, but that's only a typo I suppose). If you want to create the module only once, you could change the code as follows:
<pre>
def Methods.new
Module.new do
def self.remove!
instance_methods.each{|m| remove_method m}
end
end
end
def Methods.for object
singleton_class_for(object) do
unless defined? @dsl_methods
@dsl_methods = Methods.new
include @dsl_methods
end
@dsl_methods.module_eval(&Methods)
@dsl_methods
end
end
</pre>
The second remark: you don't need instance_eval and the block parameter in the #dsl method, because you already call it in the scope of self. So all you need is a yield.
Regards,
Pit
re
1 - great fix. it also fixes an issue this code had - that of forever littering the singleton class with module methods.
2 - yup. good call.
i'm very happy with the approach thus far though - it's vastly simply than any approach i've found thus far for rapidly building intuitive and dsls.
http://s3.amazonaws.com/drawohara.com.snippets/...