Ruby even / odd element of an array

2,443 views
Skip to first unread message

Thong Kuah

unread,
Aug 28, 2008, 9:38:23 PM8/28/08
to WellR...@googlegroups.com
Hi all,

Here's a ruby question. I've tried my google fu but I can't really find the right term for it. Is there an Array or Enumerable method to do this?

I want to get elements 0, 2, 4 first. and then display elements 1,3

a = [a, b, c, d]  #some array
a.each_with_index do |item, index|
  next if index.odd?
  p item
end

a.each_with_index do |item, index|
  next if index.even?
  p item
end


--
Best regards,
Y. Thong Kuah

Henry Maddocks

unread,
Aug 28, 2008, 9:49:15 PM8/28/08
to WellR...@googlegroups.com

On 29/08/2008, at 1:38 PM, Thong Kuah wrote:

> Hi all,
>
> Here's a ruby question. I've tried my google fu but I can't really
> find the right term for it. Is there an Array or Enumerable method
> to do this?
>
> I want to get elements 0, 2, 4 first. and then display elements 1,3

a.partition {|obj| a.index(obj) % 2 == 0}
=> [["a", "c"], ["b", "d"]]


might not work if obj repeats


Henry Maddocks

unread,
Aug 28, 2008, 9:51:57 PM8/28/08
to WellR...@googlegroups.com

On 29/08/2008, at 1:38 PM, Thong Kuah wrote:

> Hi all,
>
> Here's a ruby question. I've tried my google fu but I can't really
> find the right term for it. Is there an Array or Enumerable method
> to do this?
>
> I want to get elements 0, 2, 4 first. and then display elements 1,3

Righteous hack alert!

a = %w( a b c d)
=> ["a", "b", "c", "d"]
h = Hash[*a]
=> {"a"=>"b", "c"=>"d"}
irb(main):012:0> h.keys
=> ["a", "c"]
irb(main):013:0> h.values
=> ["b", "d"]


Tim Haines

unread,
Aug 28, 2008, 9:53:02 PM8/28/08
to WellR...@googlegroups.com
That's sexy.

Philip Murray

unread,
Aug 28, 2008, 9:53:55 PM8/28/08
to WellR...@googlegroups.com

On 29/08/2008, at 1:38 PM, Thong Kuah wrote:

101 Ways to skin a cat

arr = [ :a, :x, :b, :y, :c, :z ]
odds = []
evens = []

arr.each_with_index { |v,i| (i.odd? ? odds : evens) << v }

Cheers

Phil

Tim Haines

unread,
Aug 28, 2008, 9:54:13 PM8/28/08
to WellR...@googlegroups.com
Only problem is that a hash's keys and values aren't guaranteed to retain their order..

Tim.

On Fri, Aug 29, 2008 at 1:51 PM, Henry Maddocks <henry.m...@gmail.com> wrote:

Philip Murray

unread,
Aug 28, 2008, 10:07:32 PM8/28/08
to WellR...@googlegroups.com
On 29/08/2008, at 1:54 PM, Tim Haines wrote:

Only problem is that a hash's keys and values aren't guaranteed to retain their order..

Tim.


They are in Ruby 1.9! :P

Henry Maddocks

unread,
Aug 28, 2008, 10:10:30 PM8/28/08
to WellR...@googlegroups.com

On 29/08/2008, at 1:54 PM, Tim Haines wrote:

> Only problem is that a hash's keys and values aren't guaranteed to
> retain their order..

Good point. It might until you add something to the hash which you
wouldn't in this case. It's still a hack though. I think Phil's
solution is the best.

Tim Uckun

unread,
Aug 28, 2008, 10:56:24 PM8/28/08
to WellR...@googlegroups.com
On Fri, Aug 29, 2008 at 1:51 PM, Henry Maddocks
<henry.m...@gmail.com> wrote:
>
>


Won't work if there are an odd number of elements in the array.

Thong Kuah

unread,
Aug 29, 2008, 1:34:29 AM8/29/08
to WellR...@googlegroups.com


101 Ways to skin a cat

arr = [ :a, :x, :b, :y, :c, :z ]
odds = []
evens = []

arr.each_with_index { |v,i| (i.odd? ? odds : evens) << v }

Cheers

Phil

That seems to be the best way so far. Monkeypatching array, we'll get

class Array
 def odd_elements
    arr=[]
    self.each_with_index {|v,i| arr << v if i % 2 !=0 }
    arr
 end
end

irb(main):016:0> arr = [ :a, :x, :b, :y, :c, :z ]

=> [:a, :x, :b, :y, :c, :z]
irb(main):017:0> arr.odd_elements
=> [:x, :y, :z]

mic...@diaspora.gen.nz

unread,
Aug 29, 2008, 8:16:50 AM8/29/08
to WellR...@googlegroups.com
>That seems to be the best way so far. Monkeypatching array, we'll get
>
>class Array
> def odd_elements
> arr=[]
> self.each_with_index {|v,i| arr << v if i % 2 !=0 }
> arr
> end
>end

Why bother with the arr variable?

self.zip((1..self.length).to_a).
select {|v,i| i % 2 == 1}.
collect {|v,i| v}

(I think I've been polluted with programming in Haskell recently; I
keep trying to be point-free.)

-- michael

Henry Maddocks

unread,
Aug 29, 2008, 5:43:25 PM8/29/08
to WellR...@googlegroups.com, WellR...@googlegroups.com

See now your changing the requirements. The problem used to be odd AND
even. Even only is much simpler. It's lucky we're all so damn agile.

Henry

PS Haskell, pfffffftttttttt

Reply all
Reply to author
Forward
0 new messages