Numbers, Operators and Expressions

The nuts and bolts of basic Ruby

Scroll down...




Your success in this course will be dependent on your comfort level with Ruby. With that in mind, the lessons of this section are a whirlwind (re)introduction to basic Ruby concepts. You've already seen many of them before but there will also be many new ones. Consider the familiar ground a good chance to hammer in the fundamentals while the new stuff will help you begin to level up further.

Our goal with this section is to make sure you've got a solid base in Ruby fundamentals before taking on the heavy lifting we'll do inside the program.

With the knowledge we present here, you shouldn't have much trouble solving all of the upcoming challenges.

Our review of basic Ruby concepts starts at the very beginning with numbers, operators, and expressions. This should be a piece of cake!


Ruby has all the standard arithmetic expressions:

> 1 + 1  
#=> 2
> 10 * 2 
#=> 20
> 35 / 5 
#=> 7
> 10.0 / 4.0 
#=> 2.5
> 4 % 3 
#=> 1  #Modulus
> 2 ** 5   
#=> 32 #Exponent 

When doing mathematical operations, Ruby expects the result to be the same type as the inputs, so dividing two integers by each other will produce an integer... whether you want to or not:

> 5 / 3
=> 1  

To fix this, you need to make one of the inputs a different data type that can handle decimals, like a floating point number (float):

> 5.0 / 3               # if one of them is a float...
=> 1.6666666666666667   # ... the result is a float

Converting between integers and floats is easy -- just use to_i and to_f respectively:

> 5.0234.to_i
=> 5
> 5.to_f
=> 5.0

Because Ruby is so flexible, it lets you do some quirky things like multiplying completely different data types together in a way that you sort of think you should be able to but never expected to actually be able to do:

> "hi" * 3
=> "hihihi"

These types of operations work the same way with variables:

> my_word = "howdy"
=> "howdy"
> my_word * 3
=> "howdyhowdyhowdy"


A Range is just a continuous sequence and we represent it in a shorthand way. If we want to say 3, 4, 5, 6, 7, 8, 9, 10, 11, it's much easier to just write it the short way (3..11), meaning "all the integers beween 3 and 11, including both 3 and 11". If we wrote it (3...11), it would actually exclude 11 You can also create a range using, finish_num)

...though the shorthand notation is more conventional. It's also quite uncommon to use the exclusive (...) notation so stick with inclusive ranges.

Equality: ===, ==, and =

= is used for assignment, so it assigns a value to a variable as in:

> name = "Sven"
=> "Sven"

== is used for checking that two things are equal but don't have to be identical instances. You'll use this in most cases, especially when working with conditional statements.

> 1 + 1 == 2
=> true

When you start creating your own classes (like an "Animal" class), you'll need to tell Ruby how to compare two animal instances by writing your own version of this method (it's easy).

=== can actually mean several different things and you can overwrite it easily. You will probably be sticking with == in most situations, but it's good to understand === as well.

=== typically asks whether the thing on the right is a member or a part or a type of the thing on the left. If the thing on the left is the range (1..4) and we want to know if 3 is inside there:

> (1..4) === 3
=> true

This also works for checking whether some object is an instance of a class:

> Integer === 3
=> true

Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

Sorry, comments aren't active just yet!

Next Lesson: Objects and Methods