🥳 GOSKILLS TURNS 10: Get 10 days of free access with code 10YEARS

GoSkills
Help Sign up Share
Back to course

Arithmetic Operators

Quiz me Quiz Compact player layout Large player layout
Focus video player for keyboard shortcuts
Auto
  • 720p
  • 540p
  • 360p
1.00x
  • 0.50x
  • 0.75x
  • 1.00x
  • 1.25x
  • 1.50x
  • 1.75x
  • 2.00x
cc

We hope you enjoyed this lesson.

Get the Intro to Ruby Programming course for more great video tutorials.

Start free trial

Cool lesson, huh? Share it with your friends

Facebook Twitter LinkedIn WhatsApp Email

  • Lesson resourcesResources
  • Quick referenceReference
  • Transcript
  • Notes

About this lesson

How to do basic math with Ruby.

Exercise files

Download this lesson’s related exercise files.

Arithmetic Operators
58.6 KB
Arithmetic Operators - Solution
58.7 KB

Quick reference

Arithmetic Operators

Doing basic math with Ruby is a breeze.

When to use

There are many different reasons for using math when coding, you'll use this a lot!

Instructions

Here are the basic math operators:

+  Addition

-  Subtraction

*  Multiplication

/  Division

**  Exponents

%  Modulus

Hints & tips

  • Math is super easy with Ruby!
Login to download
  • 00:04 In this video, I want to talk about math operators, or arithmetic operators.
  • 00:08 So, it doesn't really matter what programming language you are using,
  • 00:11 you're always going to need to do math.
  • 00:13 And Ruby makes math really easy,
  • 00:15 most programming languages do math pretty easily, but Ruby's especially easy.
  • 00:19 And so, I'm just going to dive in and start showing you this stuff.
  • 00:21 So let's comment, let's say math operators.
  • 00:25 So anytime you want to do math you use an operator.
  • 00:28 And an operator is just in this case plus sign,minus sign,
  • 00:33 multiplication, division, exponents and the modulus.
  • 00:38 And if you're not familiar with the modulus,
  • 00:40 I'll show you that in just a minute.
  • 00:48 So to do math in Ruby, is very simple I'll just put puts, and let's go 2 plus 5.
  • 00:55 Now the first thing you'll notice is the numbers that I used I didn't put them
  • 00:59 in quotation marks.
  • 01:00 Up here when we output a string to the screen we use quotation marks, but
  • 01:04 a number is not a string they're two very different things.
  • 01:07 So you don't use quotation marks.
  • 01:09 We save this, come down here, we're in our file, we get 7, 5 + 2.
  • 01:15 And it's really just that easy to do math with Ruby.
  • 01:18 We can switch this around and save, 5 minus 2, save it, run it and we get 3.
  • 01:25 Likewise, multiplication 5 times 2, 10.
  • 01:30 And if we change this around, 10 divided by 2 is going to be what?
  • 01:34 5.
  • 01:35 There we go. Exponents, do you remember exponents back
  • 01:39 from grade school 4 to the 2nd power is going to be what?
  • 01:42 16.
  • 01:44 And the modulus might be something you're not familiar with, but it's pretty useful.
  • 01:48 A modulus finds the remainder.
  • 01:50 So remember in long division back in fourth grade math,
  • 01:53 where if you divide something by another thing and there was something leftover.
  • 01:58 So for instance, say 10 divided by 3.
  • 02:02 Well 3 goes into 10 three times with 1 leftover.
  • 02:05 So a modulus would return 1, it returns the remainder.
  • 02:09 So if we save this and run the program, we get 1.
  • 02:13 If we change this to two 10 is divided by 2 five times with 0 leftover or
  • 02:18 0 remainder.
  • 02:19 So if we run this we'll get 0.
  • 02:22 So those are math operators, very, very easy, very, very useful.
  • 02:25 And here is something that's kind of interesting about Ruby, and
  • 02:28 not all programming languages work like this, but this is actually really cool.
  • 02:31 So if we have a string and I'm just going to type in what's my name John Elder
  • 02:36 and we go times to what do you think's going to happen here?
  • 02:40 Normally you would get an error with a normal programming language.
  • 02:42 But in Ruby, it multiplies the string times 2 and you get John Elder,
  • 02:46 John Elder.
  • 02:47 Now this is kind of useful because we can also do weird things like, is cool.
  • 02:54 Save this and run it.
  • 02:56 We get John Elder is cool.
  • 02:58 It adds these two things together.
  • 03:01 That's the sort of interesting thing about math and
  • 03:03 math operators with Ruby that's maybe not the same with other programming languages.
  • 03:07 Some programming languages will do this, but a lot of them won't.
  • 03:10 So those are math operators, very cool.
  • 03:12 In the next video, we're going to look at floats and integers.

Lesson notes are only available for subscribers.

Comments and Errors
04m:07s
Floats and Integers
03m:20s
Share this lesson and earn rewards

Facebook Twitter LinkedIn WhatsApp Email

Gift this course
Give feedback

How is your GoSkills experience?

I need help

Your feedback has been sent

Thank you

Back to the top

© 2023 GoSkills Ltd. Skills for career advancement