Module: Corefines::String::ToB

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#to_bBoolean

Interprets common affirmative string meanings as true, otherwise false. White spaces and case are ignored.

The following strings are interpreted as true: 'true', 'yes', 'on', 't', 'y', '1'.

Examples:

'yes'.to_b   #=> true
'Yes '.to_b  #=> true
' t '.to_b   #=> true
'no'.to_b    #=> false
'xyz'.to_b   #=> false
''.to_b      #=> false

Returns:

  • (Boolean)

    true if this string represents truthy, false otherwise.


401
402
403
404
405
406
407
# File 'lib/corefines/string.rb', line 401

module ToB
  refine ::String do
    def to_b
      %w[true yes on t y 1].include? self.downcase.strip
    end
  end
end