From: Paul Harrington on
David Springer wrote:
> after some inspiration from Luc I was able to come up with this:
>
> textlist = ["Apple", "Orange", "Lemon", "Grape", "Orange",
>
> (0..textlist.length-1).select {|i| textlist[i] == "Orange"}[1]

Second day with Ruby, huh...
--
Posted via http://www.ruby-forum.com/.

From: Marc Heiler on
> Second day with Ruby, huh...

Ruby simplifies thinking.
--
Posted via http://www.ruby-forum.com/.

From: Sven Schott on
[Note: parts of this message were removed to make it a legal post.]

I like Luc's but ever since I got hit with inject:

class Array
def indices_of(obj)
self.inject([]) { |arr, element| arr << element if element == obj; arr
}
end
end

And I like indices not because I'm language nazi but because of personal
preference. :)

On Fri, Feb 26, 2010 at 10:35 AM, Marc Heiler <shevegen(a)linuxmail.org>wrote:

> > Second day with Ruby, huh...
>
> Ruby simplifies thinking.
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Giampiero Zanchi on
in order to simplify ...
(0...textlist.length).select {|i| textlist[i] == "Orange"}[1]

David Springer wrote:
> (0..textlist.length-1).select {|i| textlist[i] == "Orange"}[1]

--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
2010/2/26 Giampiero Zanchi <cidza(a)tin.it>:
> in order to simplify ...
> (0...textlist.length).select {|i| textlist[i] == "Orange"}[1]
>
> David Springer wrote:
>> (0..textlist.length-1).select {|i| textlist[i] == "Orange"}[1]

Interesting approach. That could also be done with

irb(main):005:0> textlist.size.times.select {|i| textlist[i] == "Orange"}
=> [1, 4, 6]

I have

irb(main):001:0> textlist = ["Apple", "Orange", "Lemon", "Grape",
"Orange", "Melon",
irb(main):002:1* "Orange", "Banana"]
=> ["Apple", "Orange", "Lemon", "Grape", "Orange", "Melon", "Orange", "Banana"]
irb(main):003:0> textlist.each_with_index.partition {|a,i| a ==
"Orange"}.first.map {|a,i| i}
=> [1, 4, 6]

or, even better

irb(main):006:0> textlist.each_with_index.select {|a,i| a ==
"Orange"}.map {|a,i| i}
=> [1, 4, 6]

Hmmm, we could also do

irb(main):007:0> textlist.each_with_index.select {|a,i| a ==
"Orange"}.map(&:last)
=> [1, 4, 6]

This is all very 1.9ish though. ;-)

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/