In_groups_of undefined method

Hi guys, I’m newbie of ruby and I tried to use the method
in_groups_of(x, false)

in irb i wrote:

irb(main):001:0> a = (1…12).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
irb(main):002:0> a.in_groups_of(4)
NoMethodError: undefined method `in_groups_of’ for [1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12]:Array
from (irb):2

Can someone explain me what is wrong? thanks

MeX23 wrote:

    from (irb):2

Can someone explain me what is wrong? thanks

require ‘enumerator’

a.enum_for(:each_slice,4).to_a
=> [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

a.each_slice(4) {|b| p b}
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

a.each_cons(4) {|b| p b}
[1, 2, 3, 4]
[2, 3, 4, 5]
[3, 4, 5, 6]
[4, 5, 6, 7]
[5, 6, 7, 8]
[6, 7, 8, 9]
[7, 8, 9, 10]
[8, 9, 10, 11]
[9, 10, 11, 12]

Thanks a lot to both of you

On 5/17/07, MeX23 [email protected] wrote:

    from (irb):2

Can someone explain me what is wrong? thanks

Array#in_groups_of is an extension from Rails, it’s not part of standard
Ruby.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs