[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: accessing methods from the blocks caller

eden li

3/18/2007 9:45:00 AM

$ irb
irb(main):001:0> class Test; def method1; end; def method2;
accept_block { method1 }; end; end
=> nil
irb(main):002:0> def accept_block; yield; end
=> nil
irb(main):003:0> Test.new.method2
=> nil

No error... there's something weird going on with your version of
#accept_block. Care to dump the source out here?

On Mar 18, 3:55 pm, "andrew" <non...@noname.com> wrote:
> Taking a better look at my pickaxe book, it seems as though this is simply a
> scoping issue that doesn't really have a solution. I have a workaround, but
> was curious to know if there was a "proper" way to accomplish what I was
> looking for.
>
> Thanks,
> Andrew
>
> "andrew" <non...@noname.com> wrote in message
>
> news:w44Lh.32606$zU1.24952@pd7urf1no...
>
> > Hello,
>
> > Sorry if this has been asked many times before, but I'm having a hard time
> > finding the right keywords to search for this one.
>
> > Here's my question... if I have a method that accepts a block and I want
> > to call a method of the class I'm in, in that block, how do I do it? See
> > the comment below in the example. Should make that sentence a bit
> > clearer. :)
>
> > class Test
> > def method1
> > end
>
> > def method2
> > accept_block do # accept_block is not part of the Test class
> > ...
> > method1 # I want to call method1 from the class I'm in, but
> > I'm getting undefined method for method1
> > ...
> > end
> > end
> > end
>
> > I understand why I would be getting this error, but I don't know how to
> > fix it. Is there a way to access the caller in the block in order to
> > access the method of it? Kernel.caller is the closest I found, but it's
> > not what I want.
>
> > I hope this makes some sense. The example is a bit contrived, but
> > hopefully I get the point across.
>
> > Thanks,
> > Andrew