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.
@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.
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.
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
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 so perhaps I’m missing something.
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 Radar – O’Reilly).
The same happens with this, the search-string isn’t added to the current
page. Instead it’s shown in a new page.
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 ?
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 Radar – O’Reilly).
The same happens with this, the search-string isn’t added to the current
page. Instead it’s shown in a new page.
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 so perhaps I’m missing something.
something_missing
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