Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Hash#to_proc

() -> ^(K) -> V?
hash.to_proc → proc

Returns a Proc object that maps a key to its value:

Example
h = {foo: 0, bar: 1, baz: 2}
proc = h.to_proc
proc.class # => Proc
proc.call(:foo) # => 0
proc.call(:bar) # => 1
proc.call(:nosuch) # => nil
See also #to_hash #to_s #to_h #transform_keys also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0