[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question: arrays v. ranges and *.each

David Douthitt

7/12/2005 4:49:00 PM

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...

8 Answers

threeve.org

7/12/2005 5:01:00 PM

0

On 7/12/05, David Douthitt <ssrat@mailbag.com> wrote:
> 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...

the .. operator does not output a series of numbers, but rather an
instance of the class Range.

Try 1 is an array with a single element, a Range object. Calling each
on the array yields each element, in this case the single Range
object, and you've output it to the console, which gives "1..10" which
is how the Range responds to to_s.

Try 2 is the same array. You pop the first element, which is the
Range object, and call each, which iterates over the numbers
represented by the range.

Try this:
(1..10).entries

That gives you an array of all the elements represented by the Range.


Jason


Shashank Date

7/12/2005 5:06:00 PM

0

Hi David,

--- David Douthitt <ssrat@mailbag.com> wrote:

> 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" }

You are mixing two things here: a range and an array.
[1..10] is an array of one object (1..10) which is an
instance of the Range class.

> print "\nTry 2:"
> print [1..10].pop.class, "\n"
> [1..10].pop.each { |x| print x, "\n" }

so [1..10].pop gives (1..10).

And Range itself is Enumerable.

so: (1..10).each {|i| p i} is a valid construct.


> 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...

HTH,
-- shanko



____________________________________________________
Sell on Yahoo! Auctions ? no fees. Bid on great items.
http://auctions....


James Gray

7/12/2005 5:10:00 PM

0

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



Daniel Brockman

7/12/2005 5:11:00 PM

0

David Douthitt

7/12/2005 5:18:00 PM

0

Shashank Date wrote:

> You are mixing two things here: a range and an array.
> [1..10] is an array of one object (1..10) which is an
> instance of the Range class.

Aha! That explains it all... So then:

puts ([1..10] == 1..10 ? "true" : "false")

returns "false" - a complicated way to say 1..10 and [1..10] are not
the same thing :-)

So, in my original programming, what I wanted was not "[1..10]" but
"(1..10)". It all makes sense now.

Thanks all!

James Gray

7/12/2005 6:02:00 PM

0

On Jul 12, 2005, at 12:20 PM, David Douthitt wrote:

> puts ([1..10] == 1..10 ? "true" : "false")

puts [1..10] == (1..10)

;)

James Edward Gray II



Joe Van Dyk

7/12/2005 9:29:00 PM

0

On 7/12/05, David Douthitt <ssrat@mailbag.com> wrote:
> Shashank Date wrote:
>
> > You are mixing two things here: a range and an array.
> > [1..10] is an array of one object (1..10) which is an
> > instance of the Range class.
>
> Aha! That explains it all... So then:
>
> puts ([1..10] == 1..10 ? "true" : "false")
>
> returns "false" - a complicated way to say 1..10 and [1..10] are not
> the same thing :-)
>
> So, in my original programming, what I wanted was not "[1..10]" but
> "(1..10)". It all makes sense now.

Don't feel bad... this tripped me up as well too.


Naked Gonad

11/24/2008 9:10:00 AM

0

Eli Grubman wrote:
> On Tue, 18 Nov 2008 09:38:35 +0000, Naked Gonad
> <bodron57@tiscali.co.uk> wrote:
>
>> Eli Grubman wrote:
>>> On Mon, 17 Nov 2008 18:41:53 +0000, Naked Gonad
>>> <bodron57@tiscali.co.uk> wrote:
>>>
>>>> Eli Grubman wrote:
>>>>> On Mon, 17 Nov 2008 17:13:19 +0000, Naked Gonad
>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>
>>>>>> Eli Grubman wrote:
>>>>>>> On Mon, 17 Nov 2008 16:17:35 +0000, Naked Gonad
>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>
>>>>>>>> Eli Grubman wrote:
>>>>>>>>> On Mon, 17 Nov 2008 15:23:24 +0000, Naked Gonad
>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>
>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>> On Mon, 17 Nov 2008 14:29:48 +0000, Naked Gonad
>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>> On Mon, 17 Nov 2008 13:47:33 +0000, Naked Gonad
>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>> On Mon, 17 Nov 2008 11:58:03 +0000, Naked Gonad
>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>> On Mon, 17 Nov 2008 11:33:50 +0000, Naked Gonad
>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>> On Mon, 17 Nov 2008 09:41:48 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 19:15:35 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 17:51:29 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 17:03:49 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 16:30:38 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 16:00:51 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 14:23:43 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 12:55:54 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 16 Nov 2008 08:45:08 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 15 Nov 2008 18:32:19 -0000, "Peter Hucker" <none@spam.com>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 09 Nov 2008 11:24:04 -0000, Eli Grubman <eli.grubman@googlemail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sun, 09 Nov 2008 10:22:42 +0000, Naked Gonad
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 08 Nov 2008 17:58:38 -0000, "Peter Hucker" <none@spam.com>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 08 Nov 2008 17:41:18 -0000, Eli Grubman <eli.grubman@googlemail.com> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Sat, 08 Nov 2008 17:28:59 -0000, "Peter Hucker" <none@spam.com>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> On Fri, 31 Oct 2008 11:22:52 -0000, Naked Gonad <bodron57@tiscali.co.uk> wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Eli Grubman wrote:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Could even be the first number three!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Is that a wank?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> No, it's what's euphemistically known as a "loose movement".
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> A follow through?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I would suggest just flushing it.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Colonic?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yes, but easily rectified.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Not sure a bridge rectifier would help much with an arse.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Suggest using an ass rectifier in this case.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Preferably a 'whetstone' as opposed to a 'drystone'...ha!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> They still make whetstone walls?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yup, guaranteed to leek.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Ah, look you, Boyo!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Now there's lovely, isn't it!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Llchgrwyffychdd!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I never touched your sheep!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Gwddyfyddch!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>> Not without protection I won't!
>>>>>>>>>>>>>>>>>>>>>>>>>>> Cwmrych!
>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>> Filthy fucker!
>>>>>>>>>>>>>>>>>>>>>>>>> Dwynedd y pwllych!
>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>> Impossible! I couldn't possibly stick my foot up my arse!
>>>>>>>>>>>>>>>>>>>>>>> Bwrdd yr Iaith?
>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>> A red tile.
>>>>>>>>>>>>>>>>>>>>> Pleidiol wyf gwlad! LLOL
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> True but the other testicle is affected now.
>>>>>>>>>>>>>>>>>>> Llwyddych!!!
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> No! it would be illegal!
>>>>>>>>>>>>>>>>> Rhwygchdd?
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> No, I'm not wearing knickers!
>>>>>>>>>>>>>>> Gwrd! Cwddrgwrrych!!!!!
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Yes, it was hidden under the bath.
>>>>>>>>>>>>> Yr cwnrchdd y llgach?
>>>>>>>>>>>>>
>>>>>>>>>>>> It was blue.
>>>>>>>>>>> Bllgdd? Y cwdgch wddgych!
>>>>>>>>>>>
>>>>>>>>>> They escaped yesterday.
>>>>>>>>> Llgwyddnych yr?
>>>>>>>>>
>>>>>>>> Not on your life!
>>>>>>> Wyrdggrych!!!!
>>>>>>>
>>>>>> Unfortunately they had to pull it down.
>>>>> Dyffychdd?
>>>>>
>>>> No, they've moved.
>>> Pwllg! Caerdydd?
>>>
>> How could I?
>
> Heddlu! Heddlu! Cwm dyffedd!!!!
>
Yes, 3 times.