From: Trans Date: 2007-09-11T03:30:14+09:00 Subject: Re: #to_b - why isn't this one already in there? On Sep 10, 11:16 am, Sharon Rosner wrote: > On Sep 8, 5:01 am, "Giles Bowkett" wrote: > > > everybody knows > > > if "false" > > > returns true. > > class Object > def to_bool > case self > when true, false: self > when nil: false > else > to_i != 0 > end > end > end > > class String > TRUE_REGEXP = /^(yes|true|on|t|1|\-1)$/i.freeze > FALSE_REGEXP = /^(no|false|off|f|0)$/i.freeze > > def to_bool > case self > when TRUE_REGEXP: true > when FALSE_REGEXP: false > else > to_i != 0 > end > end > end Ah. This is a good example for demonstrating what I meant. To fit into Ruby convention, only TrueClass, FalseClass and NilClass (and things like NullClass) should respond to to_bool. The String method should be #to_b, because it is not a true "Boolean" type. T.