+ 4
Ruby - Array substitution
What the second instruction does? Why is the output "mykeyholder"? Doesn't it substitute in position 1 "bottle" also? items = ["place", "key", "holder"] items[1,0] = ["bottle", "my"] puts items[-3], items[-2], items[-1]
2 Antworten
+ 5
Initial items in the array
items=["place", "key", "holder"]
Now new items are needed to add up to the given values in the array with an instruction which says items [1,0]. This implies that the new set of values to be added up to the array should be placed at index position 1 and should include everything in the array. So every item in the new array is included.
The new array becomes..
["place","bottle","my","key",
"holder"]
Now the output for item[-3]=my
item[-2]=key
item[-1]=holder
So the final output = "mykeyholder".
+ 3
items = ["place", "key", "holder"]
items[1,0] = ["bottle", "my"]
# [1,0] indicates that ["bottle", "my"] will insert after "place"
# Now - items = ["place", "bottle", "my" , "key", "holder"]
puts items[-3], items[-2], items[-1]
# items[-3] = "my"; items[-2] = "key"; items[-1] = "holder"