Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses 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
).
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Your goal was to build a simple Ruby
program that calculates the area and
0:00
perimeter of a rectangle.
0:03
Here's my solution.
0:05
It's okay if yours is slightly different.
0:07
But if you see something
interesting in my code,
0:09
you should consider borrowing it
to improve your own own program.
0:11
So up here I have defined an area method.
0:15
And it takes two parameters,
a length and a width.
0:17
And that's all that we need to
calculate an area for a rectangle.
0:22
We just multiply the length by the width,
store that in a variable, and
0:26
then return that variable.
0:31
Next step, we were supposed
to define a perimeter method.
0:35
Which, if you visualize a rectangle,
there are two sides where the length is
0:38
the same and
two sides where the width is the same.
0:42
So you just add the length and
the width together and
0:45
multiply the whole thing by two.
0:48
Making sure to complete the addition
operation before doing the multiplication.
0:50
You can use parentheses to ensure that
the correct order of operations is
0:55
followed there.
0:59
So we just accept length and
width parameters, add those together,
1:02
multiply the whole thing by 2,
1:07
assign that to a variable, and
return the value of that variable.
1:09
Then down here we were supposed to
call the methods that we've created.
1:14
So we make a call to the area method,
1:19
we pass it a length and
a width as arguments.
1:22
And here we make a call to perimeter,
again passing a length and
1:27
a width as arguments.
1:31
We take the return values of
each of those methods and
1:33
pass those in turn to the puts method
which just prints those values out.
1:37
So we make a call here to area,
with a length of 2 and a width of 4.
1:42
It multiplies those two together and
we get a resulting area of 8.
1:47
Here, we make a call to perimeter,
with a length of 2 and a width of 4.
1:52
It adds the length and
width together, giving us 6, and
1:56
then multiplies the result by 2,
giving us a return value of 12.
1:58
Now this is one way to do it but this is
actually a little longer than necessary.
2:06
It's not Idiomatic Ruby.
2:10
We can shorten this up by simply returning
2:12
the result of the math operations.
2:16
So we can get rid of the variable
that we're storing everything in, and
2:21
just return the results
of the math operation.
2:25
Let's try saving this and
running it again.
2:30
And you see we get the same results.
2:33
But we can actually make
it even shorter than that.
2:38
The last expression that gets
evaluated within the body
2:41
of a Ruby method becomes
the return value of that method.
2:45
So we can actually take
the return keyword off here.
2:49
Length times width will be the last
expression we evaluate within the area
2:53
method, and so this will return the length
and the width that were passed in.
2:57
We can do the same down here in perimeter,
3:03
the last expression that we evaluate
here is 2 times length plus width.
3:05
And that just becomes the return
value of the perimeter method.
3:10
So let's save this, try running it again.
3:14
And again we get the same result.
3:18
This is a much more succinct
way to write methods in Ruby.
3:20
And if your methods are short,
3:24
you should consider leaving off
the return keyword altogether.
3:25
I hope you got in some good Ruby practice.
3:30
See the teacher's notes for
some other experiments you might try.
3:32
Have fun!
3:35
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up