From: Robert Klemme Date: 2004-01-15T18:11:36+09:00 Subject: Re: Warnings on assignments in conditionals? "Bret Jolly" schrieb im Newsbeitrag news:7e7131a1.0401141110.d5fa65a@posting.google.com... > David Heinemeier Hansson wrote in message news:<8609BD15-45EF-11D8-A416-000A958E6254@loudthinking.com>... > > I consider the following statements to be perfectly legit (although > > perhaps slightly ambigious) and Ruby does obey, but throws this > > mandating warning. Am I just being paranoid or is Ruby forcing a style > > upon me :)? > > > > irb(main):008:0> if a = 1 then true else false end > > (irb):8: warning: found = in conditional, should be == > > => true > > irb(main):009:0> a > > => 1 > > > > irb(main):010:0> if a = nil then true else false end > > (irb):10: warning: found = in conditional, should be == > > => false > > irb(main):011:0> a > > => nil > > But this C idiom really doesn't make much sense in Ruby, since > in Ruby 0 is true in a boolean context. You might want to say > something like: > if not (a = c).nil? then foo else bar end > but what you have is almost certainly either an error or at least > stylistically dubious. So yes, I guess Ruby is forcing a style on > you. I think that if you really want something like this, the Ruby > way would be to define a method 'true?' for objects: > class Object > def true? > if nil? or self == false then return false end > true > end > def false? > not true? > end > end > > then write: > if (a = c).true? then foo else bar end *shudder* You're not serious, are you? And if so, why didn't you define them like this: class Object def true?; self; end def false?; not self; end end Cheers robert