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

  • Home
  • SEARCH
  • 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 8180767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:50+00:00 2026-06-07T00:17:50+00:00

This works but for some reason the comma is missing. data gets inserted as

  • 0

This works but for some reason the comma is missing. data gets inserted as

GAURISH SHARMA
97
69
69
975
PASS

with each value written in new line. I wonder Where did the commas go? or do those have to manually inserted?


I am trying to write hash values using the following code

CSV.open("resultdata.csv", "a") do |csv|    
  h.each do |key, value|
    csv << value
  end
end

and here are the contents of the h hash

{:name=>"GAURISH SHARMA",
 :ca=>"97",
 :cb=>"69",
 :ba_lab=>"69",
 :bb_lab=>"69",
 :grand_total=>"975",
 :result=>"PASS"}

This code looks correct but upon running this code, generates the following errors:

/home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/csv.rb:1729:in `<<': undefined method `map' for "GAURISH SHARMA":String (NoMethodError)
    from /home/gaurish/Dropbox/code/projects/ra/result.rb:35:in `block (2 levels) in <main>'
    from /home/gaurish/Dropbox/code/projects/ra/result.rb:34:in `each'
    from /home/gaurish/Dropbox/code/projects/ra/result.rb:34:in `block in <main>'
    from /home/gaurish/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/csv.rb:1354:in `open'
    from /home/gaurish/Dropbox/code/projects/ra/result.rb:33:in `<main>'

Any idea what is wrong here?

  • 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-07T00:17:52+00:00Added an answer on June 7, 2026 at 12:17 am

    When you do csv << value, you are writing a string to a file.
    The CSV library makes it possible to write an array to a file (in a certain form). The best way to get an array with all values from a hash is to use the values method btw. So:

    require 'csv'
    h = {:name=>"GAURISH SHARMA",
     :ca=>"97",
     :cb=>"69",
     :ba_lab=>"69",
     :bb_lab=>"69",
     :grand_total=>"975",
     :result=>"PASS"}
    
    CSV.open("resultdata.csv", "a") do |csv|    
      csv << h.values
    end
    

    resultdata.csv will look like

    GAURISH SHARMA,97,69,69,69,975,PASS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yeah this works in FF and Chrome, but for some reason wont work in
This works in Firefox, but for some reason the gradient doesn't apply in Safari.
So this validation thing I wrote works but for some reason I need to
ok, this code works just fine in Chrome. But, for some reason it does
This evaluation in sql doesn't seems to work properly for some reason but i
I'm doing some tests with new features of CSS3, but this combination only works
I am getting stuck on this problem. It seems simple but for some reason
I'm pretty sure that many people have thought of this, but for some reason
I have no idea why this doesn't work, but doing some validation functions and
Ok this seems so silly but I'm having some trouble getting this to work.

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.