Keyword Arguments
Published: 2022-11-18 23:05:09

Keyword Arguments, What are they? Why should we use them?

Keyword arguments are a more specific way to pass arguments to a method. They allow you to set parameters as keys and the arguments as the values of those keys, similar to a hash. Take for instance we have a Person class. class Person
  attr_accessor :name, :greeting
 def initialize(name, greeting)
   @name = name
   @greeting = greeting
 end
 
 def introduction
   puts "#{@greeting} my name is #{@name} "
 end
end
 
 
chris = Person.new("chris", "hi there");
joe = Person.new("hello", "joe");

Now we see here that Chris instance of the Person class, has a name of “chris” and a greeting of “hi there”. When we call the introduction method it prints out correctly “hi there my name is chris” to the terminal. 
However if we move over to the joe instance of this Person class we see that the arguments are in a different order. When we call the introduction method on the joe instance it prints out “joe my name is hello”. This doesn’t make much sense and represents an issue that positional arguments can create. Now this example would be obvious to most people. What if we had a situation where we were instead working with two sets of names, a first and last name.



class Person
  attr_accessor :first_name, :last_name
 def initialize(first_name, last_name)
   @first_name = first_name
   @last_name = last_name
 end
 
 def introduction
   puts "Hello #{first_name} #{last_name} is present."
 end
end
 
 
person1 = Person.new("jackson", "harper");
person2 = Person.new("riley", "lincoln");

Here’s an example where we have two arguments, a first and last name. Person1 is initialized with “jackson” and “harper”, so when we call the introduction method on person1 we get “Hello jackson harper is present.”. However these two names are actually pretty common and can be used interchangeably, so how can we be sure that it’s not supposed to be Harper Jackson? The same can be said for person2 it can either be lincoln riley or riley lincoln. These examples are admittedly contrived but they illustrate the point that using positional arguments isn’t always the best way to handle our arguments. 
These shortcomings can be fixed by using keyword arguments. Keyword arguments make it so that we do not have to worry about which order we passed our arguments. 

class Person
  attr_accessor :first_name, :last_name
 def initialize(first_name: , last_name:)
   @first_name = first_name
   @last_name = last_name
 end
 
 def introduction
   puts "Hello #{first_name} #{last_name} is present."
 end
end
 
 
person1 = Person.new(first_name:"jackson", last_name:"harper");
person2 = Person.new(first_name:"lincoln", last_name:"riley");
person3 = Person.new(last_name:"carter", first_name:"james");
 

As we can see now each instance of the person class is now initialized with keyword arguments named first_name and last_name. This makes it clear what information belongs in each argument to not just me but also whomever may be working or looking at my code. If you take a look at person3 you can also see that order does not matter. I initialized person3 by passing in the last name keyword argument first but when we call the introduction method on person3 we still get the information in the right order as it still prints the first name and then the last name. 
Keyword arguments are a great tool to use especially when you’re working with Classes. They can be a hint as to what kind of data type each argument is expecting, and it makes it so that you are less prone to inputting the wrong information.