Array#rotate fails in a simple program

R

RichardOnRails

I'm runing Ruby 1.8.6 over WinXP-Pro/SP3

I'm writing a program to illustrate prime numbers and Ruby programming
for my teenage granddaughter. Unfortunately, I've got a problem:

On line 54, I've got the statement:
$primes.rotate!(group_size)

That results in the error:
Adrienne01.rb:54: undefined method `rotate!' for [2, 3, 5, 7]:Array
(NoMethodError)

However, http://www.ruby-doc.org/core/ defines "ary.rotate!(cnt=1) -->
ary"
Also, "rotate" (without the bang) is defined in Ruby/core and fails in
the same manner.

I've posted the program and its output in http://www.pastie.org/1674687

Is Array#rotate! only defined in a Ruby version later than mine? Or
is there something I'm missing here?

Any guidance offered will be gratefully received.

Best wishes,
Richard
 
J

John Feminella

Is Array#rotate! only defined in a Ruby version later than mine? =C2=A0Or
is there something I'm missing here?

Yes. The ruby-doc.org link is for Ruby 1.9.2. (Also, 1.8.6 is a rather
old version of Ruby, so you might want to consider upgrading anyway
even if you're not impacted by the loss of #rotate!).

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



I'm runing Ruby 1.8.6 over WinXP-Pro/SP3

I'm writing a program to illustrate prime numbers and Ruby programming
for my teenage granddaughter. =C2=A0Unfortunately, I've got a problem:

On line 54, I've got the statement:
$primes.rotate!(group_size)

That results in the error:
Adrienne01.rb:54: undefined method `rotate!' for [2, 3, 5, 7]:Array
(NoMethodError)

However, http://www.ruby-doc.org/core/ defines "ary.rotate!(cnt=3D1) -->
ary"
Also, "rotate" (without the bang) is defined in Ruby/core and fails in
the same manner.

I've posted the program and its output in http://www.pastie.org/1674687

Is Array#rotate! only defined in a Ruby version later than mine? =C2=A0Or
is there something I'm missing here?

Any guidance offered will be gratefully received.

Best wishes,
Richard
 
G

Gunther Diemant

[Note: parts of this message were removed to make it a legal post.]

If upgrading isn't an option you could use each_slice from the enumerator
stdlib.

require 'enumerator'

[2,3,5,7,11,13,17].each_slice(2) do |pair|
puts pair.join(',')
end

#output
2,3
5,7
11,13
17
 
R

RichardOnRails

Is Array#rotate! only defined in a Ruby version later than mine?  Or
is there something I'm missing here?

Yes. The ruby-doc.org link is for Ruby 1.9.2. (Also, 1.8.6 is a rather
old version of Ruby, so you might want to consider upgrading anyway
even if you're not impacted by the loss of #rotate!).

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI:http://www.linkedin.com/in/johnxf
SO:http://stackoverflow.com/users/75170/

I'm runing Ruby 1.8.6 over WinXP-Pro/SP3
I'm writing a program to illustrate prime numbers and Ruby programming
for my teenage granddaughter.  Unfortunately, I've got a problem:
On line 54, I've got the statement:
$primes.rotate!(group_size)
That results in the error:
Adrienne01.rb:54: undefined method `rotate!' for [2, 3, 5, 7]:Array
(NoMethodError)
However,http://www.ruby-doc.org/core/defines "ary.rotate!(cnt=1) -->
ary"
Also, "rotate" (without the bang) is defined in Ruby/core and fails in
the same manner.
I've posted the program and its output inhttp://www.pastie.org/1674687
Is Array#rotate! only defined in a Ruby version later than mine?  Or
is there something I'm missing here?
Any guidance offered will be gratefully received.
Best wishes,
Richard

Thanks John: Point taken! I'm installing 1.9.2, plus Pik for switching
(in a Windows environment) between Ruby versions and Rails versions.

Best wishes,
Richard
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top