I remember when I was in preschool I used to climb on a huge tree in the middle of the playground. My friend David and I would catch caterpillars and then squish them on the steps of the school to see what color they were inside. That tree is barely standing now and I've long since realized that squishing things for mere novelty is a little strange. Another thing I discovered later in life was a sign that was hanging from the surrounding fence stating, "If you can read this you are too big to play here".
After my first pass reading this article on InfoQ I had the an all too familiar feeling as if there was something going on that missing me entirely. I read through it a second time and the uses of eval, class/module_eval and instance_eval are starting to become more apparent. Read the code and pay close attention to the use of eval. It might not be so evil after all.
Showing posts with label meta programming. Show all posts
Showing posts with label meta programming. Show all posts
Wednesday, April 25, 2007
Monday, April 23, 2007
The best explanation of metaclasses in Ruby I've found
I stumbled across "seeingMetaclassesClearly.html " today. I don't know how I never came across it. It's definately the best explanation of metaclasses in Ruby I've ever read. Now the whole "class << obj; def meth; puts "hello from the metaclass"; end; end;" syntax makes sense. Previously I had used "obj.extend(module)" before but the former is obviously more convenient if you are in a class definition.
Monday, January 1, 2007
define_method
I frequently find myself writing the exact same method over and over with only minor differences between them (I'm sure there are plenty of design patterns to get around this in every language). One way of getting around this in ruby without much fuss is 'define_method'. Here's a snippet from Widi:
One thing that did not behave how I expected was Module#const_get. It doesn't handle the ::'s nicely and I had to use an idiom I found in The Ruby Way.
Once that code is run the encompassing class will have two methods called 'generate_header' and 'generate_footer'. This approach would be more justifiable if I needed more than just two methods so I may end up ripping it out.
['header', 'footer'].each do |m|
define_method('generate_' + m) {
@widi_config = WEBrick::Config::Widi
file = @widi_config[m.capitalize.to_sym]
if file =~ /rhtml/ && File.exists?(file)
data = open(file) {|io| io.read}
return ERB.new(data).result(binding)
elsif file.nil?
constant = self.class.to_s + '::DEFAULT_' + m.upcase
return constant.split('::').inject(Object) do |x,y|
x.const_get(y)
end
end
}
end
One thing that did not behave how I expected was Module#const_get. It doesn't handle the ::'s nicely and I had to use an idiom I found in The Ruby Way.
Subscribe to:
Posts (Atom)
Blog Archive
About Me
Tags
- activerecord (1)
- camping (1)
- closures (1)
- cygwin (1)
- denver (1)
- drb (1)
- dsl (1)
- erlang (1)
- erlounge (1)
- fedora (8)
- freedom (1)
- FUDcon (1)
- GEB (3)
- git (1)
- gnu (1)
- gstreamer (1)
- hotmail (1)
- hpricot (1)
- java (1)
- jruby (1)
- kernel (1)
- kino (3)
- laptop (1)
- linux (3)
- machanize (1)
- meta programming (3)
- metaprogramming (1)
- mutt (2)
- open source (3)
- oss (1)
- pitivi (2)
- raleigh.rb (1)
- ruby (13)
- ruby curb hpricot (1)
- rubyconf (1)
- slashdot (1)
- svn (1)
- tux (1)
- ubuntu (1)
- unix (1)
- vim (1)
- vnc (1)
- windows (3)
