[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Puzzling behaviour with range...

Raphael Clancy

1/7/2009 4:10:00 PM

Hi all!

I've been over (some of) the documentation, and I can't figure this one
out, can anyone tell me what range is expecting? And, why it's behavior
is different from other numerics?

$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
$ irb
irb(main):001:0> 1.class
=> Fixnum
irb(main):002:0> 3.14.class
=> Float
irb(main):003:0> 1..4.class
ArgumentError: bad value for range
from (irb):3
from :0
irb(main):004:0> (1..4).class
=> Range
irb(main):005:0>

Thanks!
--
Posted via http://www.ruby-....

4 Answers

Stefano Crocco

1/7/2009 4:14:00 PM

0

Alle Wednesday 07 January 2009, Raphael Clancy ha scritto:
> Hi all!
>
> I've been over (some of) the documentation, and I can't figure this one
> out, can anyone tell me what range is expecting? And, why it's behavior
> is different from other numerics?
>
> $ ruby -v
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
> $ irb
> irb(main):001:0> 1.class
> => Fixnum
> irb(main):002:0> 3.14.class
> => Float
> irb(main):003:0> 1..4.class
> ArgumentError: bad value for range
> from (irb):3
> from :0
> irb(main):004:0> (1..4).class
> => Range
> irb(main):005:0>
>
> Thanks!

The point is that 1..4.class is parsed as 1..(4.class), that is 1..Fixnum
which, of course, isn't a valid range.

Stefano


Sebastian Hungerecker

1/7/2009 4:15:00 PM

0

Raphael Clancy wrote:
> Hi all!
>
> I've been over (some of) the documentation, and I can't figure this one
> out, can anyone tell me what range is expecting?

Two values that are comparable to each other.
1..4 works because 1 and 4 can be compared.
1..4.class does not work because 1 and Integer are not comparable to each
other.


> And, why it's behavior
> is different from other numerics?

a) Range is not a numeric
b) Which difference do you mean?

HTH,
Sebastian
--
NP: Ulver - Sadface
Jabber: sepp2k@jabber.org
ICQ: 205544826

Raphael Clancy

1/7/2009 5:08:00 PM

0

Sebastian Hungerecker wrote:
>
> a) Range is not a numeric
> b) Which difference do you mean?
>
> HTH,
> Sebastian

Thanks for the reply!

I guess what I'm getting at is that the Ruby parser makes a lot of
assumptions, when I write something like 1.class, the parser assumes I'm
calling a method on the Fixnum 1 rather than assuming I'm trying to make
some strange Float. And when I type 3.14.class the parser is again smart
enough to figure things out, but this behavior is not carried over to
range. I know it's a design choice, and that range needs to be able to
hold more than just numerics, but since a statement like "range 1..'d'"
is meaningless, it seems like the proper behavior for range should be to
examine the first element of the range statement and then assume the
second element is of the same type (at least as far as parsing method
calls goes) then we would have (x..y).class == x..y.class which seems
like it would be more in line with other basic data types...

Of course, in typing this out, I see why it's set up the way it is,
since we might want to do something like this...

class SomeObj
def lastOne()
17
end
end
blah = SomeObj::new
a = 1..blah.lastOne

Thanks for setting me on the path! (and letting me babble on...)

--
Posted via http://www.ruby-....

Robert Klemme

1/7/2009 10:13:00 PM

0

On 07.01.2009 18:07, Raphael Clancy wrote:
> Sebastian Hungerecker wrote:
>> a) Range is not a numeric
>> b) Which difference do you mean?
>>
>> HTH,
>> Sebastian
>
> Thanks for the reply!
>
> I guess what I'm getting at is that the Ruby parser makes a lot of
> assumptions, when I write something like 1.class, the parser assumes I'm
> calling a method on the Fixnum 1 rather than assuming I'm trying to make
> some strange Float.

What you call "assumptions" is actually "precedence". The syntax of the
language is defined as it is and "." (method invocation, not to be
confused with the decimal point) has higher precedence than the range
operator "..". This really makes sense, for example:

for i in s.min..s.max
puts i
end

> And when I type 3.14.class the parser is again smart
> enough to figure things out, but this behavior is not carried over to
> range. I know it's a design choice, and that range needs to be able to
> hold more than just numerics, but since a statement like "range 1..'d'"
> is meaningless, it seems like the proper behavior for range should be to
> examine the first element of the range statement and then assume the
> second element is of the same type (at least as far as parsing method
> calls goes) then we would have (x..y).class == x..y.class which seems
> like it would be more in line with other basic data types...

It cannot work that way because of the way parsers work: first the lexer
recognizes tokens. This is where it needs to decide that "1.23" is a
float literal and "1.23.class" is actually three literals ("1.23", ".",
"class"). Only after that phase that parser builds up the parse tree
which also establishes precedence. Your suggestion basically requires
to define different productions in the syntax for ranges with integer
literals and for ranges with other expressions to the left and right of
"..". I am not sure whether this is actually possible but there is a
certain risk that the syntax will not be conflict free and in any way it
means making the beast even more complex than it is today. Given the
limited usability and the easy workaround I'd say that cost estimates
rule against this change.

> Of course, in typing this out, I see why it's set up the way it is,
> since we might want to do something like this...
>
> class SomeObj
> def lastOne()
> 17
> end
> end
> blah = SomeObj::new
> a = 1..blah.lastOne

Exactly, arbitrary expressions are allowed on both sides of the "..".
Note also, that in this case the type of expression is actually unknown
at parse time.

> Thanks for setting me on the path! (and letting me babble on...)

:-)

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end