From: dohzya Date: 2007-09-08T19:21:48+09:00 Subject: Re: #to_b - why isn't this one already in there? Le samedi 08 septembre 2007 � 11:01 +0900, Giles Bowkett a �crit : > everybody knows > > if "false" > > returns true. there are situations where that's a pain. I did a Rails > thing where I was creating objects and calling methods from options > hashes which I sometimes got by simply passing in params from a > controller and sometimes actually wrote as code. so I would have > > @some_boolean = options[:some_boolean] > > and five minutes later I'd be stubbing my metaphorical toe on an > > if "false" > > statement. > > so I changed it to this: > > @some_boolean = options[:some_boolean].to_b > > and implemented #to_b by doing this: > > class String > def to_b > case self > when "true" > true > when "false" > false > else > raise Exception > end > end > end > > class FalseClass > def to_b > false > end > end > > class TrueClass > def to_b > true > end > end > > at that point I could pass in options hashes and it didn't matter > whether they came in properly and correctly as actual boolean values > or blustered in the servants' entrance as URL parameters. > > what's weird to me about that is, it's so obvious, so useful, and so > trivial that I don't understand why it isn't the language to begin > with. obviously it comes from the inspiration of #to_s, #to_i, etc. > anyone who's ever written > > if foo == "false" > > can see the usefulness of this. > Hi, why "false" would be false when "" is true ? There are just 2 values for false : nil and false. Others would introduce confusion... Use a function/method to convert your string in boolean with some rules, but don't change the ruby philosophy ! < of course, it's just my opinion ;) > -- Etienne Vallette d'Osia