Forum: Ruby range index

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2006-12-28 14:55
(Received via mailing list)
is there any dry way to get the index of an element in a range
without converting the range into an array ?

("A".."Z").each do | alpha |
	->  puts alpha.index  (?)  or something  like that .... <-
end
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-12-28 15:21
(Received via mailing list)
Hi --

On Thu, 28 Dec 2006, Josselin wrote:

> is there any dry way to get the index of an element in a range   without
> converting the range into an array ?
>
> ("A".."Z").each do | alpha |
> 	->  puts alpha.index  (?)  or something  like that .... <-
> end

("A".."Z").each_with_index do |alpha,i|
   puts i
end


David
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2006-12-28 16:12
(Received via mailing list)
On 2006-12-28 15:19:43 +0100, dblack@wobblini.net said:

>
> ("A".."Z").each_with_index do |alpha,i|
>    puts i
> end
>
>
> David

thanks a lot.. another part of my book to read before year end ;-)))
all 'each_ ....   ' statements

joss
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-12-28 19:35
(Received via mailing list)
On 28.12.2006 16:05, Josselin wrote:
>>>     ->  puts alpha.index  (?)  or something  like that .... <-
> all 'each_ ....   ' statements
>

Here's another one

irb(main):005:0> require 'enumerator'
=> true
irb(main):010:0> (1..10).to_enum(:each_with_index).find {|a,b| a==2}[1]
=> 1

	robert
This topic is locked and can not be replied to.