in_groups_of undefined method

M

MeX23

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
 
J

Joel VanderWerf

MeX23 said:
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

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]
 
R

Rick DeNatale

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top