Tuesday, February 17, 2009

Using define_method to create a method that takes arguments

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/211436
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/211440

Example:

Setting.all.each do |setting|
define_method setting.name do |*a|
r = find(:first, :include => :setting, :conditions => ["setting_id = ?", setting.id])
full_record = (a.length == 0) ? false : a[0]
full_record ? r : r.value
end
end

No comments: