Convert hash keys to lowercase -- Ruby Beginner
You can use something like this:
CSV.foreach(file, :headers => true) do |row|
new_hash = {}
row.to_hash.each_pair do |k,v|
new_hash.merge!({k.downcase => v})
end
Users.create!(new_hash)
end
I had no time to test it but, you can take idea of it.
Hope it will help
how to change hash keys in the lower case using ruby
class Hash
def downcase_key
keys.each do |k|
store(k.downcase, Array === (v = delete(k)) ? v.map(&:downcase_key) : v)
end
self
end
end
myHash.downcase_key
Ruby - Downcase on string values in a hash that also contains nil values
Check if the value is a valid string before downcasing
testHash.each { |k, v| v.downcase! if v.is_a?(String) }
Best way to convert strings to symbols in hash
In Ruby >= 2.5 (docs) you can use:
my_hash.transform_keys(&:to_sym)
Using older Ruby version? Here is a one-liner that will copy the hash into a new one with the keys symbolized:
my_hash = my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
With Rails you can use:
my_hash.symbolize_keys
my_hash.deep_symbolize_keys
How do I convert a Ruby hash so that all of its keys are symbols?
hash = {"apple" => "banana", "coconut" => "domino"}
Hash[hash.map{ |k, v| [k.to_sym, v] }]
#=> {:apple=>"banana", :coconut=>"domino"}
@mu is too short: Didn't see word "recursive", but if you insist (along with protection against non-existent to_sym
, just want to remind that in Ruby 1.8 1.to_sym == nil
, so playing with some key types can be misleading):
hash = {"a" => {"b" => "c"}, "d" => "e", Object.new => "g"}
s2s =
lambda do |h|
Hash === h ?
Hash[
h.map do |k, v|
[k.respond_to?(:to_sym) ? k.to_sym : k, s2s[v]]
end
] : h
end
s2s[hash] #=> {:d=>"e", #<Object:0x100396ee8>=>"g", :a=>{:b=>"c"}}
Converting nested hash keys from CamelCase to snake_case in Ruby
You need to treat Array and Hash separately. And, if you're in Rails, you can use underscore
instead of your homebrew to_snake_case
. First a little helper to reduce the noise:
def underscore_key(k)
k.to_s.underscore.to_sym
# Or, if you're not in Rails:
# to_snake_case(k.to_s).to_sym
end
If your Hashes will have keys that aren't Symbols or Strings then you can modify underscore_key
appropriately.
If you have an Array, then you just want to recursively apply convert_hash_keys
to each element of the Array; if you have a Hash, you want to fix the keys with underscore_key
and apply convert_hash_keys
to each of the values; if you have something else then you want to pass it through untouched:
def convert_hash_keys(value)
case value
when Array
value.map { |v| convert_hash_keys(v) }
# or `value.map(&method(:convert_hash_keys))`
when Hash
Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
else
value
end
end
Related Topics
Get Chromes Console Log via Ruby Webdriver
Ruby Pipes: How to Tie the Output of Two Subprocesses Together
Cannot Install Any Version of Ruby on Mojave - Internal Ranlib Command Failed
Simplest Way to Send Raw Byte-Arrays Using Ruby's Tcpsocket-Class
Skip/Disable Force_Ssl for Particular Controller in Rails
Rails 3.1 - Changing Default Scaffold Views and Template
How to Preload Concerns in a Rails Initializer Using Rails 6/Zeitwerk
How to Use Views and Layouts with Ruby and Erb (Not Rails)
Ruby on Rails: Confirmation Page for Activerecord Object Creation
How to Get a Selenium/Ruby Bot to Wait Before Performing an Action
Does Ruby 1.9.2 Have an Is_A? Function
Rails Server Does Not Start by Command "Rails S"
Sass Variables Not Parsing Correctly - Undefined Variable: "$Ct-White"