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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:33:25+00:00 2026-05-28T03:33:25+00:00

I have written method to load data to DB from CSV. For me it’s

  • 0

I have written method to load data to DB from CSV. For me it’s a little bit unsexy and it’s far away from DRY:

  def self.from_csv(data)
    c = Company.new
    FasterCSV.parse(data) do |row|
      c.name = row[0]
      c.street = row[1]
      c.street_number = row[2]
      c.apartament_number = row[3]

      c.city = row[4]
      c.post_code = row[5]
      c.post_office_city = row[6]

      c.nip = row[7]
      c.regon = row[8]
      c.vatin = row[9]
    end
  end

How to make it more sexy or may be there is already any lib to load data?

  • 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-05-28T03:33:26+00:00Added an answer on May 28, 2026 at 3:33 am

    You can put a sexy lady on top of it:

    #          ___
    #   _  _.-"_< }
    #    ""--"" 7(
    #          /())
    #         / )/
    #        ^ ( \
    #          / /
    #         /.'
    #        //
    # ______/L___ sexii
    def self.from_csv(data)
      #...
    end
    

    But, seriously, the only problem I see with your code is that you cannot rearrange attributes easily because you will have to manually update all indexes. I’d prefer to have an ordered list of attributes in an array and use some Ruby’s dynamic method calling:

    def self.from_csv(data)
      company = new
      row = CSV.parse_line(data)
      [:name, :street, :street_number, :apartament_number,
       :city, :post_code, :post_office_city,
       :nip, :regon, :vatin].each_with_index do |name, i|
        company.send(:"#{name}=", row[i])
      end
      company
    end
    

    Also note that you need to return a constucted company instance at the end otherwise you will get some random value when calling Company.from_csv.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a coworker who's written the following line in the page load method
I have written a method to calculate a given percentile for a set of
I have written this clone method for when the parent of the Employee class
I have written an extension method for string manipulation. I'm confused what should I
I have a written a method to get all the records and return in
I have a method written in VB.NET. It looks like this: Shared Sub SomeMethod(ByVal
I am writing some unit tests for an extension method I have written on
I have class1.m . I declared a method and written in it. Now i
Little silly question, but got stuck for a long time. I have written two
I have written an application which loads information from a plist which lies on

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.