From: Daniel Baird Date: 2006-05-04T10:17:00+09:00 Subject: Re: Newbee Question on what if x === 2..5 does ------=_Part_9103_30645438.1146705417118 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Try using: if (2...5) =3D=3D=3D x the =3D=3D=3D seems to bind before the ... ;D On 5/4/06, Rob Burrowes wrote: > > I mistyped that (left out an =3D), but the same result. > > rob1:~ rbur004$ ruby -e 'a =3D [1,2,3,4,5,6,7,8,9,10]; a.each { |x| > print x, "->"; if (2...5 =3D=3D=3D x) then print x, " " end; print "\n"}' > 1-> > 2-> > 3-> > 4-> > 5-> > 6-> > 7-> > 8-> > 9-> > 10-> > > On 4/05/2006, at 12:42 PM, Rob Burrowes wrote: > > > I get what you mean. It makes sense to have the =3D=3D=3D as a method o= f > > the range, not of x, but this produces no result at all. > > > > rob1:~ rbur004$ ruby -e 'a =3D [1,2,3,4,5,6,7,8,9,10]; a.each { |x| > > print x, "->"; if (2...5 =3D=3D 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 =3D [1,2,3,4,5,6,7,8,9,10]; a.each { |x| if (x =3D=3D=3D 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 =3D [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 =3D=3D=3D 2..5 to 2..5 =3D=3D=3D x. The case equality = operator > >> (=3D=3D=3D) 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 =3D=3D=3D 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 =3D false > >> while line =3D gets > >> flag =3D true if line =3D~ /
>> flag =3D false if line =3D~ /<\/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 > > > > > > 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 > > > > -- Daniel Baird http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog :: Things That Suck) ------=_Part_9103_30645438.1146705417118--