For Array a
, I'm doing a[range] = obj
. Here's what I think I see:
If range.end
is negative, assigns obj
at offset start
, retains range.end.abs-1
elements past that, and removes those beyond:
a = [:foo, 'bar', baz = 2]
a[1..-1] = 'foo' # => "foo"
a # => [:foo, "foo"]
a = [:foo, 'bar', baz = 2]
a[1..-2] = 'foo' # => "foo"
a # => [:foo, "foo", 2]
a = [:foo, 'bar', baz = 2]
a[1..-3] = 'foo' # => "foo"
a # => [:foo, "foo", "bar", 2]
a = [:foo, 'bar', baz = 2]
a[1..-4] = 'foo' # => "foo"
a # => [:foo, "foo", "bar", 2]
Top comments (2)
I think what's happening is:
a[-2]
is the same asa[a.length - 2]
, anda[1..-2]
is the same asa[1..a.length-2]
<=
the start index, no values are replacedYes. Good interpretation. (But still weird, I think.)