Ruby .to_proc
Beware not to forget about .to_proc conversions.
Since Ruby 2.3.0 Hash class has a .to_proc conversion. It’s pretty useful with &
operator.
h = { a: 1, b: 2, c: 3 }
[ :a, :b, :c, ].map(&h) # => [ 1, 2, 3 ]
Another case is using Hash with gsub
h = { 'a' => '1', 'b' => '2', 'c' => '3' }
"xxxabcxxx".gsub /[abc]/, h # => "xxx123xxx"
default
And very useful is Hash default for non entry values.
h = { 'a' => '1', 'b' => '2', 'c' => '3' }
h.default = 'Y'
"xxxabcxxx".gsub /[abcx]/, h # => "YYY123YYY"
You may use default_proc to create new key-value pairs.
h = { 'a' => '1', 'b' => '2', 'c' => '3' }
h.default_proc = proc { |h, c| h[c] = c.upcase }
"xxxabcxxx".gsub /[abcx]/, h # => "XXX123XXX"
Interesting that we can do cache strategy as simple as follows
cache = Hash.new { |cache,url| cache[url] = open(url) }
puts cache['http://site.com/resource']
Array#to_proc
I have found interesting suggestion to add to_proc to array with similar functionality. https://thepugautomatic.com/2014/11/array-to-proc-for-hash-access/
If that fits your eyes
[ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]
Here the way you can get it
class Array
def to_proc
->(h) { length == 1 ? h[first] : h.values_at(*self) }
end
end
And that will work with multiple keys too
[ { name: 'A', 'age' => 41 }, { name: 'B', 'age' => 42 } ].map(&[:name, 'age'])
# => [ [ "A", 41 ], [ "B", 42 ] ]