[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Range#member? Oddity

James Gray

1/13/2006 8:50:00 PM

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



41 Answers

Berger, Daniel

1/13/2006 8:54:00 PM

0

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?
>
> >> 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 cannot duplicate this with 1.8.2 or 1.8.4.

- Dan




James Gray

1/13/2006 8:59:00 PM

0

On Jan 13, 2006, at 2:54 PM, 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?
>> >> 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 cannot duplicate this with 1.8.2 or 1.8.4.

Odd. I'm using 1.8.4:

$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

James Edward Gray II


Justin Collins

1/13/2006 8:59:00 PM

0

It happens in 1.8.2 for me. It shows "1" and "10" as being in the range,
but nothing else.

[justinc@justinc-dsktp ~]$ ruby -v
ruby 1.8.2 (2004-12-25) [i586-linux-gnu]
[justinc@justinc-dsktp ~]$ irb
irb(main):001:0> range = ("1".."10")
=> "1".."10"
irb(main):002:0> range.to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
irb(main):003:0> range.member?("1")
=> true
irb(main):004:0> range.member?("2")
=> false
irb(main):005:0> range.member?("10")
=> true
irb(main):006:0>


Note that range _isn't_ getting converted into an array.

-Justin

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?
>>
>> >> 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 cannot duplicate this with 1.8.2 or 1.8.4.
>
> - Dan
>
>
>


Daniel Harple

1/13/2006 9:02:00 PM

0

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.

$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]


J. Ryan Sobol

1/13/2006 9:03:00 PM

0

On Jan 13, 2006, at 3:54 PM, 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?
>> >> 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 cannot duplicate this with 1.8.2 or 1.8.4.

I can.

$ ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

~ ryan ~


tokmak tokmak

1/13/2006 9:05:00 PM

0

it gets odder:

irb(main):001:0> range = ("1".."10")
=> "1".."10"
irb(main):002:0> range.to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
irb(main):003:0> range.member?("1")
=> true
irb(main):004:0> range.member?("2")
=> false
irb(main):005:0> range.member?("4")
=> false
irb(main):006:0> range.member?("9")
=> false
irb(main):007:0> range.member?("10")
=> true
irb(main):008:0> range.include?("2")
=> false
irb(main):009:0> range.include?("1")
=> true
irb(main):010:0> range === "1"
=> true
irb(main):011:0> range === "2"
=> false
irb(main):012:0> RUBY_VERSION
=> "1.8.2"


2006/1/13, James Edward Gray II <james@grayproductions.net>:
> 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
>
>
>


Berger, Daniel

1/13/2006 9:07:00 PM

0

James Edward Gray II wrote:
> On Jan 13, 2006, at 2:54 PM, 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?
>>> >> 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 cannot duplicate this with 1.8.2 or 1.8.4.
>
>
> Odd. I'm using 1.8.4:
>
> $ ruby -v
> ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]
>
> James Edward Gray II
>

Whoops, I misspoke. I was using numbers, not strings. I do, in fact, get the
same behavior in 1.8.4 (and 1.8.2).

/me guesses randomly that it has something to do with stringified numbers in
particular.

Regards,

Dan


Dan Hinz

1/13/2006 9:11:00 PM

0

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.
>
> $ ruby -v
> ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

--
I not only live each endless day in grief, but live each day
thinking about living each day in grief.
-- C.S. Lewis

Daniel W. Hinz Xerox Corp: XOG/SEBU/MCD/EIDC/ISM&D
MS: 111-03J e-mail: dhinz@eng.mc.xerox.com
800 Phillips Road TEL: 585.422.8078
Webster, NY 14580

Matthew Desmarais

1/13/2006 9:16:00 PM

0

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?
>
> >> 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
>
>
>
Hi,

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.

There's a previous thread on ruby-talk about it, here's a link to
somewhere near the conclusion of the discussion:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Matthew


James Gray

1/13/2006 9:29:00 PM

0

On Jan 13, 2006, at 3:16 PM, Matthew Desmarais wrote:

> There's a previous thread on ruby-talk about it, here's a link to
> somewhere near the conclusion of the discussion:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

This cleared it up for me. Thank you.

I was aware of the old system, where member?() and include?() had
separate meanings and just didn't know it had been abandoned.

James Edward Gray II