Posted By

uberdragon on 07/09/15


Tagged


Versions (?)

Ruby To Boolean module


 / Published in: Ruby
 

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

  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  

You need to login to post a comment.