wli2ig
Last Updated: February 25, 2016
·
392
· proton
Cb2df894d5595dbc7df2ce378adb26c5

Ruby to_bool conversions (monkeypatching)

class Object
  def to_bool
    self ? true : false
  end
end

class String
  def to_bool
    return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
    return false if self == false || self.empty? || self =~ (/(false|f|no|n|0)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end
end