Hi --

On Mon, 17 May 2010, Robert Klemme wrote:

> 2010/5/17 David A. Black <dblack / rubypal.com>:
>> Hi --
>>
>> On Fri, 14 May 2010, Vikrant Chaudhary wrote:
>>
>>> Hi,
>>> If I do -
>>>
>>> ('A'..'Z').include?('AA')
>>>
>>> It returns "true", while
>>>
>>> ('A'..'Z').to_a.include?('AA')
>>>
>>> (of course) returns "false". Is it intentional or possibly a bug?
>>> I'm using ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux] on
>>> Ubuntu 10.04 x64
>>
>> As per the other answers, it's because ('A'..'Z').to_a is an array of
>> discrete values, whereas ('A'..'Z') is a range with a start and end.
>> I'll just add that in Ruby 1.9, things have changed so that include?
>> on a range is, I think, the same as .to_a.include? and the behavior of
>> the old include? is found in Range#cover?
>>
>> $ ruby191 -e 'p(("A".."Z").include?("AA"))'
>> false
>> $ ruby191 -e 'p(("A".."Z").cover?("AA"))'
>> true
>>
>> include? is also available under the name member?, which I think is a
>> little clearer (both because include? used to mean something else, and
>> because being a "member" of a range doesn't really make sense so it
>> almost has to mean something other than just being within the range).
>
> Throwing just my cent in here: I believe the solution in 1.9.* is
> better because here behavior of #include? is consistent with other
> collection types: e.include?(x) == true <=> e.any? {|y| x == y} in
> other words, #include? is true only if the element is also seen during
> iteration.

Although as Rick points out:

   $ ruby191 -e "p((1..10).include?(3.4))"
   true

which I hadn't taken into account in my too-simple summary of 1.9's
Range#include?. I'm not sure what the underlying principle is that
causes that last case to be true while the "A".."Z" including "AA" is
false, though.


David

-- 
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE                   Ruby training with Black/Brown/McAnally
     COMPLEAT          Coming to Chicago area, June 18-19, 2010!
              RUBYIST  http://www.compleatrubyist.com