Class: String

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

Constant Summary collapse

TRUES =
%w[t true on y yes 1].freeze
FALSES =
%w[f false off n no 0].freeze

Instance Method Summary collapse

Instance Method Details

#to_a(delimiter: " ") ⇒ Object



15
16
17
# File 'lib/ext/string.rb', line 15

def to_a(delimiter: " ")
  split(delimiter)
end

#to_b(invalid_value_behaviour: proc { nil }) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/ext/string.rb', line 7

def to_b(invalid_value_behaviour: proc { nil })
  value = strip.downcase
  return true  if TRUES.include?(value)
  return false if FALSES.include?(value)

  invalid_value_behaviour.call
end

#to_h(arr_sep: " ", key_sep: ":") ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ext/string.rb', line 19

def to_h(arr_sep: " ", key_sep: ":")
  split(arr_sep).each_with_object({}) do |e, hsh|
    key, value = e.split(key_sep)

    hsh[key] = value
  end
end

#to_nObject



27
28
29
30
31
# File 'lib/ext/string.rb', line 27

def to_n
  return to_f if self =~ /[0-9]+\.[0-9]+/

  to_i
end