[Rails] Method_missing from Ruby for Rails book

Dark Ambient sambient at gmail.com
Sun Jul 30 23:44:18 GMT 2006


David,

I had added the class Recipe and still received same error.
Furthermore, the recipe_for_cake and chicken, are you saying those replace the
cb << recipe_for cake ?
Anyway still no luck here.

Just to show what I have now in code:

class Recipe
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_cake = Recipe.new
recipe_for_chicken = Recipe.new

cb = Cookbook.new
cb << recipe_for_cake
cb << recipe_for_chicken
beef_dishes = cb.find_all {|recipes| recipe.main_ingredient ==
"beef" }
-----------------------------------------------------------------------------

On 7/30/06, dblack at wobblini.net <dblack at wobblini.net> wrote:
> Hi --
>
> On Sun, 30 Jul 2006, dblack at wobblini.net wrote:
>
> > Hi --
> >
> > On Sun, 30 Jul 2006, Dark Ambient wrote:
> >
> >> I'm having a problem getting this example from the book to work:
> >>
> >> class Cookbook
> >> attr_accessor :title, :author
> >>
> >> def initialize
> >>   @recipes = []
> >> end
> >>
> >> def method_missing(m, *args, &block)
> >>   @recipes.send(m, *args, &block)
> >> end
> >> end
> >>
> >> cb = Cookbook.new
> >> cb << recipe_for_cake
> >> cb << recipe_for_chicken
> >> beef_dishes = cb.find_all {|recipes| recipe.main_ingredient == "beef" }
> >>
> >> I'm getting callable.rb:14: undefined local variable or method
> >> `recipe_for_cake' for main:Object (NameError) so apparently
> >> method_missing is not working.
> >
> > method_missing is working -- it's just not being called where you
> > think it is.
> >
> > cb << recipe_for_cake is syntactic sugar for this:
> >
> >  cb.<<(recipe_for_cake)
> >
> > The "recipe_for_cake" expression is sitting at the top level of the
> > program, where there's no method_missing defined.  So Ruby doesn't
> > know what you mean by it.
>
> Good grief -- I'm a space cadet.  I didn't even notice this was from
> my book, even though you said so in the subject line :-)
>
> So let me add to my answer:
>
> Just before the example, it says:
>
>    Let's assume there's a Recipe class, separate from
>    the Cookbook class, and we've already created some recipe
>    objects.
>
> So the idea is that recipe_for_cake and recipe_for_chicken are
> instances of Recipe.
>
> To run the example, add this to the beginning:
>
>    class Recipe
>    end
>
>    recipe_for_cake = Recipe.new
>    recipe_for_chicken = Recipe.new
>
> or just use Object.new and don't even bother with the Recipe class --
> the main thing is to initialize those variables.
>
>
> David
>
> --
> http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
>    ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
> http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
> http://www.manning.com/black     => book, Ruby for Rails
> http://www.rubycentral.org       => Ruby Central, Inc.
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


More information about the Rails mailing list