From: Trans Date: 2007-09-08T23:54:29+09:00 Subject: Re: #to_b - why isn't this one already in there? On Sep 7, 7:01 pm, "Giles Bowkett" wrote: > 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 This makes more sense as #to_bool, and certainly not default conditional behavior. Yet, a #to_b to dictate conditions could be useful, allowing us to create our own variants of Nil. However, the only alternate to Nil I've ever found useful is Null (nil that returns itself on method_missing) --so I'd be pretty happy if we just had that. Also, I believe there's a efficiency issue with using #to_b for conditions, which is why it is never considered. T.