From: Robert Dober Date: 2006-09-20T19:59:10+09:00 Subject: Re: Something strange in ruby or I'm a newbie? ------=_Part_169556_30934447.1158749947531 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 9/20/06, Hussachai Puripunpinyo wrote: > > Question 2: > > nil in ruby can put in condition statement > such as if(nil) it will assume that's false > > ex. > if(nil) > else > puts "nil is false" > end > > sure it show me the string > Becuase I'm curious,I want to test more > > puts nil=3D=3Dnil #Yeah that's true > > puts !nil #true > > puts !nil=3D=3Dtrue #true > > puts nil=3D=3Dfalse #FALSE !!! > > What happen in the last statement? [or I miss something] That is a very interesting way you are looking at things, I would not necessarily think Ruby's behavior as bad. For myself I think rubies semantics can be expressed as follows class Object def true? true end end class NilClass def true? false.true? end end class FalseClass def true? self end end p true.true? p "".true? p nil.true? Now - in the classical terms of thinking you have invoked above, if an object evaluates to true or false just replace it by #true?. (you can imagine the interpreter doing that e.g.) You see somehow comparing nil =3D=3D false does not make too much sense in that context, as an analog= y you have puts "42 is true" if 42 puts "42**2 is true 2" if 1764 and yet 42 =3D=3D 1764 =3D> false ;) Cheers Robert > -- > Posted via http://www.ruby-forum.com/. > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_169556_30934447.1158749947531--