[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

dynamically calling methods that require a block

Barun Singh

4/29/2009 12:32:00 AM

Is it possible to call methods that require block inputs dynamically
without resorting to "eval"? For example, consider:

a = [1,2,3]
a.detect{|x| x > 2}

I want to know if it's possible to do something like the following (this
doesn't work):

a.send(:detect, {|x| x > 2})

I'm using Ruby 1.8.6

Thanks..
--
Posted via http://www.ruby-....

4 Answers

Joel VanderWerf

4/29/2009 1:07:00 AM

0

Barun Singh wrote:
> Is it possible to call methods that require block inputs dynamically
> without resorting to "eval"? For example, consider:
>
> a = [1,2,3]
> a.detect{|x| x > 2}
>
> I want to know if it's possible to do something like the following (this
> doesn't work):
>
> a.send(:detect, {|x| x > 2})
>
> I'm using Ruby 1.8.6
>
> Thanks..

Just pass the block to the send method as you normally would pass a block:

a.send(:detect, &proc {|x| x > 2})
=> 3
a.send(:detect) {|x| x > 2}
=> 3

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Barun Singh

4/29/2009 1:15:00 AM

0

Ah, right I forgot the ampersand. I needed to be able to define the
block separately from the send call, so I tried this:

cond = Proc.new {|x| x > 2}
a.send(:detect, cond)

but I needed that second line to be
a.send(:detect, &cond)

thanks!


Joel VanderWerf wrote:
> Barun Singh wrote:
>>
>> I'm using Ruby 1.8.6
>>
>> Thanks..
>
> Just pass the block to the send method as you normally would pass a
> block:
>
> a.send(:detect, &proc {|x| x > 2})
> => 3
> a.send(:detect) {|x| x > 2}
> => 3

--
Posted via http://www.ruby-....

plainolamerican

4/4/2014 8:55:00 PM

0

On Monday, March 31, 2014 5:06:58 PM UTC-5, The Peeler wrote:
> On Mon, 31 Mar 2014 14:48:42 -0700 (PDT), plainstupidamerican wrote:
>
>
>
>
>
> >>
>
> >> people who are moved to anti-Semitic acts by events connected to Israel.
>
> >>
>
> >> Oy vey, the goyim are waking up!
>
> >
>
> > The number of anti-Semitic incidents documented in Switzerland's
>
> > French-speaking cantons surged in 2013
>
> > ---
>
> > not an American concern.
>
> >
>
> > next ...
>
>
>
> It's no concern of YOURS, plainstupidamerican! Can you get that into your
>
> plain stupid "mind"? <BG>

It's no concern of YOURS
---
it is when jews are extorting our government for our weapons and tax dollars.

removing jews from our government is difficult ... killing them is easy.

The Peeler

4/4/2014 9:08:00 PM

0

On Fri, 4 Apr 2014 13:54:50 -0700 (PDT), plainstupidamerican wrote:

>>> next ...
>>
>> It's no concern of YOURS, plainstupidamerican! Can you get that into your
>>
>> plain stupid "mind"? <BG>
>
> It's no concern of YOURS
> ---
> it is when jews are extorting our government for our weapons and tax dollars.
>
> removing jews from our government is difficult ... killing them is easy.

It's no concern of YOURS, plainstupidamerican! You better are concerned
about your mental health! <BG>