Method: String#insert

Defined in:
ext/enterprise_script_service/mruby/mrbgems/mruby-string-ext/mrblib/string.rb

#insert(idx, str) ⇒ Object

call-seq:

   str.insert(index, other_str)   -> str

Inserts <i>other_str</i> before the character at the given
<i>index</i>, modifying <i>str</i>. Negative indices count from the
end of the string, and insert <em>after</em> the given character.
The intent is insert <i>aString</i> so that it starts at the given
<i>index</i>.

   "abcd".insert(0, 'X')    #=> "Xabcd"
   "abcd".insert(3, 'X')    #=> "abcXd"
   "abcd".insert(4, 'X')    #=> "abcdX"
   "abcd".insert(-3, 'X')   #=> "abXcd"
   "abcd".insert(-1, 'X')   #=> "abcdX"


254
255
256
257
258
259
260
261
262
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-string-ext/mrblib/string.rb', line 254

def insert(idx, str)
  if idx == -1
    return self << str
  elsif idx < 0
    idx += 1
  end
  self[idx, 0] = str
  self
end