(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

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -