Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)

后端 未结 10 2452
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 10:39

I was going through the exercises in Ruby Koans and I was struck by the following Ruby quirk that I found really unexplainable:

array = [:peanut, :butter, :a         


        
10条回答
  •  猫巷女王i
    2020-11-22 11:09

    This does make sense

    You need to be able to assign to those slices, so they are defined in such a way that the beginning and the end of the string have working zero-length expressions.

    array[4, 0] = :sandwich
    array[0, 0] = :crunchy
    => [:crunchy, :peanut, :butter, :and, :jelly, :sandwich]
    

提交回复
热议问题