[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I catch a missing method on a passed block?

J2M

12/4/2006 11:05:00 PM

I would like to invoke method_missing on baz in this code;

x = Foo.new
x.bar { baz }

I tried the following, and although the method is added to the proc
objects singleton class, the method_missing method never gets called.
Is there any way to achieve this? My current code (that doesn't work)
is below;

class Foo

def bar(*values, &block)

if block_given?

class << block
def method_missing(m, *args, &block)
puts "missing #{m}"
end
end

block.call

end
end
end


36 Answers

Ara.T.Howard

12/5/2006

0

Logan Capaldo

12/5/2006 12:13:00 AM

0

On Tue, Dec 05, 2006 at 08:04:33AM +0900, J2M wrote:
> I would like to invoke method_missing on baz in this code;
>
> x = Foo.new
> x.bar { baz }
>
> I tried the following, and although the method is added to the proc
> objects singleton class, the method_missing method never gets called.
> Is there any way to achieve this? My current code (that doesn't work)
> is below;
>
You'd want to define the method_missing
<--- here
x.bar { baz }

Obviously that's not always practical, so what you need to do is change
self. That can be acheived by using #instance_eval. This has
disadvantages, obviously, but that's how to do it if you want to do it.

Or you can pass a parameter into the block:
x.bar { |q| q.baz }
> class Foo
>
> def bar(*values, &block)
>
> if block_given?
>
> class << block
> def method_missing(m, *args, &block)
> puts "missing #{m}"
> end
> end
>
> block.call
>
> end
> end
> end
>

J2M

12/5/2006 2:43:00 PM

0


ara.t.howard@noaa.gov wrote:

> but block __will__ respond_to? 'call' - ergo your method_missing hook will
> never fire? what are you trying to do?

I want to catch a missing method (baz) inside the passed block.

Thanks,
James


J2M

12/5/2006 2:47:00 PM

0


Logan Capaldo wrote:

> Or you can pass a parameter into the block:
> x.bar { |q| q.baz }

Thanks Logan, that is what I ended up doing, I just wanted to avoid
having to pass the parameter if it was at all possible. I am writing a
dsl so wanted it to be as clean as possible e.g.

define_stuff do
this :option => "something"
that :option => "something else"
the_other :option => "and something else"
end

Thanks,
James


Daniel Schierbeck

12/5/2006 2:54:00 PM

0

On Tue, 2006-12-05 at 08:04 +0900, J2M wrote:
> I would like to invoke method_missing on baz in this code;
>
> x = Foo.new
> x.bar { baz }
>
> I tried the following, and although the method is added to the proc
> objects singleton class, the method_missing method never gets called.
> Is there any way to achieve this? My current code (that doesn't work)
> is below;
>
> class Foo
>
> def bar(*values, &block)
>
> if block_given?
>
> class << block
> def method_missing(m, *args, &block)
> puts "missing #{m}"
> end
> end
>
> block.call
>
> end
> end
> end

class Foo
def bar
yield
rescue NoMethodError => error
# mojo goes here
end
end


Cheers,
Daniel


Logan Capaldo

12/5/2006 3:07:00 PM

0

On Tue, Dec 05, 2006 at 11:54:06PM +0900, Daniel Schierbeck wrote:
> On Tue, 2006-12-05 at 08:04 +0900, J2M wrote:
> > I would like to invoke method_missing on baz in this code;
> >
> > x = Foo.new
> > x.bar { baz }
> >
> > I tried the following, and although the method is added to the proc
> > objects singleton class, the method_missing method never gets called.
> > Is there any way to achieve this? My current code (that doesn't work)
> > is below;
> >
> > class Foo
> >
> > def bar(*values, &block)
> >
> > if block_given?
> >
> > class << block
> > def method_missing(m, *args, &block)
> > puts "missing #{m}"
> > end
> > end
> >
> > block.call
> >
> > end
> > end
> > end
>
> class Foo
> def bar
> yield
> rescue NoMethodError => error
> # mojo goes here
> end
> end
>
If ruby were common lisp, that could actually work. But it's not and our
exceptions are not resuamble :).
>
> Cheers,
> Daniel
>

Ara.T.Howard

12/5/2006 5:33:00 PM

0

J2M

12/5/2006 11:41:00 PM

0

-a ! Thanks, I will give this a shot. I really appreciate your help.
James


Tim Fletcher

12/6/2006 8:53:00 AM

0

Logan Capaldo wrote:
> On Tue, Dec 05, 2006 at 11:54:06PM +0900, Daniel Schierbeck wrote:
> >
> > class Foo
> > def bar
> > yield
> > rescue NoMethodError => error
> > # mojo goes here
> > end
> > end
> >
> If ruby were common lisp, that could actually work. But it's not and our
> exceptions are not resuamble :).

class Foo
def bar
yield
rescue NameError => method_call
p method_call.name
end
end

Foo.new.bar { baz }

Martin DeMello

12/6/2006 12:03:00 PM

0

On 12/6/06, Tim Fletcher <twoggle@gmail.com> wrote:
> Logan Capaldo wrote:
> > >
> > If ruby were common lisp, that could actually work. But it's not and our
> > exceptions are not resuamble :).
>
> class Foo
> def bar
> yield
> rescue NameError => method_call
> p method_call.name
> end
> end
>
> Foo.new.bar { baz }

Breaks:

irb(main):008:0> a = Foo.new
=> #<Foo:0x2b301a826778>
irb(main):009:0> a.bar { baz }
:baz
=> nil
irb(main):010:0> a.bar { baz; quux }
:baz
=> nil

martin