简体   繁体   中英

Better way to fill a Ruby hash?

Is there a better way to do this? (it looks clunky)

form_params = {}
form_params['tid'] = tid
form_params['qid'] = qid
form_params['pri'] = pri
form_params['sec'] = sec
form_params['to_u'] = to_u
form_params['to_d'] = to_d
form_params['from'] = from
form_params['wl'] = wl
form_params = { "tid" => tid, "qid" => qid }     

Or you could do

form_params = Hash["tid", tid, "qid", qid]       #=> {"tid"=>tid, "qid"=>qid}
form_params = Hash["tid" => tid, "qid" => qid]   #=> {"tid"=>tid, "qid"=>qid}
form_params = Hash[tid => tid, qid => qid]       #=> {"tid"=>tid, "qid"=>qid}

(nb. the last one is new for 1.9 and it makes your key symbols instead of strings )

{tid:tid, qid:qid}

Keys and values occur in pairs, so there must be an even number of arguments.

Slight modification to the above, since your example had string keys:

form_params = {}
%w(tid qid pri sec to_u to_d from wl).each{|v| form_params[v] = send(v)} 

If performance is not important this one might look better:

form_params = {}
['tid', 'qid', 'pri', 'sec', 'to_u', 'to_d', 'from', 'wl'].each do |v|
  form_params[v] = eval(v)
end

If those names are actually methods you can replace eval by the faster send :

form_params[v] = send(v.to_sym)

(Update) An alternative (and more elegant) way using inject :

form_params = ['tid', 'qid', 'pri', 'sec', 'to_u', 'to_d', 'from', 'wl'].inject({}) { |h, v| h[v] = send(v); h }

另一种形式,红宝石1.9:

form_params = {tid: "tid", qid: "qid", ...}
Hash[%w(tid qid pri sec to_u to_d from wl).collect{|x| [x,send(x)]}]

I created a custom class called MoreOpenStruct to deal with Hash's lack of aesthetics, and using that class, your example would look like:

form_params = MoreOpenStruct.new
form_params.tid = tid
form_params.qid = qid
form_params.pri = pri
form_params.sec = sec
form_params.to_u = to_u
form_params.to_d = to_d
form_params.from = from
form_params.wl = wl

form_params_hash = form_params._to_hash
  #=> { :tid => tid, :qid => qid, etc }

If you need string literals rather than symbols as the key to your hash then a couple more tweaks are required.

I use my custom struct class when I want easy to read/manipulate Hashes, but if all you're doing is an assignment and don't need to parse the hash beyond that, I'd use Roger's solution.

The other options would be to create a Struct based class to store those values in. For example:

FormParams = Struct.new(:tid, :qid, :pri, :sec, :to_u, :to_d, :from, :wl)

Then you'd be able to initialise the form_params instance in a variety of ways:

form_params = FormParams.new(:tid => tid, :qid => qid, # etc ...
form_params = FormParams.new(tid, qid, pri, sec, to_u, to_d, from, wl)
form_params = FormParams.new
form_params.tid = tid
form_params.gid = gid
# etc ...
a = %w(tid quid pri sec to_u to_d from wl)
form_params = Hash[a.zip(a.collect {|v| send(v) })]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM