range index

J

Josselin

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
 
D

dblack

Hi --

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

Josselin

Hi --



("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
 
R

Robert Klemme

thanks a lot.. another part of my book to read before year end ;-)))
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top