Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:17:44+00:00 2026-06-05T02:17:44+00:00

I have this 2D array: arr = [[1,2],[3,4]] I usually do: CSV.open(file) do |csv|

  • 0

I have this 2D array:

arr = [[1,2],[3,4]]

I usually do:

CSV.open(file) do |csv| 
  arr.each do |row| 
    csv << row
  end
end

Is there any easier or direct way of doing it other than adding row by row?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:17:45+00:00Added an answer on June 5, 2026 at 2:17 am

    Assuming that your array is just numbers (no strings that potentially have commas in them) then:

    File.open(file,'w'){ |f| f << arr.map{ |row| row.join(',') }.join('\n') }
    

    One enormous string blatted to disk, with no involving the CSV library.

    Alternatively, using the CSV library to correctly escape each row:

    require 'csv'
    # #to_csv automatically appends '\n', so we don't need it in #join
    File.open(file,'w'){ |f| f << arr.map(&:to_csv).join } 
    

    If you have to do this often and the code bothers you, you could monkeypatch it in:

    class CSV
      def self.dump_array(array,path,mode="rb",opts={})
        open(path,mode,opts){ |csv| array.each{ |row| csv << row } }
      end
    end
    CSV.dump_array(arr,file)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this: arr = [] arr[0] = "ab" arr[1] =
Noob question here! I have an array with hashes that looks like this: arr
I have this PHP code - <?php for($i=1; $i<=1000; $i++) { $array=array(); $array[$i]=54*$i; $arr=array($array[$i].,);
i have this code : NSMutableArray *arr = [NSMutableArray array]; for (int i =
I have this array: var arr = []; arr.push({name:k1, value:abc}); arr.push({name:k2, value:hi}); arr.push({name:k3, value:oa});
If I have for example this array-variable: var arr=[ [1,2,3] [4,5,6] ] How can
I have this code: val arr: Array[Int] = ... val largestIndex = { var
I have this array, in which each index contains an object literal. All of
Right now, I have this code where $obj_arr maybe contain array and an object.
I have this array A = array([[-0.49740509, -0.48618909, -0.49145315], [-0.48959259, -0.48618909, -0.49145315], [-0.49740509, -0.47837659,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.