cybo
September 14th, 2010, 05:12 AM
i'm trying to learn ruby and ruby on rails from a book called 'Ruby for Rails' it is a good book (at least for me) but below is a piece of code i don't understand:
#!/usr/bin/ruby
class Recipe
attr_accessor :main_ingredient
end
class Cookbook
attr_accessor :title, :author
def initialize
@recipes = []
end
def method_missing(m, *args, &block)
@recipes.send(m, *args, &block)
end
end
recipe_for_stew = Recipe.new
recipe_for_stew.main_ingredient = 'beef'
recipe_for_cake = Recipe.new
cb = Cookbook.new
cb << recipe_for_stew
cb << recipe_for_cake
beef_dishes = cb.find_all {|recipe| recipe.main_ingredient == 'beef'}
puts beef_dishes
i understand that when i call '<<' and 'find_all' methods, method_missing is called from the cookbook. i just don't understand what happens after that.
1) what are m, *args, and &block are set to in each case and why
2) what does <array>.send do with those arguments.
help is greatly appreciated.
#!/usr/bin/ruby
class Recipe
attr_accessor :main_ingredient
end
class Cookbook
attr_accessor :title, :author
def initialize
@recipes = []
end
def method_missing(m, *args, &block)
@recipes.send(m, *args, &block)
end
end
recipe_for_stew = Recipe.new
recipe_for_stew.main_ingredient = 'beef'
recipe_for_cake = Recipe.new
cb = Cookbook.new
cb << recipe_for_stew
cb << recipe_for_cake
beef_dishes = cb.find_all {|recipe| recipe.main_ingredient == 'beef'}
puts beef_dishes
i understand that when i call '<<' and 'find_all' methods, method_missing is called from the cookbook. i just don't understand what happens after that.
1) what are m, *args, and &block are set to in each case and why
2) what does <array>.send do with those arguments.
help is greatly appreciated.