From: James Edward Gray II Date: 2005-07-13T02:10:01+09:00 Subject: Re: Question: arrays v. ranges and *.each On Jul 12, 2005, at 11:50 AM, David Douthitt wrote: > The results of the following code just seem strange to me: > > print "\nTry 1:" > print [1..10].class, "\n" [ ... ] builds an Array. 1..10 builds a Range. So Ruby sees this as an Array with a single member, a Range. As an aside, you should look into puts(): puts [1..10].class # no newline needed! > [1..10].each { |x| print x, "\n" } So here we are printing the one member, the Range. > print "\nTry 2:" > print [1..10].pop.class, "\n" Here we remove the Array's one member (again a Range) and show its class. > [1..10].pop.each { |x| print x, "\n" } Again, printing the one popped member. Now all of that discusses what you showed, not what you meant. Let's talk about that. It seems to me you are trying to treat a Range as an Array. The first thing to know there is that Ranges are already Enumerable: irb(main):006:0> (1..10).each { |n| puts n } 1 2 3 4 5 6 7 8 9 10 => 1..10 The other fact that may be of use to you is that we can turn a Range into an Array, as needed: irb(main):007:0> ("a".."j").to_a => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] Hope that clears some things up. James Edward Gray II