Welcome back! We're going to jump in to practicing building methods in Ruby. Remember that methods are chunks of code that we name and can call at any time.
The synax for a method in Ruby is:
def method_name(input)
# code using the input variable
end
Create a new Ruby file called 'methods-practice.rb' with the touch
command in your command line. Open up the file and code the following functions. Make sure to test them out by calling the function and then running ruby methods-practice.rb
from the command line!
-
- Build a method
timesByOneThousand
that takes an integer and multiplies it by 1000. (We started this one for you)
- Build a method
def timesByOneThousand(number)
#code here
end
-
- Build a method
howOldIWillBeIn2030
that takes in your age (as an integer) and tells you how old you will be in the year 2030. (16 -> 30)
- Build a method
-
- Build a method
makeExcited
that takes a string, makes it all uppercase and adds three exclamation points. ("hello" -> "HELLO!!!")
- Build a method
-
- Build a method called
happyBackwards
that takes in a string, reverses it, and then adds on a smiley face character at the end. ("cool" -> "LOOC :)")
- Build a method called
-
- Build a method called isEven that returns a boolean (true or false) depending on whether or not the number is divisible by 2. (3 -> false, 4 -> true)
-
- Build a method called gradeCalculator that takes in a number (0 to 100) and returns the letter grade that corresponds to it. (95 -> "A", 85 -> "B")