Robert Klemme
3/15/2006 2:21:00 PM
"Tod McIntyre" <todmcintyre@gmail.com> wrote in message
news:da9c35364b80a5c4e9e603e0db41190a@ruby-forum.com...
> This is quite embarassing because I Don't consider myself a poor
> programmer, but I just started looking at ruby the other day.
>
> Blocks seem to be a good way to iterate through something to search for
> an entry for instance. It's easy to return the object that meets
> certain criteria using blocks but I'm finding it hard to return the
> index of that particular object in, say, an array.
>
> as an example
>
> arr=[4,1,3,7]
> arr.find { |f| f==someval }
> will return the val within arr that matches someval, which doesn't
> really help me because I already have someval
Yeah, but this is just a special case. With a block you can employ
arbitrary selection criteria - not just ==.
> How do I actually return the index in arr to the first match of someval.
>
> so if someval == 3, my block would return 2, for the above example
As Ross and Daniel have pointed out already, use #index.
Kind regards
robert