(Ruby) how to insert a pair into an array -
i have 2d array have name , slug of each school in database, pairs. want start array off empty , add each school one-by-one it.
this have tried:
<% schoolselect = [] %> <% @schools.each { |x| schoolselect += [x.name, x.slug] } %>
however, adds name , slug of school array in session, instead of two-dimensional.
use <<
instead of +=
:
schoolselect = [] @schools.each { |x| schoolselect << [x.name, x.slug] }
or better use ruby idiom map
:
schoolselect = @schools.map { |s| [s.name, s.slug] }
this works, because map
returns array.
Comments
Post a Comment