On Sep 8, 5:01 am, "Giles Bowkett" <gil... / gmail.com> 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

"false".to_bool #=> false
"on".to_bool #=> true
1.to_bool #=> true
0.to_bool #=> false
"1".to_bool #=> true
nil.to_bool #=> false

enjoy
Sharon