tags:

views:

2729

answers:

2

I am working through a book which gives examples of Ranges being converted to equivalent arrays using their "to_a" methods

When i run the code in irb I get the following warning

 warning: default `to_a' will be obsolete

What is the the correct alternative to using to_a?

are there alternate ways to populate an array with a Range?

Cheers

+20  A: 

This works for me in irb:

irb> (1..4).to_a
=> [1, 2, 3, 4]

I notice that:

irb> 1..4.to_a
(irb):1: warning: default `to_a' will be obsolete
ArgumentError: bad value for range
        from (irb):1

So perhaps you are missing the parentheses?

(I am running Ruby 1.8.6 patchlevel 114)

Daniel Lucraft
+12  A: 

Sounds like you're doing this:

0..10.to_a

The warning is from Fixnum::to_a, not from Range::to_a. Try this instead:

(0..10).to_a
Richard Turner