Dynamically creating local variables in Ruby [duplicate]

不羁的心 提交于 2019-12-08 10:45:22

问题


I have a CSV file which I'm parsing and inserting bits of it in a database. I'd like to write a parser which takes account of the fact that the column order may change in the future.

I thought that I could do this by grabbing the header line as an array, and for each line, putting the value in a dynamically created local variable (using eval). However this doesn't seem to work, as local variables don't seem to be accessible outside the eval. I've read elsewhere that this may be correct in ruby 1.9, but I'm using 1.8.7

Code such as:

headers = ["a", "b"]
  headers.each do |h|
  p e_str = h+"=1"
  eval(e_str)
end
puts a

simply doesn't work, giving

test.rb:6: undefined local variable or method `a' for main:Object (NameError)

though line 3 prints "a=1" and "b=1" as expected

Does anyone know how I could go about this?


回答1:


You can get a similar effect using an openstruct

require 'ostruct'

x = OpenStruct.new

headers = ["a", "b"]
headers.each do |h|
  x.send("#{h}=", 1)
end
puts x.a
puts x.b



回答2:


Given data.csv

"foo","bar","baz"
1,2,3
4,5,6

then

#!/usr/bin/ruby1.8

require 'csv'

rows = CSV.readlines('data.csv')
header = rows[0]
data = rows[1..-1].map do |row|
  {}.tap do |h|
    header.each.with_index.map do |name, i|
      h[name] = row[i]
    end
  end
end

p data
# => [{"baz"=>"3", "foo"=>"1", "bar"=>"2"}, {"baz"=>"6", "foo"=>"4", "bar"=>"5"}]

Note: Require 1.8.7 or later.



来源:https://stackoverflow.com/questions/3864488/dynamically-creating-local-variables-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!