There was some discussion about this in the recent past. If my memory serves me right (certainly an infrequent happening), the issue that you're running into is that Range#member? is implemented as:
class Range
def member?(val)
if self.exclude_end?
(self.first <= val) and (val < self.last)
else
(self.first <= val) and (val <= self.last)
end
end
end
You should find this in both 1.8.2 and 1.8.4 I think.
I don't know why to_a is working here.
I know that a range from '1' to '10' could be like you described, but it doesn't make a logical sense I think.
like ('a'..'ah') what should that be?
[a-255.chr] and all a[0.chr-255.chr]?
This is what I think, of course.
Dunno if it makes sense :>
I'm not understanding what I am seeing here. Can anyone please explain why the last line of this session gives *false* as an answer?
>> range = ("1".."10")
=> "1".."10"
>> range.to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
>> range.member?("1")
=> true
>> range.member?("2")
=> false
James Edward Gray II
I'm not understanding what I am seeing here. Can anyone please explain why the last line of this session gives *false* as an answer?
>> range = ("1".."10")
=> "1".."10"
>> range.to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
>> range.member?("1")
=> true
>> range.member?("2")
=> false
James Edward Gray II
This would seem to make it an oddity of String#succ, that behaving automagically, not generating successors with respect to String comparison.
E.g. for any Integers i, i.succ > i. For some strings, that does't hold true.
Bottom line: Don't use strings when you're really using numbers. Like in mathemathical contexts. D'oh. You could possibly hack around that in Range code to provide for data types where generating successors is inconsistent with comparison, but I wouldn't like to see that.
David Vallner
···
On Fri, 13 Jan 2006 22:16:14 +0100, Matthew Desmarais <desmarm@gmail.com> wrote:
James Edward Gray II wrote:
I'm not understanding what I am seeing here. Can anyone please explain why the last line of this session gives *false* as an answer?
There was some discussion about this in the recent past. If my memory serves me right (certainly an infrequent happening), the issue that you're running into is that Range#member? is implemented as:
class Range
def member?(val)
if self.exclude_end?
(self.first <= val) and (val < self.last)
else
(self.first <= val) and (val <= self.last)
end
end
end
You should find this in both 1.8.2 and 1.8.4 I think.
I'm not understanding what I am seeing here. Can anyone please explain why the last line of this session gives *false* as an answer?
>> range = ("1".."10")
=> "1".."10"
>> range.to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
>> range.member?("1")
=> true
>> range.member?("2")
=> false
James Edward Gray II
I don't know for sure but my guess has something to do with numbers vs
characters and whether "1".."10" expands to the same thing that 1..10
does.
range=(1..10)
1..10
range.member?(1)
true
range.member?(2)
true
range.member?(3)
true
puts range
1..10
Huh???
-dwh-
···
On Fri, 2006-01-13 at 16:01, Daniel Harple wrote:
Daniel Berger wrote:
> James Edward Gray II wrote:
>> I'm not understanding what I am seeing here. Can anyone please
>> explain why the last line of this session gives *false* as an answer?
>> <snip>
>
> I cannot duplicate this with 1.8.2 or 1.8.4.
>
> - Dan
I'm getting this too. 1 and 10 both return true, everything else
returns false.
You could possibly hack around that in Range code to provide for data types where generating successors is inconsistent with comparison, but I wouldn't like to see that.
Yes, that always works, but it beats the point of having first class ranges as opposed to just having a pythonesque range function in the first place. I'd personally rather coerce the strings to numbers if I know they represent such to get more type safety and possibly some execution speed too.
David Vallner
···
On Fri, 13 Jan 2006 23:00:37 +0100, James Edward Gray II <james@grayproductions.net> wrote:
On Jan 13, 2006, at 3:58 PM, David Vallner wrote:
You could possibly hack around that in Range code to provide for data types where generating successors is inconsistent with comparison, but I wouldn't like to see that.