Prev: Error on iterating through a set of nested tables in a form
Next: Making a VALUE visible to multiple source files
From: Paul Harrington on 25 Feb 2010 17:51 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 25 Feb 2010 18:35 > Second day with Ruby, huh... Ruby simplifies thinking. -- Posted via http://www.ruby-forum.com/.
From: Sven Schott on 25 Feb 2010 19:23 [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 26 Feb 2010 06:04 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 26 Feb 2010 09:07 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/
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Error on iterating through a set of nested tables in a form Next: Making a VALUE visible to multiple source files |