5 # string.clear -> string
18 # str.lstrip -> new_str
20 # Returns a copy of <i>str</i> with leading whitespace removed. See also
21 # <code>String#rstrip</code> and <code>String#strip</code>.
23 # " hello ".lstrip #=> "hello "
24 # "hello".lstrip #=> "hello"
29 a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
30 (z >= 0) ? self[a..z] : ""
35 # str.rstrip -> new_str
37 # Returns a copy of <i>str</i> with trailing whitespace removed. See also
38 # <code>String#lstrip</code> and <code>String#strip</code>.
40 # " hello ".rstrip #=> " hello"
41 # "hello".rstrip #=> "hello"
46 z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
47 (z >= 0) ? self[a..z] : ""
52 # str.strip -> new_str
54 # Returns a copy of <i>str</i> with leading and trailing whitespace removed.
56 # " hello ".strip #=> "hello"
57 # "\tgoodbye\r\n".strip #=> "goodbye"
62 a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
63 z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
64 (z >= 0) ? self[a..z] : ""
69 # str.lstrip! -> self or nil
71 # Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
72 # change was made. See also <code>String#rstrip!</code> and
73 # <code>String#strip!</code>.
75 # " hello ".lstrip #=> "hello "
76 # "hello".lstrip! #=> nil
79 raise FrozenError, "can't modify frozen String" if frozen?
81 (s == self) ? nil : self.replace(s)
86 # str.rstrip! -> self or nil
88 # Removes trailing whitespace from <i>str</i>, returning <code>nil</code> if
89 # no change was made. See also <code>String#lstrip!</code> and
90 # <code>String#strip!</code>.
92 # " hello ".rstrip #=> " hello"
93 # "hello".rstrip! #=> nil
96 raise FrozenError, "can't modify frozen String" if frozen?
98 (s == self) ? nil : self.replace(s)
103 # str.strip! -> str or nil
105 # Removes leading and trailing whitespace from <i>str</i>. Returns
106 # <code>nil</code> if <i>str</i> was not altered.
109 raise FrozenError, "can't modify frozen String" if frozen?
111 (s == self) ? nil : self.replace(s)
116 # str.casecmp(other_str) -> -1, 0, +1 or nil
118 # Case-insensitive version of <code>String#<=></code>.
120 # "abcdef".casecmp("abcde") #=> 1
121 # "aBcDeF".casecmp("abcdef") #=> 0
122 # "abcdef".casecmp("abcdefg") #=> -1
123 # "abcdef".casecmp("ABCDEF") #=> 0
126 self.downcase <=> str.__to_str.downcase
133 # str.casecmp?(other) -> true, false, or nil
135 # Returns true if str and other_str are equal after case folding,
136 # false if they are not equal, and nil if other_str is not a string.
139 c = self.casecmp(str)
145 raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
149 [ slice(0, n), sep, slice(m, size - m) ]
156 raise TypeError, "type mismatch: #{sep.class} given" unless sep.is_a? String
160 [ slice(0, n), sep, slice(m, size - m) ]
168 # str.slice!(fixnum) -> new_str or nil
169 # str.slice!(fixnum, fixnum) -> new_str or nil
170 # str.slice!(range) -> new_str or nil
171 # str.slice!(other_str) -> new_str or nil
173 # Deletes the specified portion from <i>str</i>, and returns the portion
176 # string = "this is a string"
177 # string.slice!(2) #=> "i"
178 # string.slice!(3..6) #=> " is "
179 # string.slice!("r") #=> "r"
180 # string #=> "thsa sting"
182 def slice!(arg1, arg2=nil)
183 raise FrozenError, "can't modify frozen String" if frozen?
184 raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
186 if !arg1.nil? && !arg2.nil?
188 idx += self.size if arg1 < 0
189 if idx >= 0 && idx <= self.size && arg2 > 0
190 str = self[idx, arg2]
196 if arg1.kind_of?(Range)
199 beg += self.size if beg < 0
200 ed += self.size if ed < 0
201 ed -= 1 if arg1.exclude_end?
203 elsif arg1.kind_of?(String)
207 idx += self.size if arg1 < 0
208 validated = true if idx >=0 && arg1 < self.size
216 unless str.nil? || str == ""
217 if !arg1.nil? && !arg2.nil?
218 idx = arg1 >= 0 ? arg1 : self.size+arg1
219 str2 = self[0...idx] + self[idx+arg2..-1].to_s
221 if arg1.kind_of?(Range)
222 idx = beg >= 0 ? beg : self.size+beg
223 idx2 = ed>= 0 ? ed : self.size+ed
224 str2 = self[0...idx] + self[idx2+1..-1].to_s
225 elsif arg1.kind_of?(String)
226 idx = self.index(arg1)
227 str2 = self[0...idx] + self[idx+arg1.size..-1] unless idx.nil?
229 idx = arg1 >= 0 ? arg1 : self.size+arg1
230 str2 = self[0...idx] + self[idx+1..-1].to_s
233 self.replace(str2) unless str2.nil?
240 # str.insert(index, other_str) -> str
242 # Inserts <i>other_str</i> before the character at the given
243 # <i>index</i>, modifying <i>str</i>. Negative indices count from the
244 # end of the string, and insert <em>after</em> the given character.
245 # The intent is insert <i>aString</i> so that it starts at the given
248 # "abcd".insert(0, 'X') #=> "Xabcd"
249 # "abcd".insert(3, 'X') #=> "abcXd"
250 # "abcd".insert(4, 'X') #=> "abcdX"
251 # "abcd".insert(-3, 'X') #=> "abXcd"
252 # "abcd".insert(-1, 'X') #=> "abcdX"
266 # str.ljust(integer, padstr=' ') -> new_str
268 # If <i>integer</i> is greater than the length of <i>str</i>, returns a new
269 # <code>String</code> of length <i>integer</i> with <i>str</i> left justified
270 # and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
272 # "hello".ljust(4) #=> "hello"
273 # "hello".ljust(20) #=> "hello "
274 # "hello".ljust(20, '1234') #=> "hello123412341234123"
275 def ljust(idx, padstr = ' ')
276 raise ArgumentError, 'zero width padding' if padstr == ''
277 return self if idx <= self.size
278 pad_repetitions = (idx / padstr.length).ceil
279 padding = (padstr * pad_repetitions)[0...(idx - self.length)]
285 # str.rjust(integer, padstr=' ') -> new_str
287 # If <i>integer</i> is greater than the length of <i>str</i>, returns a new
288 # <code>String</code> of length <i>integer</i> with <i>str</i> right justified
289 # and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
291 # "hello".rjust(4) #=> "hello"
292 # "hello".rjust(20) #=> " hello"
293 # "hello".rjust(20, '1234') #=> "123412341234123hello"
294 def rjust(idx, padstr = ' ')
295 raise ArgumentError, 'zero width padding' if padstr == ''
296 return self if idx <= self.size
297 pad_repetitions = (idx / padstr.length).ceil
298 padding = (padstr * pad_repetitions)[0...(idx - self.length)]
304 self.split('').each do |i|
313 def each_char(&block)
314 return to_enum :each_char unless block
316 split('').each do |i|
322 def codepoints(&block)
326 self.split('').each do|x|
331 self.split('').map{|x| x.ord}
334 alias each_codepoint codepoints
338 # str.prepend(other_str) -> str
340 # Prepend---Prepend the given string to <i>str</i>.
343 # a.prepend("hello ") #=> "hello world"
344 # a #=> "hello world"
352 # string.lines -> array of string
353 # string.lines {|s| block} -> array of string
355 # Returns strings per line;
358 # a.lines #=> ["abc\n", "def"]
360 # If a block is given, it works the same as <code>each_line</code>.
373 # str.upto(other_str, exclusive=false) {|s| block } -> str
374 # str.upto(other_str, exclusive=false) -> an_enumerator
376 # Iterates through successive values, starting at <i>str</i> and
377 # ending at <i>other_str</i> inclusive, passing each value in turn to
378 # the block. The <code>String#succ</code> method is used to generate
379 # each value. If optional second argument exclusive is omitted or is false,
380 # the last value will be included; otherwise it will be excluded.
382 # If no block is given, an enumerator is returned instead.
384 # "a8".upto("b6") {|s| print s, ' ' }
385 # for s in "a8".."b6"
391 # a8 a9 b0 b1 b2 b3 b4 b5 b6
392 # a8 a9 b0 b1 b2 b3 b4 b5 b6
394 # If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
395 # both are recognized as decimal numbers. In addition, the width of
396 # string (e.g. leading zeros) is handled appropriately.
398 # "9".upto("11").to_a #=> ["9", "10", "11"]
399 # "25".upto("5").to_a #=> []
400 # "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"]
401 def upto(max, exclusive=false, &block)
402 return to_enum(:upto, max, exclusive) unless block
403 raise TypeError, "no implicit conversion of #{max.class} into String" unless max.kind_of? String
408 if len == 1 and maxlen == 1
412 break if exclusive and c == e
418 # both edges are all digits
422 if (bi > 0 or bi == "0"*len) and (ei > 0 or ei == "0"*maxlen)
424 break if exclusive and bi == ei
426 s = s.rjust(len, "0") if s.length < len
436 break if exclusive and n == 0