M
M
MisTFoR2021-09-24 17:32:29
ruby
MisTFoR, 2021-09-24 17:32:29

Why are arrays printed the way they are?

Hello.
I'm JUST starting to learn Ruby and ran into 1 question that got me interested.
I don't fully understand how arrays are displayed. I have this code:

names = Array["George", "Bob", "Alex"]
names[0] = "Diana"
puts(names[0, 2])

In fact, Diana and Alex should be displayed. But I have Diana and Bob displayed. Why? If the account Alex - 2.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2021-09-24
@0xD34F

Not specific indexes are specified through a comma, but an interval of indexes is set. The first number is the starting index, the second is how many elements to take. Read the documentation .

C
cryogenic_phoenix, 2021-12-09
@cryogenic_phoenix

Array["George", "Bob", "Alex"]
George - 1 index
bob - 2
alex - 3
diana 0
from 0 to 2
0 - diana, 2 -
ruby ​​bean is not pascal, here is the interval from which index to report
like this

names = Array["George", "Bob", "Alex"]
names[0] = "Diana"
puts(names[0,  3])

there is no problem until the problem becomes a real problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question