Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)
Multidimensional Arrays
We can create multidimensional arrays. Rather, arrays inside of arrays. You can also put a hash in an array.
double_array = [[1,2,3,],["a","b","c"],{"Kim" => 19, "Sam" => 20, "Jane" => 22}]
We're going to puts our array assets out on their own lines using the .each method we learned earlier.
double_array.each { |x| puts "#{x}" }
⇩
[1, 2, 3]
["a", "b", "c"]
{"Kim"=>19, "Sam"=>20, "Jane"=>22}
Find out how old Sam is.
puts double_array[2]["Sam"]
⇩
20
Create Your Own Methods
Remember when we used the method .upcase earlier to capitalize all the letters in our string.
print "rabbit".upcase
⇩
RABBIT
We want to create our own custom method. We will start with a simple one.
def hello
puts "Hello everyone!"
end
hello
⇩
Hello everyone!
Make sure to start with def and end with end. Notice that by simply typing out the method name we were able to execute its code.
You can do more complex methods by adding parameters. We'll create a method that accepts an argument and then multiplies the argument by three.
def times_three(n)
puts n * 3
end
times_three(5)
⇩
15
Note: For clarity, (n) is our parameter and (5) is our argument. The parameter(s) are what we set when we're creating the method. Arguments are what's made when we're calling the method. Parameters are the things waiting for arguments to be plugged into.
def our_method(parameter, parameter)
#code goes here
end
our_method(argument, argument)
The arguments will be plugged into the parameters at top. I'm clarifying this because these words are often used interchangeably. They don't have to be if we understand the difference right away.
As you can see in the example, I created a method that has two parameters. Why not go ahead and write a method that accepts two arguments? (Meaning, it has two parameters. Oh my goodness.)
def multiply_em(x,y)
puts x * y
end
multiply_em(4,5)
⇩
20
Splat
Methods don't always know how many arguments you're going to feed them. In these cases you can use splat arguments. In order to tell Ruby you don't know exactly how many arguments you're going to have, you use a * symbol.
def fav_novels(*books)
books.each {|book| puts "One of my favorite books is #{book}."}
end
fav_novels("Mistborn","The Demon-Haunted World","Harry Potter","Perdido Street Station")
⇩
One of my favorite books is Mistborn.
One of my favorite books is The Demon-Haunted World.
One of my favorite books is Harry Potter.
One of my favorite books is Perdido Street Station.
We can actually distinguish between two arguments using splat:
def fav_novels(opening,*books)
books.each {|book| puts "#{opening} #{book}."}
end
fav_novels("I liked","Mistborn","The Demon-Haunted World","Harry Potter","Perdido Street Station")
⇩
I liked Mistborn.
I liked The Demon-Haunted World.
I liked Harry Potter.
I liked Perdido Street Station.
The first argument goes into the first parameter position and any remaining arguments are put into the second parameter with the * symbol.
All materials on the site are licensed Creative Commons Attribution-Sharealike 3.0 Unported CC BY-SA 3.0 & GNU Free Documentation License (GFDL)
If you are the copyright holder of any material contained on our site and intend to remove it, please contact our site administrator for approval.
© 2016-2026 All site design rights belong to S.Y.A.