[英]Assign each array element attribute with a value from another array respectively
[英]Assign object attribute value to each element of array in iteration Ruby
我有一個名為“thing1”的數組和一個名為“Sr”的類,其屬性與“thing1”數組的元素相匹配。 我需要將事物 1 的每個元素分配給 Sr 對象的每個相應屬性。
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
因為我在 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
csv
庫看起來可以在此處用於解析每個元素(按|
拆分)並為每個元素分配標頭:
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}
然后,而不是輸出它( p
),你只需將它傳遞到你的類SR.new(**CSV.parse_line(line, headers: columns, col_sep: '|'))
。
如果things1
來自一個文件和第一行(你跳過的index == 0
是一個標題行,你可以這樣做:
CSV.foreach('path/to/file.extension', headers: true, col_sep: '|') do |line|
SR.new(**line)
end
使用Array#zip
將鍵壓縮為值。
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
返回一個數組,這就是為什么我們需要對其調用to_h
將其轉換為哈希。
與您可以將數組壓縮到鍵並將結果散列作為參數傳遞給Sr#new
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.