RubyKin

Methods with args

What if we want to multiply any two numbers together using our own method. We could write something like this.

def multiply(num1,num2)
  num1 * num2
end

We define our method as multiply and pass in two arguments using parenthesis (num1 and num2). Then we can simply use Ruby’s multiply operator (the * method) on both numbers. The result is returned since it is the last line in our method. (In Ruby, the last line of a method is returned by default.)

multiply(2,3)
=> 6

multiply(4,2)
=> 8

multiply("Hi",3)
=> "HiHiHi"

Uh oh, looks like our method can multiply more than numbers! We can modify our code to ensure our users enter numbers, but let’s not worry about that now. How about another way to use arguments for a method. What if we want to calculate how many days old we are?

def years_to_days_old(years)
  result = years * 365
  puts "You are #{result} days old!"
end

Let's go over the method we wrote above. First, our method name is set to years_to_days_old. It takes one argument as input, stored in the variable years. The first line of our method creates a new variable result and assigns it the value of our passed-in argument years, multiplied by 365. This gives us the number of days per years. We then simply put the string "You are x days old!" to the screen, where x is our result variable.

Here's what it might look like if we call the method and pass in 10 years as the input.

years_to_days_old(10)
You are 3650 days old!
=> nil

Our puts method outputs the string using interpolation (the #{ } part) to display the result in our answer string. String interpolation is when Ruby allows a piece code to exist inside a string. This can be a variable or even math, written with a pound sign and curly braces #{ }. Here are some examples to get the idea.

x = 15
puts "This string has the #{x} variable inside."
=> This string has the 15 variable inside.

puts "This one is doing math: #{x + 5}"
=> This one is doing math: 20

We could add more functionality to the years_to_days_old method and remove the argument input. If we use Ruby’s gets method we can capture the input from a user’s response. We call chomp to remove (or chomp and eat) the new line character like we did in a previous chapter. The to_i method turns the user’s string input into an integer. Our method might look something like this.

1. def years_to_days_old
2.   puts "How old are you in years?"
3.   years = gets.chomp.to_i
4.   days = 365 * years
5.   puts "You are #{days} days old!"
6. end

Let’s run this from our IRB terminal. First, save the code above in a file called age_in_days.rb and place this in your desktop or your project folder. Now, when you open up your terminal or console, navigate to this folder and type irb in the command line. There, you can load the file and call the method by typing it. Here’s an example below.

/Documents/Ruby/my_code_folder >
/Documents/Ruby/my_code_folder > irb
>> load 'age_in_days.rb'
=> true
>> years_to_days_old
How old are you in years?

Now that you have a basic understanding of methods, see if you can write one yourself. You could write an adventure word game or create your own calculator. The best way to get started is to just experiment!