From: Robert Klemme Date: 2010-05-17T23:48:26+09:00 Subject: Re: Range on strings. 2010/5/17 David A. Black : > 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. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/