Module: StringNumericHelper
- Included in:
- String
- Defined in:
- lib/rubyhelper/string_numeric.rb
Instance Method Summary collapse
-
#get_1float(sign = true) ⇒ String
get only the digits and + - .
-
#get_1float!(sign = true) ⇒ String
see #get_1float.
-
#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.
-
#get_1int(sign = true, sep = '') ⇒ String
It take every digits (and sign - see param sign) of the first group of digits and return them.
-
#get_1int!(sign = true, sep = '') ⇒ String
see #get_1int.
- #get_1ints(firstsep = ' ', sign = true, sep = '') ⇒ Array of String
-
#get_float(sign = true) ⇒ String
get every digits and + - .
-
#get_float!(sign = true) ⇒ String
see #get_float.
-
#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.
-
#get_int(sign = true) ⇒ String
It take every digits (and sign - see param sign) and return them you should see also #get_float.
-
#get_int!(sign = true) ⇒ String
see #get_int.
-
#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.
-
#ha2m2 ⇒ Float
transforme the string into an float in m² if containing “ha”.
-
#to_fi ⇒ Float
improvement of to_f to count “,” caracter as “.”.
-
#to_ii(char = ' ') ⇒ Integer
to_i with delimiter to remove Example : “12.000.000”.to_ii => 12000000.
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
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
see #get_1float
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
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
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
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
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
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
see #get_float
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
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
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
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
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 |
#ha2m2 ⇒ Float
transforme the string into an float in m² if containing “ha”
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_fi ⇒ Float
improvement of to_f to count “,” caracter as “.”
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
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 |