Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Hash.ruby2_keywords_hash

Hash.ruby2_keywords_hash(hash) → hash

Deprecated: will be removed in Ruby 4.5, one version after the removal of the ruby2_keywords mechanism. See bugs.ruby-lang.org/issues/22205 for the schedule.

Duplicates a given hash and adds a ruby2_keywords flag. This method is not for casual use; debugging, researching, and some truly necessary cases like deserialization of arguments.

Example
h = {k: 1}
h = Hash.ruby2_keywords_hash(h)
def foo(k: 42)
  k
end
foo(*[h]) #=> 1 with neither a warning or an error

Ruby version changes

  1. 4.1
See also .ruby2_keywords_hash? .new .[] .try_convert also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0