3

Ok, I have a hash which contains several properties. I wanted certain properties of this hash to be added to a CSV file.

Here's what I've written:

    require 'csv'
    require 'curb'
    require 'json'

    arr = []

    CSV.foreach('test.csv') do | row |
        details = []
        details << result['results'][0]['formatted_address']
        result['results'][0]['address_components'].each do | w |
            details << w['short_name']
        end
        arr << details
    end

    CSV.open('test_result.csv', 'w') do | csv |
        arr.each do | e |
            csv << [e]
        end
      end
    end

All works fine apart from the fact the I get each like so:

["something", "300", "something", "something", "something", "something", "something", "GB", "something"]

As an array, which I do not want. I want each element of the array in a new column. The problem is that I do not know how many items I'll have otherwise I could something like this:

CSV.open('test_result.csv', 'w') do | csv |
        arr.each do | e |
            csv << [e[0], e[1], ...]
        end
      end
    end

Any ideas?

3
  • 1
    Why not do csv << e ? Commented Jul 7, 2015 at 12:32
  • 1
    It is indeed! Do you want to add as answer so I can mark it appropriately? Cheers Commented Jul 7, 2015 at 13:52
  • Yeah sure :3. Glad it helped. Commented Jul 7, 2015 at 13:57

1 Answer 1

3

Change csv << [e] to csv << e.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.