Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialOzgur Canibeyaz
3,126 PointsAdd multiple item to an array
How do we add more than one item to the array instead of adding one by one ? I tried doing something like my_cars = Array.new car1 = "Ferrari" car2 = "Bugatti" my_cars.push %W(#{car1} #{car2})
This actually compiles but it creates a separate array within the array.
1 Answer
Maciej Czuchnowski
36,441 PointsYou can do it this way for example:
my_array = []
my_array << 'bugatti' << 'ferrari' << 'opel' << 'polonez'
And keep adding stuff using <<
Or like this:
%W(Toyota Honda).each do |item|
my_array << item
end
Ozgur Canibeyaz
3,126 PointsOzgur Canibeyaz
3,126 Pointsthanks