Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/linmeric/CnGal_new_classes.rb
Overview
Overload of String class
- Author
-
Massimiliano Dal Mas ([email protected])
- License
-
Distributed under MIT license
Direct Known Subclasses
Instance Method Summary collapse
-
#-(str) ⇒ Object
Subtracts the substring to the main.
-
#compact ⇒ Object
Deletes spaces from a string.
-
#contain?(str) ⇒ Boolean
Checks if the string contains at least a character of the given one.
-
#contain_all?(str) ⇒ Boolean
Checks if the string contains all the characters of the given one.
-
#float? ⇒ Boolean
Checks if the strings represents a ‘Float` value.
-
#integer? ⇒ Boolean
Checks if the strings represents a ‘Fixnum` value.
-
#number? ⇒ Boolean
Checks if the string represents a number.
-
#remove(index) ⇒ Object
Removes a character at the given index.
-
#to_n ⇒ Object
Converts a string to a number.
Instance Method Details
#-(str) ⇒ Object
Subtracts the substring to the main
-
argument: substring
-
returns: new string
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 184 def -(str) myStr = str.to_s temp = '' i = 0 if self.include? myStr then while i < self.length do if self[i...(i + myStr.size)] == myStr then i += myStr.size else temp += self[i] i += 1 end end else return self end return temp end |
#compact ⇒ Object
Deletes spaces from a string
-
returns: new string
172 173 174 175 176 177 178 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 172 def compact() nstr = '' self.each_char do |ch| nstr += ch if ch != ' ' end return nstr end |
#contain?(str) ⇒ Boolean
Checks if the string contains at least a character of the given one.
-
argument: string for the checking
-
returns:
trueif a character in common is found;falseelse.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 140 def contain?(str) raise ArgumentError, '' unless str.is_a? String str.each_char do |ch| return true if self.include? ch end return false rescue ArgumentError str = str.to_s retry end |
#contain_all?(str) ⇒ Boolean
Checks if the string contains all the characters of the given one.
-
argument: string for the checking
-
returns:
trueif the string contins all the chars of the argument;
false else
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 157 def contain_all?(str) raise ArgumentError, '' unless str.is_a? String str.each_char do |ch| return false if not self.include? ch end return true rescue ArgumentError str = str.to_s retry end |
#float? ⇒ Boolean
Checks if the strings represents a ‘Float` value
-
returns:
trueif the string represents a ‘Float`;falseelse
222 223 224 225 226 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 222 def float? pat = /^[-+]?[1-9]([0-9]*)?\.[0-9]+$/ return true if self=~pat return false end |
#integer? ⇒ Boolean
Checks if the strings represents a ‘Fixnum` value
-
returns:
trueif the string represents a ‘Fixnum`;falseelse
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 206 def integer? [ # In descending order of likeliness: /^[-+]?[1-9]([0-9]*)?$/, # decimal /^0[0-7]+$/, # octal /^0x[0-9A-Fa-f]+$/, # hexadecimal /^0b[01]+$/, # binary /[0]/ # zero ].each do |match_pattern| return true if self =~ match_pattern end return false end |
#number? ⇒ Boolean
Checks if the string represents a number
-
*returns:
trueif the string represents a number;falseelse
231 232 233 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 231 def number? (self.integer? or self.float?) ? (return true) : (return false) end |
#remove(index) ⇒ Object
Removes a character at the given index
-
argument: index of the character (‘Fixnum`)
-
returns: new string
249 250 251 252 253 254 255 256 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 249 def remove(index) return self if index < 0 n_str = '' for i in 0...self.size n_str += self[i] unless i == index end return n_str end |
#to_n ⇒ Object
Converts a string to a number
-
returns: ‘Float` value if the string represents a float
‘Fixnum` value if the string represents a fixnum; 0 else.
239 240 241 242 243 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 239 def to_n return self.to_f if self.float? return self.to_i if self.integer? return 0 end |