Rails: how do I add element to an array? - IT-C@FE
×
Masterpro Nivo Slider (06 фев 2023)

Это форк Vinaora Nivo Slider, пришлось переименовать, в силу требования JED. Даже старую версию качать можно было только с варезных сайтов, нашпигованную троянами. Зачем оно такое, согласитесь.

Вопрос Rails: how do I add element to an array?

Подробнее
6 года 3 нед. назад #1 от serge
serge создал тему: Rails: how do I add element to an array?
Create an array:
@array = [123, 456, 789, 012, 345]

Then you can push or shovel the new value in:
@array << element

or
@array.push(element)

Convert String to Array. Alternately if you want the numbers to remain strings for some reason, just convert the string into an array using the String#split method:
@array = "123,456,789,012,345".split(',') #=> ['123', '456', '789', '012', '345']

Just append to String. You could also just add the new value (as a string) to the existing string:
@array += ",#{element}"

So if element were set to 678, this would turn the integer into a string and add it to the existing string with a leading-comma.

If you want the values in your array to remain in a string, you can use the split method:
@array.split or @array.split(',')

The following lines add the element as the last element in the array:
array[array.length] = element
array += [element]
array << element
array.push(element)
array.append(element)

To add it at a specific position, use insert:
array.insert(position, element)

А я смогу! - А поглядим! - А я упрямый!

Пожалуйста Войти или Регистрация, чтобы присоединиться к беседе.

Работает на Kunena форум