Method_missing from Ruby for Rails book

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.

Stuart

Hi –

On Sun, 30 Jul 2006, Dark A. wrote:

@recipes.send(m, *args, &block)
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.

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.

Hi –

On Sun, 30 Jul 2006, [email protected] wrote:

@recipes = []
beef_dishes = cb.find_all {|recipes| recipe.main_ingredient == “beef” }
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 :slight_smile:

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.

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” }

David,
I’m lost now . The variable as recipes should be fine. recipe is
undefined but I thought the purpose of the code sample was to show how
method_missing still responds to the call. ?
It’s not an easy chapter :slight_smile: so perhaps I’m missing something.

Stuart

Hi,
I’m new to ruby and trying to figure out how to get form_remote_tag
working.
I tried two examples. But I’m always ending up with the same behavior.

I want to code a ajax-style search. When the search button is pressed
the matching rows should be added to the current page. But instead they
are shown in a new page.

Here follows some more basic code. I’m tried to add the search-string to
the current page. (Just like the example on
http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html?page=2).
The same happens with this, the search-string isn’t added to the current
page. Instead it’s shown in a new page.

<%= form_remote_tag(:update =>“search”,
:url => {:action => :search}) %>





              <td> <%= submit_tag ("Search now") %></td>
           </tr>
      </tbody>
</table>

<%= end_form_tag %>

def search
render_text “

  • ” + params[:search_text] + “

  • end

    regards
    tim

    Search for: <%= text_field_tag :search_text %>

    Hi –

    On Sun, 30 Jul 2006, Dark A. wrote:

    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 ?

    No; what you’ve got is right, except:

    beef_dishes = cb.find_all {|recipes| recipe.main_ingredient ==
    “beef” }

    Your loop variable is recipes but you’re using recipe (which is
    undefined) inside the loop.

    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.

    Hi,
    I’m new to ruby and trying to figure out how to get form_remote_tag
    working.
    I tried two examples. But I’m always ending up with the same behavior.

    I want to code a ajax-style search. When the search button is pressed
    the matching rows should be added to the current page. But instead they
    are shown in a new page.

    Here follows some more basic code. I tried to add the search-string to
    the current page. (Just like the example on
    http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html?page=2).
    The same happens with this, the search-string isn’t added to the current
    page. Instead it’s shown in a new page.

    <%= form_remote_tag(:update =>“search”,
    :url => {:action => :search}) %>





                  <td> <%= submit_tag ("Search now") %></td>
               </tr>
          </tbody>
    </table>
    

    <%= end_form_tag %>

    def search
    render_text “

  • ” + params[:search_text] + “

  • end

    regards
    tim


    Rails mailing list
    [email protected]
    http://lists.rubyonrails.org/mailman/listinfo/rails

    Search for: <%= text_field_tag :search_text %>

    Hi –

    On Sun, 30 Jul 2006, Dark A. wrote:

    the
    David,
    I’m lost now . The variable as recipes should be fine. recipe is
    undefined but I thought the purpose of the code sample was to show how
    method_missing still responds to the call. ?
    It’s not an easy chapter :slight_smile: so perhaps I’m missing something.

    something_missing :slight_smile:

    The thing to remember is that you define method_missing for a
    particular class. So if you do this:

    class Cookbook
    def method_missing(m,*args,&block)

    end
    end

    that doesn’t mean that every unknown identifier anywhere in your
    program will be processed by that method. It means that if you send a
    message to a Cookbook object, and the object doesn’t understand the
    message, it will send it along to its method_missing.

    The ‘recipe’ identifier in the block has nothing to do with any
    Cookbook object. It’s just a misspelled variable name :slight_smile:

    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.

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs