From: Robert Klemme Date: 2005-05-04T02:49:38+09:00 Subject: Re: Pickaxe page 342, Ranges in Boolean Expressions --I don't understand Gunnar wrote: > Jacob Fugal wrote: > >> On 5/3/05, Gunnar wrote: >>> Hi all >>> I can not make sense of the boolean ranges. The example >>> (11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} => [nil, 12, nil, >>> nil, nil, 16, 17, 18, nil, 20]. >>> >>> I can figure out all numbers except the '18', which in my opinion >>> should be a 'nil' since i%3==0 is true and would cause the state to >>> change from SET to UNSET. Is the state machine Fig 22.1 correct? >> >> It appears to me that the second condition in the range UNSETs, but >> inclusively (which makes sense for the semantics of '..'). That's >> why, for example, 12 is printed at all... 12%4==0, so it SETs, >> retval is set to true since it is SET, then 12%3==0 so it UNSETs. >> But the retval is already set, and it returns true, thus printing 12. >> >> In the same way, when we get to 18, the state machine is already SET >> (since 16). retval is set to true since it is SET, then 18%3==0 so it >> unsets. But just like with 12, retval is already set, so it returns >> true and prints 18. >> >> For more information see [ruby-talk:91137]. >> >> Jacob Fugal >> >> [ruby-talk:91137] >> [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/91137 > OK. > Thank you for answering so very quickly > Gunnar It's historic: Perl does it the same way. It's useful because you can easily match ranges (for example of line comments, that start with "=begin" and end with "=end" line) etc. >> s=< "1\n2\n3\n4\n5\n6\n6\n" >> ?> s.each do |item| ?> puts /3/ =~ item .. /5/ =~ item ? "inside:"+item : "outside:"+item >> end outside:1 outside:2 inside:3 inside:4 inside:5 outside:6 outside:6 => "1\n2\n3\n4\n5\n6\n6\n" >> Kind regards robert