Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
String#[]=
string[index] = new_string
string[start, length] = new_string
string[range] = new_string
string[regexp, capture = 0] = new_string
string[substring] = new_string
string.c
View on GitHub ↗
static VALUE
rb_str_aset_m(int argc, VALUE *argv, VALUE str)
{
if (argc == 3) {
if (RB_TYPE_P(argv[0], T_REGEXP)) {
rb_str_subpat_set(str, argv[0], argv[1], argv[2]);
}
else {
rb_str_update(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
}
return argv[2];
}
rb_check_arity(argc, 2, 3);
return rb_str_aset(str, argv[0], argv[1]);
}
Replaces all, some, or none of the contents of self; returns new_string. See String Slices.
A few examples:
Example
s = 'foo' s[2] = 'rtune' # => "rtune" s # => "fortune" s[1, 5] = 'init' # => "init" s # => "finite" s[3..4] = 'al' # => "al" s # => "finale" s[/e$/] = 'ly' # => "ly" s # => "finally" s['lly'] = 'ncial' # => "ncial" s # => "financial"