K
K
kramidev2015-02-05 09:41:32
Ruby on Rails
kramidev, 2015-02-05 09:41:32

rails. How to add elements to an array in a hash?

Hi all.
There is a hash. The hash has an array like 'pictures'. How to add elements to an array?

hash = {'pictures' => [] }
someArray.each do |a|
   hash['pictures'].push(a) # не работает :/
end

=================== Everything worked out, I added some logic. thanks to all.
def check_docs(docs)
    if docs.nil?
      false
    else
      hash = {}
      docs.each do |doc|
        name = doc.original_filename.split('.').last
        if pic(name) != false
          hash['pictures'] = [] unless hash['pictures']
          hash['pictures'] << doc
        end
        if doc(name) != false
          hash['docs'] = [] unless hash['docs']
          hash['docs'] << doc
        end
      end
      if hash.size > 0
        hash
      else
        false
      end
    end
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-02-05
@kramidev

I checked your solution and it works for me. Here is an alternative syntax:

hash = {'pictures' => [] }
someArray.each do |a|
   hash['pictures'] << a 
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question