From: Rob Biedenharn Date: 2007-06-11T09:48:21+09:00 Subject: Re: Possible bug with Range#include? On Jun 10, 2007, at 5:40 PM, bbiker wrote: > On Jun 9, 3:03 pm, Rob Biedenharn wrote: >> On Jun 9, 2007, at 10:34 AM, Logan Capaldo wrote: >> >>> On Sat, Jun 09, 2007 at 11:20:16PM +0900, charon wrote: >>>> Hi, >>>> definitely _NOT_ a bug >>>> read this plz:http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ >>>> ruby-talk/168231 >>> Well, it not being a bug is arguable. ;) It's definitely not an >>> unintentional bug. >> >> No, no argument at all. There is however conflicting information in >> the Pickaxe that describes Range#member? as having different behavior >> that #include? (although ri Range#member? shows that #=== #include? >> and #member? are the same). >> >> If the OP wants behavior like the Pickaxe (now incorrectly) >> describes, you can always do: >> >> class Range >> def member? item >> any? {|x| item == x} >> end >> end >> >> irb> ('A'..'IV').member?('J') >> => true >> irb> (1..10).include?(5.5) >> => true >> irb> (1..10).member?(5.5) >> => false >> >> Although the performance for testing a "big" Range and an item close >> to the #end might be undesirable. >> >> Of course, that does beg the question of whether #include? (#===) or >> #member? is used by libraries. Is there a common way to note which >> of a set of names is used when there are aliases? It seems like if >> one were to treat #begin, #include?, and #end as a set, then #first, >> #member?, and #last would be a similar set (currently all aliases) >> and a redefinition of #member? could be reasonably expected to >> redefine #last to be the final value returned by #each. For a Range >> where #exclude_end? is true, #last != #end would be justifiable. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> R...@AgileConsultingLLC.com > > I am the OP. Subsequent to my post I also found that #include? would > return true even though an item is not included (no a member) in a > range. > > irb(main):039:0> ('A'..'IV').include?('A:') => true # Note A: is > NOT a member of 'A'..'IV' > > Now in mho, any method that produces incorrect results is buggy! > whether intentionally or not. > I do not believe that any one intentionally writes buggy code. > > As I needed to find a reliable method to determine whether an item is > included in a range object, I tried several schemes none of which were > reliable. Finally, I tried the following: > > rng = 'A'..'IV' > rng_arr = rng.to_a > rng_arr.include?('Z') > now the last statement returns the correct answer. > > Additional tests showed that it produces consistently correct answers. > > But I felt that it was very inefficient in memory usage and execution. > > So I so I redefined Range#include?. Prior to my posting this messaage, > I read Rob Biedenharn. > > Here is my implementation. > > class Range > # my solution > def include?(t) > inc = false > self.each do |m| > if m == t > inc = true > break > end > end > inc > end > > # Rob Biedenharn's solution > def member?(item) > any? { |x| item == x } > end > end > > rng = "A".."IV" > t = "I5" > range = "A".."IV" > > puts range.include?("BZ") => true > puts range.member?("BZ") => true > > puts range.include?("IW") => false > puts range.member?('IW') => false > > puts range.include?("A:") => false > puts range.member?("A:") => false > > puts range.include?("K") => true > puts range.include?("K") => true > > As you can see, both solutions provide correct answers. > Rob Biedenharn's solution is obviously simpler than my solution. So it > should the preferred solution .. Occam's Razor. > However, I do not know the 'behind the scene magic' that any? does > with regards to a Range object. > > C:\Documents and Settings\Owner>qri any? > -------------------------------------------------------- > Enumerable#any? > enum.any? [{|obj| block } ] => true or false > ---------------------------------------------------------------------- > -- > Passes each element of the collection to the given block. The > method returns +true+ if the block ever returns a value other > than > +false+ or +nil+. If the block is not given, Ruby adds an > implicit > block of +{|obj| obj}+ (that is +any?+ will return +true+ if at > least one of the collection members is not +false+ or +nil+. > > %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true > %w{ ant bear cat}.any? {|word| word.length >= 4} #=> true > [ nil, true, 99 ].any? #=> true > > Does it create an array to get the collection? > If yes, then my method would be the preferred one. The case worse > scenerio is when the test item is not included in the range. > Efficiency decreases as the item's location is toward the far end. > > My question is: which solution is better in term of memory usage and > execution time? > > Thank You. > > renard@nc.rr.com Since the #any? method is provided by Enumerable, it uses the #each method supplied by the underlying class (i.e., Range). I'd expect that your redefinition of Range#include? is equivalent to the use of any? where the block evaluates the x==item as both will return as soon as a value matches. It does go directly to the question of behavior. Like David Black said, Ranges are fundamentally not collections even though some kinds of Ranges are easily converted to an Array. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com