Convert ruby source code from old style to new style hash
Rubocop is a static code analyzer that evaluates your code against many of the recommendations of the Ruby Style Guide. It has an --auto-correct
option that can automatically change your code to what is recommended. One of those auto-correct options is for hash syntax.
Example:
rubocop --only HashSyntax --auto-correct
will only correct your hashes.
Gem or script to turn old ruby syntax into new syntax (:symbol = whatever into symbol: whatever)
See this answer from a similar question. Looks like Rubocop's --auto-correct
option will take care of this for you.
Example usage:rubocop --only HashSyntax --auto-correct
Convert this to Ruby 1.9 Hash
I'm not sure where the trouble is, but it converts pretty simply to this:
render json: [@note.to_json(include: { contact: { except: [:created_at, :updated_at]}}, only: :body)], status: :created, location: [@contact, @note]
As an aside, stringing so many nested structures together on one line is bound to be confusing. Break it down so it's readable (and writable).
How to change Hash values?
my_hash.each { |k, v| my_hash[k] = v.upcase }
or, if you'd prefer to do it non-destructively, and return a new hash instead of modifying my_hash
:
a_new_hash = my_hash.inject({}) { |h, (k, v)| h[k] = v.upcase; h }
This last version has the added benefit that you could transform the keys too.
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"}}
ruby convert array to hash preserve duplicate key
I hope you would like this :
ary = [
"19d97e408ee3f993745b053e281ac9dc69519e06","refs/heads/auto",
"8f6f47c6e8023540b022586e368c68e1e814ce6d","refs/heads/callout_hooks",
"3cbdb4b2fcb85bc7f0ed08b62e2bf2445a7659e8","refs/heads/elab",
"d38a9a26ef887c08b306bdab210b39882f58e587","refs/heads/elab_6.1",
"19d97e408ee3f993745b053e281ac9dc69519e06","refs/heads/master",
"906dfe6eebff832baf0f92683d751432fcc98ab7","refs/heads/regression"
]
array_hash = ary.each_slice(2).with_object(Hash.new { |h,k| h[k] = []}) do |(k,v),hash|
hash[k] << v
end
# the main advantage is here you wouldn't loose any data, all are with you. You can
# use it as per your need. I think it is a better approach to deal with your situation.
array_hash
# => {"19d97e408ee3f993745b053e281ac9dc69519e06"=>
# ["refs/heads/auto", "refs/heads/master"],
# "8f6f47c6e8023540b022586e368c68e1e814ce6d"=>["refs/heads/callout_hooks"],
# "3cbdb4b2fcb85bc7f0ed08b62e2bf2445a7659e8"=>["refs/heads/elab"],
# "d38a9a26ef887c08b306bdab210b39882f58e587"=>["refs/heads/elab_6.1"],
# "906dfe6eebff832baf0f92683d751432fcc98ab7"=>["refs/heads/regression"]}
Swap hash keys with values and convert keys to symbols in Ruby?
You can do this:
Hash[periods.values.zip(periods.keys.map(&:to_sym))]
Or if you're using a version of Ruby where to_h
is available for arrays, you can do this:
periods.values.zip(periods.keys.map(&:to_sym)).to_h
What the two examples above do is make arrays of the keys and values of the original hash. Note that the string keys of the hash are mapped to symbols by passing to_sym
to map
as a Proc
:
periods.keys.map(&:to_sym)
# => [:q1, :q2, :q3, :q4, :h1, :h2]
periods.values
# => [0, 1, 2, 3, 4, 5]
Then it zips them up into an array of [value, key]
pairs, where each corresponding elements of values
is matched with its corresponding key in keys
:
periods.values.zip(periods.keys.map(&:to_sym))
# => [[0, :q1], [1, :q2], [2, :q3], [3, :q4], [4, :h1], [5, :h2]]
Then that array can be converted back into a hash using Hash[array]
or array.to_h
.
Related Topics
How to Change Passenger Ruby Version Without Recompiling
How to Delete a Range of Values from an Array
Split a String into Chunks of Specified Size Without Breaking Words
Ruby Refuses to Divide Correctly
How to Make Xcode Use the Correct Version of Ruby When Running a Script
Sort Values Using a Specific Collation in Ruby/Rails
How to Read a Gzip File Line by Line
Split Seeds.Rb into Multiple Sections
What's the "Ruby Way" to Parse a String for a Single Key/Value
Rails: Wkhtmltopdf Runtimeerror (Location of Wkhtmltopdf Unknown)
How to Use Regex for Utf8 in Ruby
Rails -V Cannot Load Such File -- Rails/Cli (Loaderror)
Stack Level Too Deep When Using Carrierwave Versions
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv2/V3 Read Server Hello A
Change Value of Request.Remote_Ip in Ruby on Rails
How to Disable All Form_For Input Fields in Ruby on Rails App