re: This is Weird VIEW POST


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.)

code of conduct - report abuse