Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Your goal was to build a simple Ruby program that calculates the area and perimeter of a rectangle. Here's our solution.
Extra credit
 Write a method that accepts the length, width, and height of a box, and returns its volume (
length * width * height
).  Write a method that accepts the radius of a circle, and returns its area (
Math::PI * radius ** 2
).

0:00
Your goal was to build a simple Ruby program that calculates the area and

0:03
perimeter of a rectangle.

0:05
Here's my solution.

0:07
It's okay if yours is slightly different.

0:09
But if you see something interesting in my code,

0:11
you should consider borrowing it to improve your own own program.

0:15
So up here I have defined an area method.

0:17
And it takes two parameters, a length and a width.

0:22
And that's all that we need to calculate an area for a rectangle.

0:26
We just multiply the length by the width, store that in a variable, and

0:31
then return that variable.

0:35
Next step, we were supposed to define a perimeter method.

0:38
Which, if you visualize a rectangle, there are two sides where the length is

0:42
the same and two sides where the width is the same.

0:45
So you just add the length and the width together and

0:48
multiply the whole thing by two.

0:50
Making sure to complete the addition operation before doing the multiplication.

0:55
You can use parentheses to ensure that the correct order of operations is

0:59
followed there.

1:02
So we just accept length and width parameters, add those together,

1:07
multiply the whole thing by 2,

1:09
assign that to a variable, and return the value of that variable.

1:14
Then down here we were supposed to call the methods that we've created.

1:19
So we make a call to the area method,

1:22
we pass it a length and a width as arguments.

1:27
And here we make a call to perimeter, again passing a length and

1:31
a width as arguments.

1:33
We take the return values of each of those methods and

1:37
pass those in turn to the puts method which just prints those values out.

1:42
So we make a call here to area, with a length of 2 and a width of 4.

1:47
It multiplies those two together and we get a resulting area of 8.

1:52
Here, we make a call to perimeter, with a length of 2 and a width of 4.

1:56
It adds the length and width together, giving us 6, and

1:58
then multiplies the result by 2, giving us a return value of 12.

2:06
Now this is one way to do it but this is actually a little longer than necessary.

2:10
It's not Idiomatic Ruby.

2:12
We can shorten this up by simply returning

2:16
the result of the math operations.

2:21
So we can get rid of the variable that we're storing everything in, and

2:25
just return the results of the math operation.

2:30
Let's try saving this and running it again.

2:33
And you see we get the same results.

2:38
But we can actually make it even shorter than that.

2:41
The last expression that gets evaluated within the body

2:45
of a Ruby method becomes the return value of that method.

2:49
So we can actually take the return keyword off here.

2:53
Length times width will be the last expression we evaluate within the area

2:57
method, and so this will return the length and the width that were passed in.

3:03
We can do the same down here in perimeter,

3:05
the last expression that we evaluate here is 2 times length plus width.

3:10
And that just becomes the return value of the perimeter method.

3:14
So let's save this, try running it again.

3:18
And again we get the same result.

3:20
This is a much more succinct way to write methods in Ruby.

3:24
And if your methods are short,

3:25
you should consider leaving off the return keyword altogether.

3:30
I hope you got in some good Ruby practice.

3:32
See the teacher's notes for some other experiments you might try.

3:35
Have fun!
You need to sign up for Treehouse in order to download course files.
Sign up