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 9220855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:26:44+00:00 2026-06-18T03:26:44+00:00

Hey guys I’m writing a ruby program that reads a database of food items

  • 0

Hey guys I’m writing a ruby program that reads a database of food items and recipes that is in CSV format, and writes it back to a file. I’m having issues writing to a CSV file correctly

I want to write an objects attributes to a CSV file

csv_text = CSV.open("FoodDB1.txt","w") do |i|
    @@dataList.each do |j|
        if j.get_type == "b"
            i << [j.name,j.get_type,j.cal]
        elsif j.get_type == "r"
            i << [j.name,j.get_type,j.print_bFood]
        end
    end
end

I have two types of objects, basic food and a recipe. Both are stored in the dataList array. I check each object for its type, if it’s a basic food, writing it is easy since it is just three simple fields. If it is a recipe, I write the name,type,and the basic foods that make up that recipe.

The issue I’m having is at this line

i << [j.name,j.get_type,j.print_bFood]

So it prints out the name of the recipe, the type(whether its a basic food or a recipe) and then finally the list of foods in the recipe. That is where I’m having issues.

bFood is an array of basic foods that is stored in the object, and I’m having trouble adding it to the CSV row. I tried making a method(which is print_bFood) that returns a string of the combined array using .join(“,”), but because of the comma in the string, when CSV writes it to a file it is wrapped in quotes

"PB&J Sandwich,r,"Jelly,Peanut butter,Bread slice, Bread slice""

I want it to look like this

"PB&J Sandwich,r,Jelly,Peanut butter,Bread slice, Bread slice"

Any ideas on what can help. I’ve looked for ways to do this and I just can’t think of anything anymore.

One idea I had was if I had the ability to just add on to a row, I could iterate through the bFood array and add each one to the row, but I haven’t found any functionality that can do that.

  • 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-18T03:26:45+00:00Added an answer on June 18, 2026 at 3:26 am

    If I read this correctly you should just need…

    i << [j.name, j.get_type, j.bFood].flatten
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I have an array of dictionaries plist file that I am reading
Hey guys I was wondering how I could download a file that is generated
Hey guys I'm making a chat program and I have a parent MessagePacket class
Hey guys im trying to connect to an database and collect some data out
Hey guys so in JQuery ui it produces a close link that once clicked
Hey guys. I have a method that gets called each second which I want
Hey guys.. Quick question: I wrote a simple JS that opens lightBox for image
Hey guys i am currently working on an android app that will allow me
hey guys i wanna ask a question about sending data to database to server
Hey guys I'm implementing a page that has about 9 separate divs on it

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.