Module: StringNumericHelper

Included in:
String
Defined in:
lib/rubyhelper/string_numeric.rb

Instance Method Summary collapse

Instance Method Details

#get_1float(sign = true) ⇒ String

get only the digits and + - . , symbols in the string from the first group of digits you should see also #get_float and #get_1int

Parameters:

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:



116
117
118
119
120
# File 'lib/rubyhelper/string_numeric.rb', line 116

def get_1float(sign = true)
  return self.match(/([\d\.,\-\+]*\d[\d\.,\-\+]*)/).to_a[1].to_s.get_float(sign) if sign == true
  return self.match(/([\d\.,\-]*\d[\d\.,\-]*)/).to_a[1].to_s.get_float(sign) if sign == :less
  return self.match(/([\d\.,]*\d[\d\.,]*)/).to_a[1].to_s.get_float(sign)
end

#get_1float!(sign = true) ⇒ String

Returns:



125
126
127
# File 'lib/rubyhelper/string_numeric.rb', line 125

def get_1float!(sign = true)
  return self.replace(self.get_1float(sign))
end

#get_1floats(sep = ' ', sign = true) ⇒ Array of String

get all floats groups if sep is a sign and the param sign == true, then theses signs will be splited first see also #get_float and #get_ints

Parameters:

  • sep (String or Regexp) (defaults to: ' ')

    separator

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:

Raises:

  • ArgumentError if sep is not a String



150
151
152
153
# File 'lib/rubyhelper/string_numeric.rb', line 150

def get_1floats(sep = ' ', sign = true)
  raise ArgumentError, "sep must be a String" unless sep.is_a? String or sep.is_a? Regexp
  return self.split(sep).map{|e| e.get_1float(sign)}
end

#get_1int(sign = true, sep = '') ⇒ String

It take every digits (and sign - see param sign) of the first group of digits and return them. For exemple, “+3 4”.get_1int will return “+3” you should see also #get_int #get_ints and #get_1float

Parameters:

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

  • sep (String) (defaults to: '')

Returns:

Raises:

  • ArgumentError if sep not a String



49
50
51
52
53
54
# File 'lib/rubyhelper/string_numeric.rb', line 49

def get_1int(sign = true, sep = '')
  raise ArgumentError, "sep must be a String" unless sep.is_a? String
  return self.delete(sep).match(/([\-\+]?\d+)/).to_a[1].to_s.get_int(sign) if sign == true
  return self.delete(sep).match(/(\-?\d+)/).to_a[1].to_s.get_int(sign) if sign == :less
  return self.delete(sep).match(/(\d+)/).to_a[1].to_s.get_int(sign)
end

#get_1int!(sign = true, sep = '') ⇒ String

see #get_1int

Returns:

Raises:

  • ArgumentError if sep not a String



60
61
62
# File 'lib/rubyhelper/string_numeric.rb', line 60

def get_1int!(sign = true, sep = '')
  return self.replace(self.get_1int(sign, sep))
end

#get_1ints(firstsep = ' ', sign = true, sep = '') ⇒ Array of String

get all integer groups into an array of string (split from self) if sep is a sign and the param sign == true, then theses signs will be splited first see also #get_ints and #get_1int

Parameters:

  • firstsep (String or Regexp) (defaults to: ' ')

    separator

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

  • sep (String) (defaults to: '')

    separator for #get_1int

Returns:

Raises:

  • ArgumentError if firstsep is not a String or if sep is not a String/Regexp



87
88
89
90
# File 'lib/rubyhelper/string_numeric.rb', line 87

def get_1ints(firstsep = ' ', sign = true, sep = '')
  raise ArgumentError, "firstsep must be a String" unless firstsep.is_a? String or firstsep.is_a? Regexp
  return self.split(firstsep).map{|e| e.get_1int(sign, sep)}
end

#get_float(sign = true) ⇒ String

get every digits and + - . , symbols in the string you can also see #to_fi to turn the String into a Float see als #get_1float #get_floats and #get_int

Parameters:

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:



98
99
100
101
102
# File 'lib/rubyhelper/string_numeric.rb', line 98

def get_float(sign = true)
  return self.gsub(/[^\-\+\d\.\,]/, '') if sign == true
  return self.gsub(/[^\-\d\.\,]/, '') if sign == :less
  return self.gsub(/[^\d\.\,]/, '')
end

#get_float!(sign = true) ⇒ String

Returns:



107
108
109
# File 'lib/rubyhelper/string_numeric.rb', line 107

def get_float!(sign = true)
  return self.replace(self.get_float(sign))
end

#get_floats(sep = ' ', sign = true) ⇒ Array of String

get all digit and symboles from a splited string if sep is a sign and the param sign == true, then theses signs will be splited first see also #get_1float and #get_floats

Parameters:

  • sep (String or Regexp) (defaults to: ' ')

    separator

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:

Raises:

  • ArgumentError if sep is not a String



137
138
139
140
# File 'lib/rubyhelper/string_numeric.rb', line 137

def get_floats(sep = ' ', sign = true)
  raise ArgumentError, "sep must be a String" unless sep.is_a? String or sep.is_a? Regexp
  return self.split(sep).map{|e| e.get_float(sign)}
end

#get_int(sign = true) ⇒ String

It take every digits (and sign - see param sign) and return them you should see also #get_float

Parameters:

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:



28
29
30
31
32
# File 'lib/rubyhelper/string_numeric.rb', line 28

def get_int(sign = true)
  return self.gsub(/[^\-\+\d]/, '') if sign == true
  return self.gsub(/[^\-\d]/, '') if sign == :less
  return self.gsub(/[^\d]/, '')
end

#get_int!(sign = true) ⇒ String

see #get_int

Returns:



37
38
39
# File 'lib/rubyhelper/string_numeric.rb', line 37

def get_int!(sign = true)
  return self.replace(self.get_int(sign))
end

#get_ints(sep = ' ', sign = true) ⇒ Array of String

get all digits into an array of string (split from self) if sep is a sign and the param sign == true, then theses signs will be splited first (if sep == ‘-’ for example, “1-1”.get_ints will return [“1”, “1”] see also #get_floats and #get_int

Parameters:

  • sep (String or Regexp) (defaults to: ' ')

    separator

  • sign (true or false or :less) (defaults to: true)

    if true, keep the - and + signs, if :less, only keep -

Returns:

Raises:

  • ArgumentError if sep is not a String



73
74
75
76
# File 'lib/rubyhelper/string_numeric.rb', line 73

def get_ints(sep = ' ', sign = true)
  raise ArgumentError, 'sep must be a String or a Regexp' unless sep.is_a? String or sep.is_a? Regexp
  return self.split(sep).map{|e| e.get_int(sign)}
end

#ha2m2Float

transforme the string into an float in m² if containing “ha”

Returns:

  • (Float)


158
159
160
161
162
163
164
165
# File 'lib/rubyhelper/string_numeric.rb', line 158

def ha2m2
  v = self.get_1float
  return String.new if v.empty?
  m2_i = self.index(/m(2|²)/i)
  ha_i = self.index(/ha|hectar/i)
  return v.to_fi if ha_i.nil? or (not m2_i.nil? and m2_i < ha_i)
  return v.to_fi * 10_000
end

#to_fiFloat

improvement of to_f to count “,” caracter as “.”

Returns:

  • (Float)

    like Integer#to_f



8
9
10
# File 'lib/rubyhelper/string_numeric.rb', line 8

def to_fi
  return self.gsub(',', '.').to_f
end

#to_ii(char = ' ') ⇒ Integer

to_i with delimiter to remove Example : “12.000.000”.to_ii => 12000000

Parameters:

  • char (String) (defaults to: ' ')

    char to delete (default : ‘ ’)

Returns:

  • (Integer)

    like {Integer#to_i]

Raises:

  • (ArgumentError)

    If (param char) is not a String



18
19
20
21
# File 'lib/rubyhelper/string_numeric.rb', line 18

def to_ii(char=' ')
  raise ArgumentError, "Argument is not a String" unless char.is_a? String
  self.delete(char).to_i
end