From: Daniel Brockman Date: 2005-07-13T02:11:13+09:00 Subject: Re: Question: arrays v. ranges and *.each "David Douthitt" writes: > The results of the following code just seem strange to me: > > print "\nTry 1:" > print [1..10].class, "\n" > [1..10].each { |x| print x, "\n" } > > print "\nTry 2:" > print [1..10].pop.class, "\n" > [1..10].pop.each { |x| print x, "\n" } > > I would have thought that either 1) try 1 would output a sequence of > numbers, and try 2 would result in an error (no such method > Integer#each); or, 2) try 1 would output a series of numbers and try 2 > would result in an error (no such method Range#pop). Perhaps - another > option would be 3) try 1 would result in '1..10' and try 2 would result > in something like '1..1' or an error? > > I'm confused... Well, `a..b' is a range and `[foo]' is an array of one element, so rather naturally `[a..b]' is an array containing one element: a range. You can get an array from a range by doing `(a..b).to_a'. print "\nTry 2:" print (1..10).to_a.pop.class, "\n" (1..10).to_a.pop.each { |x| print x, "\n" } Of course, ranges are already enumerable, so this works OK: print "\nTry 1:" print (1..10).class, "\n" (1..10).each { |x| print x, "\n" } -- Daniel Brockman So really, we all have to ask ourselves: Am I waiting for RMS to do this? --TTN.