Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/kirinnee_core/string.rb
Instance Method Summary collapse
-
#*(x) ⇒ String
Repeats the string x number of times Does not modify the original string Input is absolute-d.
-
#back(x) ⇒ String
Takes the last x characters of the string Does not modify the original string.
-
#back!(x) ⇒ String
Takes the last x characters of the string Modify the original string.
-
#count_occurrences(search) ⇒ Integer
Counts the number of times a string appears.
-
#omit(x) ⇒ String
Omits the last x characters of the string Does not modify the original string.
-
#omit!(x) ⇒ String
Omits the last x characters of the string Modifies the original string.
-
#remove(search) ⇒ String
Removes all instance of the word Does not modify the original string.
-
#remove!(search) ⇒ String
Removes all instance of the word Modifies the original string.
-
#remove_char_at(x) ⇒ Object
Removes the character at the index Does not modify the original string.
-
#remove_char_at!(x) ⇒ Object
Removes the character at the index Modifies the original string.
-
#repeat(x) ⇒ String
Repeats the string x number of times Does not modify the original string Input is absolute-d.
-
#repeat!(x) ⇒ String
Repeats the string x number of times Modifies the original string Input is absolute-d.
-
#replace_all(search, target) ⇒ string
Replaces the search string with the target string.
-
#replace_all!(search, target) ⇒ string
Replaces the search string with the target string.
-
#skip(x) ⇒ String
Skips the first x characters of the string Does not modify the original string.
-
#skip!(x) ⇒ String
Skips the first x characters of the string Modifies the original string.
-
#take(x) ⇒ String
Takes the first x characters of the string Does not modify original string.
-
#take!(x) ⇒ String
Takes the first x characters of the string Modifies original string.
-
#without(unwanted) ⇒ String
Remove any instance of the words in the array in order Does not modify the original string.
-
#without!(unwanted) ⇒ String
Remove any instance of the words in the array in order Modifies the original string.
Instance Method Details
#*(x) ⇒ String
Repeats the string x number of times Does not modify the original string Input is absolute-d
“Hello ”.repeat 3 # => “Hello Hello Hello ” “Hello ”.repeat -3 # => “Hello Hello Hello ”
264 265 266 |
# File 'lib/kirinnee_core/string.rb', line 264 def *(x) repeat x end |
#back(x) ⇒ String
Takes the last x characters of the string Does not modify the original string
“Singapore”.back 4 #=> “pore” “Singapore”.back 100 # =>“Singapore” “Singapore”.back 0 # => “”
122 123 124 125 126 127 |
# File 'lib/kirinnee_core/string.rb', line 122 def back(x) if x > self.length return self end self[self.length - x..-1] end |
#back!(x) ⇒ String
Takes the last x characters of the string Modify the original string
“Singapore”.back! 4 #=> “pore” “Singapore”.back! 100 # =>“Singapore” “Singapore”.back! 0 # => “”
138 139 140 |
# File 'lib/kirinnee_core/string.rb', line 138 def back!(x) replace(back x) end |
#count_occurrences(search) ⇒ Integer
Counts the number of times a string appears
“Hello”.count_occurrences “l” # => 2 “one day one night”.count_occurrences “one” # => 2 “one day one night”.count_occurrences “day” # => 1 “one day one night”.count_occurrences “morning” # => 0 “one day one night one”.count_occurrences “one” # => 3
278 279 280 |
# File 'lib/kirinnee_core/string.rb', line 278 def count_occurrences(search) (length - remove(search).length).abs / search.length end |
#omit(x) ⇒ String
Omits the last x characters of the string Does not modify the original string
“Singapore”.omit 5 # => “Sing” “Singapore”.omit 100 # => “” “Singapore”.omit 0 # => “Singapore”
96 97 98 |
# File 'lib/kirinnee_core/string.rb', line 96 def omit(x) self[0..-(x + 1)] end |
#omit!(x) ⇒ String
Omits the last x characters of the string Modifies the original string
“Singapore”.omit 5 # => “Sing” “Singapore”.omit 100 # => “” “Singapore”.omit 0 # => “Singapore”
109 110 111 |
# File 'lib/kirinnee_core/string.rb', line 109 def omit!(x) replace(omit x) end |
#remove(search) ⇒ String
Removes all instance of the word Does not modify the original string
“a=>b=>c”.remove “=>” # => “abc” “a,b,c”.remove “,” # => “abc” “a,b,c”.remove “a” # => “,b,c”
190 191 192 |
# File 'lib/kirinnee_core/string.rb', line 190 def remove(search) replace_all(search, "") end |
#remove!(search) ⇒ String
Removes all instance of the word Modifies the original string
“a=>b=>c”.remove! “=>” # => “abc” “a,b,c”.remove! “,” # => “abc” “a,b,c”.remove! “a” # => “,b,c”
203 204 205 |
# File 'lib/kirinnee_core/string.rb', line 203 def remove!(search) replace remove search end |
#remove_char_at(x) ⇒ Object
Removes the character at the index Does not modify the original string
Returns without removing characters if index or negative index exceeds length of string
“Hey! scent!”.remove_char_at 0 # => “ey! scent!” “Hey! scent!”.remove_char_at 3 # => “Hey scent!” “Hey! scent!”.remove_char_at -1 # => “Hey! scent” “Hey! scent!”.remove_char_at -6 #=> “Hey! cent!” “Hey! scent!”.remove_char_at 100 # => “Hey! scent!” “Hey! scent!”.remove_char_at -100 # => “Hey! scent!”
157 158 159 160 |
# File 'lib/kirinnee_core/string.rb', line 157 def remove_char_at(x) i = x + (x < 0 ? length : 0) i < 0 ? self : take(i) + skip(i + 1) end |
#remove_char_at!(x) ⇒ Object
Removes the character at the index Modifies the original string
Returns without removing characters if index or negative index exceeds length of string
“Hey! scent!”.remove_char_at! 0 # => “ey! scent!” “Hey! scent!”.remove_char_at! 3 # => “Hey scent!” “Hey! scent!”.remove_char_at! -1 # => “Hey! scent” “Hey! scent!”.remove_char_at! -6 #=> “Hey! cent!” “Hey! scent!”.remove_char_at! 100 # => “Hey! scent!” “Hey! scent!”.remove_char_at! -100 # => “Hey! scent!”
177 178 179 |
# File 'lib/kirinnee_core/string.rb', line 177 def remove_char_at!(x) replace remove_char_at x end |
#repeat(x) ⇒ String
Repeats the string x number of times Does not modify the original string Input is absolute-d
“Hello ”.repeat 3 # => “Hello Hello Hello ” “Hello ”.repeat -3 # => “Hello Hello Hello ”
238 239 240 |
# File 'lib/kirinnee_core/string.rb', line 238 def repeat(x) Array.new(x.abs, self).reduce("", &:+) end |
#repeat!(x) ⇒ String
Repeats the string x number of times Modifies the original string Input is absolute-d
“Hello ”.repeat! 3 # => “Hello Hello Hello ” “Hello ”.repeat! -3 # => “Hello Hello Hello ”
251 252 253 |
# File 'lib/kirinnee_core/string.rb', line 251 def repeat!(x) replace repeat(x) end |
#replace_all(search, target) ⇒ string
Replaces the search string with the target string. Returns a copy, does not modify original string
“a=>b=>c”.replace_all(“=>”,“-”) # => “a-b-c”
12 13 14 15 16 17 18 |
# File 'lib/kirinnee_core/string.rb', line 12 def replace_all (search, target) r = self.split(search).join target if self.end_with? search r += target end r end |
#replace_all!(search, target) ⇒ string
Replaces the search string with the target string. Modifies original string
“a=>b=>c”.replace_all!(“=>”,“-”) # => “a-b-c”
28 29 30 |
# File 'lib/kirinnee_core/string.rb', line 28 def replace_all!(search, target) replace(replace_all(search, target)) end |
#skip(x) ⇒ String
Skips the first x characters of the string Does not modify the original string
“Singapore”.skip 5 # => “pore” “Singapore”.skip 100 # => “” “Singapore”.skip 0 # => “Singapore”
67 68 69 70 71 72 |
# File 'lib/kirinnee_core/string.rb', line 67 def skip(x) if x > self.length return "" end self[x..-1] end |
#skip!(x) ⇒ String
Skips the first x characters of the string Modifies the original string
“Singapore”.skip! 5 # => “pore” “Singapore”.skip! 100 # => “” “Singapore”.skip! 0 # => “Singapore”
83 84 85 |
# File 'lib/kirinnee_core/string.rb', line 83 def skip!(x) replace(skip x) end |
#take(x) ⇒ String
Takes the first x characters of the string Does not modify original string
“Singapore”.take 4 # => “Sing” “Singapore”.take 100 # => “Singapore” “Singapore”.take 0 # => “”
41 42 43 |
# File 'lib/kirinnee_core/string.rb', line 41 def take(x) self[0...x] end |
#take!(x) ⇒ String
Takes the first x characters of the string Modifies original string
“Singapore”.take! 4 # => “Sing” “Singapore”.take! 100 # => “Singapore” “Singapore”.take! 0 # => “”
54 55 56 |
# File 'lib/kirinnee_core/string.rb', line 54 def take!(x) replace(self[0...x]) end |
#without(unwanted) ⇒ String
Remove any instance of the words in the array in order Does not modify the original string
“A=>B->C”.without [“=>”,“->”] # => “ABC”
214 215 216 |
# File 'lib/kirinnee_core/string.rb', line 214 def without(unwanted) unwanted.reduce(self, &:remove) end |
#without!(unwanted) ⇒ String
Remove any instance of the words in the array in order Modifies the original string
“A=>B->C”.without! [“=>”,“->”] # => “ABC”
225 226 227 |
# File 'lib/kirinnee_core/string.rb', line 225 def without!(unwanted) replace without unwanted end |