Skip to content
loading...

This is Weird

burdettelamar profile image Burdette Lamar github logo ・1 min read

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]
twitter logo DISCUSS (2)
Discussion
markdown guide
 

I think what's happening is:

  • The elements covered by the range are replaced by the assigned value
  • Negative indexes are used as offsets from the end of the array. So a[-2] is the same as a[a.length - 2], and a[1..-2] is the same as a[1..a.length-2]
  • When the end index of a range is <= the start index, no values are replaced
a = [:foo, 'bar', baz = 2]
a[1..-1]  # 1..-1 is the same as 1..2 because a.length is 3
# => ["bar", 2]
a[1..-1] = 'foo'  # this is the same as removing "bar" and 2 from a and replacing them with "foo"
# => [:foo, "test"]
 

Yes. Good interpretation. (But still weird, I think.)

Classic DEV Post from Aug 4 '19

You're not worth hiring unless...

Burdette Lamar profile image
Started out teaching English at Embry-Riddle. Graded 10,000 essays. Lesson learned. Became a mathematics teacher. Discovered computing. Never looked back.