Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/kojo/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#to_typedObject

Convert a string to the most appropriate type



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kojo/extensions/string.rb', line 3

def to_typed
  if self =~ /\A[+-]?\d+\Z/
    self.to_i

  elsif self =~ /\A[+-]?\d+\.\d+\Z/
    self.to_f

  elsif %w[yes no true false].include? downcase
    %w[yes true].include? downcase

  else
    self

  end
end