Ruby To Boolean module


/ Published in: Ruby
Save to your folder(s)

injects .to_bool instance method into NilClass, TrueClass, FalseClass, Numeric & String


Copy this code and paste it in your HTML
  1. module ToBoolean
  2. def to_bool
  3. return true if self == true || self.to_s.strip =~ /^(true|t|yes|y|1)$/i
  4. return false
  5. end
  6. end
  7.  
  8. class NilClass; include ToBoolean; end
  9. class TrueClass; include ToBoolean; end
  10. class FalseClass; include ToBoolean; end
  11. class Numeric; include ToBoolean; end
  12. class String; include ToBoolean; end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.