Where is Enumerable#each_cons()?

Using: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
and: ruby 1.8.4 (2006-02-24) [x86_64-linux]

I can’t find Enumerable#each_cons in either version – always a
NoMethodError:

irb(main):001:0> [1, 2, 3].cons(2) {|a| p a}
=> NoMethodError: undefined method `cons’ for [1, 2, 3]:Array from
(irb):1

irb(main):002:0> (1…10).each_cons(2) {|a| p a}
=> NoMethodError: undefined method `each_cons’ for 1…10:Range from
(irb):2

Am I missing something? (Besides #each_cons, of course.)

Jeremy

Try requiring enumerator.

irb(main):001:0> require “enumerator”
=> true
irb(main):002:0> (1…10).each_cons(2) {|a| p a}
[1, 2]
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8]
[8, 9]
[9, 10]
=> nil

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