On Fri, Apr 24, 2009 at 11:22 AM, Loga Ganesan
<loganathan_gpt / yahoo.co.in> wrote:
> lasitha wrote:
>> On Fri, Apr 24, 2009 at 9:38 AM, Rob Redmon <rob.webinator / gmail.com>
>> wrote:
>>> How do I get the matched indices? That is, without ugly loops.
>> #index and #rindex in their block forms come to mind:
>> http://ruby-doc.org/core-1.9/classes/Array.html#M001974
>>
>> cheers,
>> lasitha
>
> In the URL, they specified the following:
>   a.rindex{|x|x=="b"}
>
> But the rindex method doesn't provide the syntax of passing the blocks.
> I had
> tried the following and it thrown the error.
>
> irb(main):012:0> a=[10,20,30,40,50]
> => [10, 20, 30, 40, 50]
> irb(main):013:0> a.rindex{|x| x>20}
> ArgumentError: wrong number of arguments (0 for 1)
>    from (irb):13:in `rindex'
>    from (irb):13
>    from (null):0

Hmm, the doco was for 1.9, but it works for 1.8.7 too:

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e == 2 }'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin8]
1

$: ruby -ve 'puts [1, 2, 3].rindex {|e| e == 2 }'
ruby 1.9.1p0 (2009-01-30) [i386-darwin8.11.1]
1

I don't have 1.8.6 to test on this box, i'm afraid.

solidarity,
lasitha.