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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 196 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
184 185 186 187 188 189 190 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 184 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.
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 152 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
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 169 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
234 235 236 237 238 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 234 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
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 218 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
243 244 245 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 243 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
261 262 263 264 265 266 267 268 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 261 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.
251 252 253 254 255 |
# File 'lib/linmeric/CnGal_new_classes.rb', line 251 def to_n return self.to_f if self.float? return self.to_i if self.integer? return 0 end |