This is my first email that I send to this ML, so hi everyone
I've been using Ruby for 2 years or more now, and I am still learning
this awesome language. But I can't grasp my head about method_missing
and how it works, can someone please help me understand about how
method_missing works?
On Fri, Sep 17, 2010 at 4:48 PM, Diego Viola <diego.viola@gmail.com> wrote:
this awesome language. But I can't grasp my head about method_missing
and how it works, can someone please help me understand about how
method_missing works?
I've been using Ruby for 2 years or more now, and I am still learning
this awesome language. But I can't grasp my head about method_missing
and how it works, can someone please help me understand about how
method_missing works?
class Foo
def bar(*args)
puts "You called bar with #{args.inspect}"
end
def method_missing(*args)
puts "method_missing with #{args.inspect}"
end
end
Nice, so method_missing is called when I call a method that I didn't
defined myself?
Thanks,
Diego
路路路
On Fri, Sep 17, 2010 at 6:37 AM, Brian Candler <b.candler@pobox.com> wrote:
Diego Viola wrote:
I've been using Ruby for 2 years or more now, and I am still learning
this awesome language. But I can't grasp my head about method_missing
and how it works, can someone please help me understand about how
method_missing works?
class Foo
def bar(*args)
puts "You called bar with #{args.inspect}"
end
def method_missing(*args)
puts "method_missing with #{args.inspect}"
end
end
method_missing is a great tool for dynamically adding/handling methods
which aren't currently part of the object.
I was just reading up on this in the free Ruby Best Practices book
(check out ch. 3): http://rubybestpractices.com
路路路
On Fri, 2010-09-17 at 12:20 -0500, Rick DeNatale wrote:
On Fri, Sep 17, 2010 at 11:55 AM, Diego Viola <diego.viola@gmail.com> wrote:
> Nice, so method_missing is called when I call a method that I didn't
> defined myself?
>
> Thanks,
It's called when a message is sent to an object which doesn't have a
method. If it does it doesn't matter who defined it.
but why do i get a: asd.rb:6:in `method_missing': wrong number of
arguments (1 for 0) (ArgumentError) -- when i try to call
method_missing like that?
if i use method_missing(*args) it works, does method_missing requires
that i use an argument?
Thanks
路路路
On Fri, Sep 17, 2010 at 2:16 PM, Alex Stahl <astahl@hi5.com> wrote:
method_missing is a great tool for dynamically adding/handling methods
which aren't currently part of the object.
I was just reading up on this in the free Ruby Best Practices book
(check out ch. 3): http://rubybestpractices.com
On Fri, 2010-09-17 at 12:20 -0500, Rick DeNatale wrote:
On Fri, Sep 17, 2010 at 11:55 AM, Diego Viola <diego.viola@gmail.com> wrote:
> Nice, so method_missing is called when I call a method that I didn't
> defined myself?
>
> Thanks,
It's called when a message is sent to an object which doesn't have a
method. If it does it doesn't matter who defined it.
but why do i get a: asd.rb:6:in `method_missing': wrong number of
arguments (1 for 0) (ArgumentError) -- when i try to call
method_missing like that?
if i use method_missing(*args) it works, does method_missing requires
that i use an argument?
Run the code I first posted again, and look at the output carefully.
method_missing is passed the name of the method which didn't exist as
the first argument, followed by 0 or more values which were the
arguments to the original call. So you must define it to receive at
least one argument.
A good template to use:
def method_missing(method_name, *args, &block)
...
end