Ruby 2.1 Hashes and Using Hashes for Keyword Arguments

Let’s Review…

What is a hash? According to ruby-doc.org, a Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. Hashes enumerate their values in the order that the corresponding keys were inserted. Ultimately, it is a series a of Key/Value pairs in no particular order.

These key/value pairs can be called by their key as seen below:

hash = {key1: value1, 
key2: value2}
# => hash[:key1] = value1

By now, you are probably all familiar to the following notation for a general method definition where a function is defined to take in arguments:

def new_method(arg1,arg2,arg3)
puts arg1
result = arg2 + arg3
end

And then that function can be called with values directly, or with values defined in variables and then those variables can be passed to that function:

new_method(2,3,4)
 or

a = 2 , b = 3 , c = 4 new_method(a,b,c)

Now to the good stuff…

So all of that above is fine and good, but sometimes it may be easiest to take in a hash given the way you are retrieving your data or have stored it previously. In older versions of ruby, you would have to extract the elements needed from the hash and store them in variables and then pass those variables into the method. With Ruby 2.0+ you can now define your method to take in specific keys/values of a hash. Therefore, you can pass in an entire hash of the required arguments and execute as seen in the examples below:

# Defined with default value

When defining a method that can accept hash arguments, they can be defined with a default value, just as normal arguments can be. When no corresponding value is found in the given hash, the default value will be returned. Otherwise if defined as it is passed in, it will return the new specified value.

def new_method(arg1: 'default', arg2: 'default2')   
puts arg1
puts arg2
end
(pass in no arguments)
new_method # => 'default' 'default2'
or (pass in key/value pairs)
new_method(arg1: 'hello') # => 'hello' 'default2'
or (pass in entire hash)
hash = {arg1: 'hello'}
new_method(hash) # => 'hello' 'default2'
or (Pass in both arguments)
hash = {arg1: "hello",arg2: "hey"}
new_method(hash) # => 'hello' 'hey'

#Required Arguments (No default values)

If no default value is defined in the method definition, then the function must be provided with the required values when called. Below you can also see the various ways that the function can be called and what you will be returned in the end. In the examples below, note that you will get an error if you pass unnecessary key/value pairs or no arguments at all.

def new_method(arg1:,arg2:)   
puts arg1
puts arg2
end
(pass in no arguments)
new_method # => ArgumentError: missing keyword: arg1 
or (pass in key/value pairs)
new_method(arg1: 'hello', arg2: 'hey') # => 'hello' 'hey'
or (pass in entire hash)
hash = {arg1: "hello",arg2: "hey"}
new_method(hash) # => 'hello' 'hey'
or (switch order in hash)
hash = {arg2: "hey",arg1: "hello"}
new_method(hash) # => 'hello' 'hey'
or (Pass in larger hash)
hash = {arg2: "hey",arg1: "hello",arg3: "hiya"}
new_method(hash) # => Argument Error: unknown keyword: arg3

Hopefully this helps you better understand how to execute running your functions with key/value pair arguments.

https://ruby-doc.org/core-2.1.1/Hash.html