Index regex question


(Shashank Date) #1

Not sure how index function works with regex

a = [“this”,“is”,“his”,“book”]
puts a.index(“is”) # 1
puts a.index(/is/) # nil (Why?)

Any ideas?
Thanks,
–Shanko


(ts) #2

Not sure how index function works with regex

ruby call #==, try

   a = ["this","is","his","book", /is/]
   puts a.index(/is/)

Guy Decoux


(Yukihiro Matsumoto) #3

Hi,

···

In message “index regex question” on 02/06/27, “Shashank Date” ADATE@kc.rr.com writes:

Not sure how index function works with regex

Array#index uses “==” to compare, so the answer is “no”.

						matz.

(Shashank Date) #4

Thanks, matz.

I have worked around the problem using Array#each_with_index.

n = nil
a = [“this”,“is”,“his”,“book”]
a.each_with_index {|r,i|
n = i if (r =~ /^is$/)
break if n
}

And to get the effect of Array # rindex just comment out the break
statement.

Any other suggestions are highly welcome.

– Shanko

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1025154592.403173.3285.nullmailer@picachu.netlab.jp

···

Hi,

In message “index regex question” > on 02/06/27, “Shashank Date” ADATE@kc.rr.com writes:

Not sure how index function works with regex

Array#index uses “==” to compare, so the answer is “no”.

matz.