From: Rob Burrowes Date: 2006-05-04T09:42:45+09:00 Subject: Re: Newbee Question on what if x === 2..5 does --Apple-Mail-237-892431984 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed I get what you mean. It makes sense to have the === as a method of the range, not of x, but this produces no result at all. rob1:~ rbur004$ ruby -e 'a = [1,2,3,4,5,6,7,8,9,10]; a.each { |x| print x, "->"; if (2...5 == x) then print x, " " end; print "\n"}' 1-> 2-> 3-> 4-> 5-> 6-> 7-> 8-> 9-> 10-> On 4/05/2006, at 11:06 AM, Jacob Fugal wrote: > On 5/3/06, Rob Burrowes wrote: >> I'm new to ruby and don't understand why the following code does what >> is does. Can anyone enlighten me? >> >> ruby -e 'a = [1,2,3,4,5,6,7,8,9,10]; a.each { |x| if (x === 2..5 ) >> then print x, " " end}; puts ' >> 2 3 4 5 6 7 8 9 10 >> >> I would have thought this would be the same as >> >> ruby -e 'a = [1,2,3,4,5,6,7,8,9,10]; a.each { |x| case x; when 2..5 >> then print x, " "; end}; puts ' >> 2 3 4 5 > > Try switching x === 2..5 to 2..5 === x. The case equality operator > (===) is not cummutative. The conditions of a case statement are > evaluated using the when condition as the *receiver* of the case > equality operator, not the argument. Ie. > > case a > when b > ... > end > > is equivalent to: > > if b === a > ... > end > >> I am also confused that >> >> ruby -e 'while gets; print if /
/; end' < >> fred >>
>> UETHUEPUK >> THEUTEUH >> teuhepxn >> thueonhoe >> ononueuouh >>
>> >> >> and a three . range test give the same output. >> >> ruby -e 'while gets; print if /
/; end' >> < fred >>
>> UETHUEPUK >> THEUTEUH >> teuhepxn >> thueonhoe >> ononueuouh >>
>> > > Sorry, can't help you there. I always have a hard time remembering > what the difference between .. and ... are in that context. I stay > away from it in favor of more descriptive flagging in a while loop. > E.g.: > > flag = false > while line = gets > flag = true if line =~ /
flag = false if line =~ /<\/body>/ > print line if flag > end > > Jacob Fugal > Rob Rob Burrowes Ph: +64 (9) 3737 599 Extn 87972 Computing Services Manager Fax: +64 (9) 3737 453 Computer Science Department Mob:+64 (27) 4731 856 The University of Auckland Rm 391, Level 3 38 Princes Street http://www.cs.auckland.ac.nz/~rob Private bag 92019 http://www.wikarekare.org Auckland 1001 http://www.burrowes.org --Apple-Mail-237-892431984--