Erik Trautman logo crest shield seal

Erik Trautman

Technical entrepreneurship, business strategy and product development

Ruby Explained: Numbers, Operators and Expressions

This post will be very brief look at some of the basic numbers, operators, and expressions in Ruby

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               # as long as 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
=> "hihihihi"

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 Range.new(start, finish), though the shorthand notation is more conventional.

For equality:

  • = is for assignment, so it assigns a value to a variable as in > name = "Erik"
  • == is 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 returns '=> 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 some different things (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

See http://stackoverflow.com/questions/4467538/what-does-the-operator-do-in-ruby for a more detailed explanation.


The "Ruby Explained" posts are designed to be a sort of "In-Plain-English" version of key Ruby concepts which are usually covered in other introductory texts but rarely for free and often incompletely. When I'm learning a new thing, I usually want someone to explain it to me like I'm a five year old because that's the best way to make sure nothing gets missed. This is my attempt to pass that same sentiment on to you. Let me know if there's anything I can improve.

If you're just getting interested in this stuff, check out The Odin Project for a free curriculum to learn web development.

Tags:   Ruby Explained