Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge arrays in Ruby/Rails

How can I merge two arrays? Something like this:

@movie = Movie.first()
@options = Movie.order("RANDOM()").first(3).merge(@movie)

But it doesn't work.

In @options I need an array with four elements includes @movie.

like image 881
Croaton Avatar asked Sep 01 '25 18:09

Croaton


1 Answers

Like this?

⚡️ irb
2.2.2 :001 > [1,2,3] + [4,5,6]
 => [1, 2, 3, 4, 5, 6] 

But you don't have 2 arrays.

You could do something like:

@movie = Movie.first()
@options = Movie.order("RANDOM()").first(3).to_a << @movie
like image 133
Nick Veys Avatar answered Sep 04 '25 07:09

Nick Veys