Ruby 1.9.2 introduced order into hashes. How can I test two hashes for equality considering the order?
Given:
h1 = {"a"=>1, "b"=>2, "c"=>3}
h2 = {"a"=>1, "c"=>3, "b"=>2}
I want a comparison operator that returns false for h1 and h2. Neither of the followings work:
h1 == h2 # => true
h1.eql? h2 # => true
Probably the easiest is to compare the corresponding arrays.
h1.to_a == h2.to_a
You could compare the output of their keys methods:
h1 = {one: 1, two: 2, three: 3} # => {:one=>1, :two=>2, :three=>3}
h2 = {three: 3, one: 1, two: 2} # => {:three=>3, :one=>1, :two=>2}
h1 == h2 # => true
h1.keys # => [:one, :two, :three]
h2.keys # => [:three, :one, :two]
h1.keys.sort == h2.keys.sort # => true
h1.keys == h2.keys # => false
But, comparing Hashes based on key insertion order is kind of strange. Depending on what exactly you're trying to do, you may want to reconsider your underlying data structure.
Related
I have a hash of key values and I want to downcase all of the Keys.
However I don't want to have to create an local variable, I would rather functionally do it.
NOT:
x = downcase_keys(params_hash)
BUT THIS:
params_hash.downcase_keys
How would do this in ruby?
I do not understand why you tagged this question as functional-programming it seems you are looking for a method to call on a Hash object.
Be aware that you may encounter problems doing so because duplicated keys are going to be overwritten.
h = {"a" => 1, "B" => 2}
# Singleton method on the object
def h.downcase_keys
temp = map {|k,v| [k.downcase, v]}.to_h
clear
merge! temp
end
h.downcase_keys()
p h # {"a"=>1, "b"=>2}
# Method available on all Hash objects
class Hash
def downcase_keys
temp = map {|k,v| [k.downcase, v]}.to_h
clear
merge! temp
end
end
h = {"a" => 1, "B" => 2}
h.downcase_keys()
p h # {"a"=>1, "b"=>2}
def downcase_keys(hash)
hash.downcase_keys
end
h = {"C" => 1, "B" => 2, "D" => 3}
downcase_keys(h)
p h # {"c"=>1, "b"=>2, "d"=>3}
I'm trying to create this huge hash, where there are many keys but only a few values.
So far I have it like so...
du_factor = {
"A" => 1,
"B" => 1,
"C" => 1,
"D" => 2,
"E" => 2,
"F" => 2,
...etc., etc., etc., on and on and on for longer than you even want to know. What's a shorter and more elegant way of creating this hash without flipping its structure entirely?
Edit: Hey so, I realized there was a waaaay easier and more elegant way to do this than the answers given. Just declare an empty hash, then declare some arrays with the keys you want, then use a for statement to insert them into the array, like so:
du1 = ["A", "B", "C"]
du2 = ["D", "E", "F"]
dufactor = {}
for i in du1
dufactor[i] = 1
end
for i in du740
dufactor[i] = 2
end
...but the fact that nobody suggested that makes me, the extreme Ruby n00b, think that there must be a reason why I shouldn't do it this way. Performance issues?
Combining Ranges with a case block might be another option (depending on the problem you are trying to solve):
case foo
when ('A'..'C') then 1
when ('D'..'E') then 2
# ...
end
Especially if you focus on your source code's readability.
How about:
vals_to_keys = {
1 => [*'A'..'C'],
2 => [*'D'..'F'],
3 => [*'G'..'L'],
4 => ['dog', 'cat', 'pig'],
5 => [1,2,3,4]
}
vals_to_keys.each_with_object({}) { |(v,arr),h| arr.each { |k| h[k] = v } }
#=> {"A"=>1, "B"=>1, "C"=>1, "D"=>2, "E"=>2, "F"=>2, "G"=>3, "H"=>3, "I"=>3,
# "J"=>3, "K"=>3, "L"=>3, "dog"=>4, "cat"=>4, "pig"=>4, 1=>5, 2=>5, 3=>5, 4=>5}
What about something like this:
du_factor = Hash.new
["A", "B", "C"].each {|ltr| du_factor[ltr] = 1}
["D", "E", "F"].each {|ltr| du_factor[ltr] = 2}
# Result:
du_factor # => {"A"=>1, "B"=>1, "C"=>1, "D"=>2, "E"=>2, "F"=>2}
Create an empty hash, then for each group of keys that share a value, create an array literal containing the keys, and use the array's '.each' method to batch enter them into the hash. Basically the same thing you did above with for loops, but it gets it done in three lines.
keys = %w(A B C D E F)
values = [1, 1, 1, 2, 2, 2]
du_factor = Hash[*[keys, values].transpose.flatten]
If these will be more than 100, writing them down to a CSV file might be better.
keys = [%w(A B C), %w(D E F)]
values = [1,2]
values.map!.with_index{ |value, idx| Array(value) * keys[idx].size }.flatten!
keys.flatten!
du_factor = Hash[keys.zip(values)]
Notice here that I used destructive methods (methods ending with !). this is important for performance and memory usage optimization.
I use this code for sorting Hash;
I've got no idea how its works.
please explain to me:
def foo(hash)
Hash[hash.sort]
end
irb(main):001:0> h = {1=>'z', 3=>'x', 2=>'y'}
=> {1=>"z", 3=>"x", 2=>"y"}
irb(main):002:0> Hash[h.sort]
=> {1=>"z", 2=>"y", 3=>"x"}
irb(main):003:0>
Enumerable#sort reutrns an sorted array of key-value pairs:
h = {b: 1, a: 2}
h.sort
# => [[:a, 2], [:b, 1]]
Hash::[] create a new hash base on the argument:
Hash[h.sort]
# => {:a=>2, :b=>1}
BTW, if you use Ruby 2.1+, you can use Array#to_h instead:
h.sort.to_h
# => {:a=>2, :b=>1}
Is there any difference between:
#attr[:field] = new_value
and
#attr.merge(:field => new_value)
If you're using merge! instead of merge, there is no difference.
The only difference is that you can use multiple fields (meaning: another hash) in the merge parameters.
Example:
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h3 = h1.merge(h2)
puts h1 # => {"a" => 100, "b" => 200}
puts h3 # => {"a"=>100, "b"=>254, "c"=>300}
h1.merge!(h2)
puts h1 # => {"a"=>100, "b"=>254, "c"=>300}
When assigning single values, I would prefer h[:field] = new_val over merge for readability reasons and I guess it is faster than merging.
You can also take a look at the Hash-rdoc: http://ruby-doc.org/core/classes/Hash.html#M000759
They do the same thing, however:
#attr[:field] = new_value
is more efficient, since no hash traversing is necessary.
Merge returns a new hash at a different location merging other_hashes into self
but Merge! operated like "update" returning self hash, copying at self-location.
h1 = { "a": 1 }
h2 = { "b": 2 }
def merge_hash (a,b)
puts h1 # {:a=> 1}
puts h1.object_id # 340720
h1 = h1.merge(h2)
puts h1 # {:a=>1, :b=>2}
puts h1.object_id # 340760
end
merge_hash(h1, h2)
puts h1 # {:a=> 1}
h1.object_id # 340720
def merge_hash (a,b)
puts h1 # {:a=> 1}
puts h1.object_id # 340720
h1 = h1.merge!(h2)
puts h1 # {:a=>1, :b=>2}
puts h1.object_id # 340720
end
merge_hash(h1, h2)
puts h1 # {:a=>1, :b=>2}
h1.object_id # 340720
You can use non-bang merge to use hashes in a functional programming style.
Ruby Functional Programming has under Don't update variables
Don't update hashes
No:
hash = {:a => 1, :b => 2}
hash[:c] = 3
hash
Yes:
hash = {:a => 1, :b => 2}
new_hash = hash.merge(:c => 3)
number = {:a => 1, :b => 2, :c => 3, :d => 4}
upon evaluation of certain condition i want to delete key-value pair of a,b,c
number.delete "A"
number.delete "B"
number.delete "C"
Or, less performant but more terse:
number.reject! {|k, v| %w"A B C".include? k }
or, more performant than second Chris' solution but shorter than first:
%w"A B C".each{|v| number.delete(v)}
ActiveSupport that is a part of Rails comes with several built-in methods can help you to achieve your goal.
If you just want to delete some key-value pairs, you can use Hash#except!
number.except!(:a, :b, :c)
If you want to keep the original hash, then use Hash#except
new_hash = number.except!(:a, :b, :c)
new_hash # => {:d=>4}
number # => {:a=>1, :b=>2, :c=>3, :d=>4}
You also can go with Rails-free way:
new_hash = number.dup.tap do |hash|
%i[a b c].each {|key| hash.delete(key)}
end
new_hash # => {:d=>4}
number # => {:a=>1, :b=>2, :c=>3, :d=>4}
P.S.: the last code example is very slow, I'm just providing it as an alternative.