Bug in Range#end with exclusive ranges?

D

Daniel Berger

Hi all,

Ruby 1.8.2 preview 2

If I have r = Range.new(0,50,true), shouldn't r.end return 49 and not
50? I am confused by this.

If this has come up on the ML in the past, please direct me (and
others) to the thread - I couldn't find it.

Regards,

Dan

PS - I disagree with this behavior.
 
A

Austin Ziegler

Ruby 1.8.2 preview 2

If I have r = Range.new(0,50,true), shouldn't r.end return 49 and
not 50? I am confused by this.

If this has come up on the ML in the past, please direct me (and
others) to the thread - I couldn't find it.


In mathematical range notation, the difference between 0..50 and
0...50 is [0, 50] and [0, 50). Thus:

irb(main):001:0> a = 0..50
=> 0..50
irb(main):002:0> b = 0...50
=> 0...50
irb(main):003:0> a.include?(49)
=> true
irb(main):004:0> b.include?(49)
=> true
irb(main):005:0> a.include?(49.9)
=> true
irb(main):006:0> b.include?(49.9)
=> true
irb(main):007:0> a.include?(50)
=> true
irb(main):008:0> b.include?(50)
=> false

-austin
 
M

Martin DeMello

Austin Ziegler said:
In mathematical range notation, the difference between 0..50 and
0...50 is [0, 50] and [0, 50). Thus:

It doesn't seem quite right that Ruby should do this though

irb(main):001:0> a = (0..50)
a = (0..50)
=> 0..50
irb(main):002:0> b = (0.0..50.0)
b = (0.0..50.0)
=> 0.0..50.0
irb(main):004:0> a.last
a.last
=> 50
irb(main):005:0> a.last.class
a.last.class
=> Fixnum
irb(main):006:0> b.last.class
b.last.class
=> Float
irb(main):007:0> a.include?(49.9)
a.include?(49.9)
=> true
irb(main):008:0> b.include?(49.9)
b.include?(49.9)
=> true

Shouldn't Range#include work differently for integers?

martin
 
A

Austin Ziegler

Austin Ziegler said:
In mathematical range notation, the difference between 0..50 and 0...50 is
[0, 50] and [0, 50). Thus:

It doesn't seem quite right that Ruby should do this though
irb(main):001:0> a = (0..50)
a = (0..50)
=> 0..50
irb(main):002:0> b = (0.0..50.0)
b = (0.0..50.0)
=> 0.0..50.0
irb(main):004:0> a.last
a.last
=> 50
irb(main):005:0> a.last.class
a.last.class
=> Fixnum
irb(main):006:0> b.last.class
b.last.class
=> Float
irb(main):007:0> a.include?(49.9)
a.include?(49.9)
=> true
irb(main):008:0> b.include?(49.9)
b.include?(49.9)
=> true

Shouldn't Range#include work differently for integers?

No. You want Range#member?

----------------------------------------------------- Range#member?
rng.member?(val) => true or false
 
G

Gavin Sinclair

No. You want Range#member?

I consider it poor form that in Enumerable, include? and member? are
the same thing, but not so in Range (which includes Enumerable).
Aliases should be a matter of personal taste

Cheers,
Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top