Charlie T Lee
Charlie T Lee

Reputation: 1

Assign object attribute value to each element of array in iteration Ruby

I have an array called "thing1" and a class called "Sr" with attributes matching elements of the "thing1" array. I need to assign each element of thing 1 to each corresponding attributes of the Sr object.

things1.each_with_index do |line,index|
      if index == 0
          next
      elsif
       array = line.strip.split('|')
       array.each do |attribute|
         # here is where I am stuck !!!!!!!
       end

as I received the following in pry

[5] pry(main)> array= l.strip.split('|')
=> ["324184-101003318953",
 "5118 ROBERTSON, HOUSTON TX 77009",
 "HARRIS",
 "H",
 "NEAR NORTHSIDE",
 "0311520000001",
 "NE",
 "NW",
 "FRIDAY",
 "1st Friday",
 "FRIDAY-B",
 "453V",
 "Greater Northside MD",
 "311 HelpLine",
 "311 Call Handling",
 "Unclassified 311 Web Request",
 "311_Seniors",
 "18",
 "Closed",
 "2019-01-01 00:02:10",
 "2019-01-02 09:02:07",
 "2019-01-03 09:34:08",
 "1.02",
 "Other - 324184",
 "-95.35739000000",
 "29.80668000000",
 "29.80654129",
 "-95.35715636",
 "WAP"]
[6] pry(main)> array[0]
=> "324184-101003318953"
[7] pry(main)> array.size
=> 29
Here is the new Sr class attributes that I need to assign each of the 29 elements of the thing1 array
  sr = Sr.new(:case_number => case_number, :sr_location => sr_location, :county => county, :district => district, :neighborhood => neighborhood, :tax_id => tax_id, :trash_quad => trash_quad,:recycle_quad => recycle_quad, :trash_day => trash_day, :heavy_trash_day => heavy_trash_day, :recycle_day => recycle_day, :key_map => key_map,:management_district => management_district, :department => department, :division => division, :sr_type => sr_type, :queue => queue, :sla => sla, :status => status, :sr_create_date => sr_create_date, :due_date => due_date, :date_closed => date_closed, :overdue => overdue, :title => title, :x => x, :y => y, :latitude = latitude, :longitude => longitude, :channel_type => channel_type, :created_at => created_at, :updated_at => updated_at )
  sr.save
     end

Upvotes: 0

Views: 467

Answers (2)

Simple Lime
Simple Lime

Reputation: 11090

The csv library looks like it might be able to be used here to parse each element (splitting by |) and assign headers to each element:

require 'csv'

things1 = ["324184-101003318953|5118 ROBERTSON, HOUSTON TX 77009|HARRIS|H|NEAR NORTHSIDE|0311520000001|NE|NW|FRIDAY|1st Friday|FRIDAY-B|453V|Greater Northside MD|311 HelpLine|311 Call Handling|Unclassified 311 Web Request|311_Seniors|18|Closed|2019-01-01 00:02:10|2019-01-02 09:02:07|2019-01-03 09:34:08|1.02|Other - 324184|-95.35739000000|29.80668000000|29.80654129|-95.35715636|WAP"]

columns = %i[case_number sr_location county district neighborhood tax_id trash_quad recycle_quad trash_day heavy_trash_day recycle_day key_map management_district department division sr_type queue sla status sr_create_date due_date date_closed overdue title x y latitude longitude channel_type created_at updated_at]

things1.each do |line|
  p(**CSV.parse_line(line, headers: columns, col_sep: '|'))
end
# output: {:case_number=>"324184-101003318953", :sr_location=>"5118 ROBERTSON, HOUSTON TX 77009", :county=>"HARRIS", :district=>"H", :neighborhood=>"NEAR NORTHSIDE", :tax_id=>"0311520000001", :trash_quad=>"NE", :recycle_quad=>"NW", :trash_day=>"FRIDAY", :heavy_trash_day=>"1st Friday", :recycle_day=>"FRIDAY-B", :key_map=>"453V", :management_district=>"Greater Northside MD", :department=>"311 HelpLine", :division=>"311 Call Handling", :sr_type=>"Unclassified 311 Web Request", :queue=>"311_Seniors", :sla=>"18", :status=>"Closed", :sr_create_date=>"2019-01-01 00:02:10", :due_date=>"2019-01-02 09:02:07", :date_closed=>"2019-01-03 09:34:08", :overdue=>"1.02", :title=>"Other - 324184", :x=>"-95.35739000000", :y=>"29.80668000000", :latitude=>"29.80654129", :longitude=>"-95.35715636", :channel_type=>"WAP", :created_at=>nil, :updated_at=>nil}

Then, instead of outputting it (p) you just pass it into your class SR.new(**CSV.parse_line(line, headers: columns, col_sep: '|')).

If things1 came from a file and the first line (the index == 0 that you're skipping is a header row, you could do:

CSV.foreach('path/to/file.extension', headers: true, col_sep: '|') do |line|
  SR.new(**line)
end

Upvotes: 1

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121020

Use Array#zip to zip keys to values.

array = ['foo', 'bar']
keys = %i[num loc]
def fun(hash)
  puts hash.inspect
end

fun(keys.zip(arr).to_h)
#⇒ {:num=>"foo", :loc=>"bar"}

zip returns an array, that’s why we need to call to_h on it to convert it to the hash.

The same way you might zip your array to the keys and pass the resulting hash as a parameter to Sr#new.

Upvotes: 0

Related Questions