push in array with auto-create in ruby

Viewed 1624

i want to know if it's possible to push in array with auto-creation of array if doesn't exist already, like in PHP:

$toto[] = 'titi';

if $toto is not already defined, it will create array and push 'titi' into. If already exist, it will just push.

in Ruby i have to do:

toto ||= []
toto.push('titi')

It's possible to do this in one line?

Because if i have an loop, it will test "||=" for nothing except the first time:

Person.all.each do |person|    
   toto ||= [] #with 1 billion of person, this line is useless 999 999 999 times...
   toto.push(person.name)

have you a better solution?

thx.

5 Answers
Related