简体   繁体   中英

Array and Hash Iteration in Ruby - Rails

The params' hash keys are mix of camelcase, lowercase and uppercase characters:

params = {"RefreshTime"=>"0", "lang"=>"tr", "amount"=>"50", "ACQBIN"=>"490740"}

The array , which is a list of one of my models' column names, is all lowercase but exactly the same values with the keys of params hash =>

columns = ["refreshtime", "lang", "amount", "acqbin", ......]

I'm trying to match hash keys with the members of the array to create a new record in my controller =>

def create_transaction
   @transaction = OrderTransaction.new(
      params.each do |k, v|
        columns.each do |i|
          if i == k.downcase
            "#{i}: params[:#{k}],"
          end
        end
      end
    )
end

But this piece of code isn't working as I expected. It seems like I'm doing something wrong in the line of;

#{i}: #{v}

What am I missing here?

By the way, this was my old way to do this job, which causes many lines of code =>

@transaction = OrderTransaction.new(
  refreshtime: params[:RefreshTime],
  lang: params[:lang],
  amount: params[:amount],
  acqbin: params[:ACQBIN],
  ...
)

You could do something like this

@transaction = OrderTransaction.new(Hash[params.map{|k,v|[k.downcase,v]}])

This creates a new hash with all lowercase keys and uses it to initialize the OrderTransaction.

To only include keys that appear in the columns array

@transaction = OrderTransaction.new(Hash[params.map{|k,v|[k.downcase,v]}.select{|k,v|columns.include?(k)}])

You're not creating a hash entry, you're creating a string.

I'd suggest...

eval "#{i}: params[:#{k}],"

Which hopefully will actually create the key value pair.

or for that matter, why not...

i.to_sym => params[k.to_sym]

I suggest a cleaner way to do it:

def create_transaction
    result_hash = {}
    params.each do |k,v|
        temp = k.to_s.downcase
        if columns.include? temp
            result_hash[temp] = v
        end
    end
    @transaction = OrderTransaction.new(result_hash)
end

Typically we'd create a mapping hash. Starting with:

params = {"RefreshTime"=>"0", "lang"=>"tr", "amount"=>"50", "ACQBIN"=>"490740"}

This is a fast way to do it if you know the maps are consistent:

cols_to_key = Hash[params.keys.map(&:downcase).zip(params.keys)]
# => {"refreshtime"=>"RefreshTime",
#     "lang"=>"lang",
#     "amount"=>"amount",
#     "acqbin"=>"ACQBIN"}

Or:

cols_to_key = params.keys.map(&:downcase).zip(params.keys).to_h
# => {"refreshtime"=>"RefreshTime",
#     "lang"=>"lang",
#     "amount"=>"amount",
#     "acqbin"=>"ACQBIN"}

Accessing values looks like:

params[cols_to_key['refreshtime']] # => "0"
params[cols_to_key['lang']] # => "tr"

You can even slice the params hash various ways. If you want everything:

params.values_at(*cols_to_key.values)
# => ["0", "tr", "50", "490740"]

If you want a few things:

params.values_at(*cols_to_key.values_at('amount', 'acqbin'))
# => ["50", "490740"]

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