June 2017
Beginner
330 pages
7h 30m
English
Last on our list of method arguments is the ability to pass optional arguments to a method. There are a number of times when you want flexibility with what types of data you pass to methods, and that's where optional arguments can come in handy.
Let's imagine that we're creating an invoice method. Our implementation needs to be flexible because some customers provide different data than others. We can accommodate this type of behavior using code like this:
def invoice options={} puts options[:company] puts options[:total] puts options[:something_else] end invoice company: "Google", total: 123, state: "AZ"
When we run this program, you can see some interesting behavior. We're using the options hash in our method declaration. ...
Read now
Unlock full access