Skip to content

Instantly share code, notes, and snippets.

@rpearce
Last active May 10, 2022 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpearce/e4157247aa9609b2a7f9 to your computer and use it in GitHub Desktop.
Save rpearce/e4157247aa9609b2a7f9 to your computer and use it in GitHub Desktop.
Ruby Fundamentals - Operators

Operators

Ruby makes use of mathematical operators, such as plus and minus signs, to perform many different functions. Plus and minus signs, might seem straightforward when dealing with numbers, but they can have many different applications depending on whether you're working with numbers or arrays, for example. Additionally, equals signs are not always what they seem! In this lesson, we will dive in to a number of different uses of +, -, /, *, =, and more.

Computational

Just like a calculator, Ruby lets you do addition, subtraction, multiplication, division and more. Here are the symbols for each:

  • addition: +
  • subtraction: -
  • multiplication: *
  • division: /

As you might expect, 2 + 2 returns 4. You can also chain together as many expressions as you like:

2 + 2 - 3 * 15 # => -41

Because of grouping order, 3 * 15 is evaluated separately from 2 + 2, and so the above is the same as

(2 + 2) - (3 * 15) # => -41

The ( and ) characters allow you to explicitly group expressions together. An example of when this grouping comes in handy would be when you want to convert a Kelvin temperature to Fahrenheit (rounded to 2 decimal places):

# this should convert to 31.73˚ Fahrenheit
kelvin = 273
((kelvin * (9.0 / 5.0)) - 459.67).round(2) # => 31.73

Tip: Make sure to use decimal places, as above with 9.0 and 5.0, if you want the result to contain a decimal place, as well. Without at least 1 decimal place in an expression, then Ruby thinks you're only asking for an integer back, and so something like 9 / 5 will simply return 1, where you may have been expecting 1.8.

Assignment

Fear not! There is no homework here. Assignment, in Ruby operators context, refers to assigning/allocating a value to a variable:

x = 7 
x + 5 # => 12

+=, -=, *=, /=

In programming, you will often have to increment or decrement values by a certain amount and reassign our variable to the new value; this is also known as mutating (changing) the variable. Luckily, Ruby gives us shortcuts! Assuming x = 7,

x = x + 5
# can be shortened to
x += 5

You can do this for multiplication, subtraction, and division, as well:

  • x -= 5 is x = x - 5
  • x *= 5 is x = x * 5
  • x /= 5 is x = x / 5

Tip: You can also use += for concatenating strings!

output = "This"
output += " is"
output += " a"
output += " sentence!"
output # => "This is a sentence!"

Memoization

Assume that we have a username variable that we need to ensure is set to at least value, but that we don't want to overwrite if it gets set somewhere else. We can do this by assigning a variable to itself and, if the variable is not assigned to anything, provide a default of "Guest":

username = username || "Guest"
# or using the shorthand
username ||= "Guest"

When you use the || operator with variable assignment, if the value on the left of it is nil or false (falsy), then it will evaluate the code on the right hand side and assign that to the variable. With this concept, you can make sure you don't overwrite a sensitive value if it is set somewhere else.

Equality

When you need to determine if one value is equal to another value, you use ==:

x = 7 + 5
x == 12 # => true

If you instead want to ask if x is not equal to 12, you would write:

x = 7
x != 12 # => true

Logical

When we ask questions in the real world that include mandatory conditions, we tie those conditions together typically with the word "and." For example, when a student asks, Will my jetpack get me to school today, and will I have a safe landing?, then if both of those are accurate, the answer to the question is true. However, if either of these are not true, then the entire question is untrue. In Ruby, we use two ampersands, &&, to determine if statements on the left and right of the ampersands are both neither false nor nil (also known as truthy). Here's what that looks like:

7 > 5 && 12 < 13 # => true
7 < 5 && 12 < 13 # => false

Alternatively, if we are only care that one condition is truthy, then we use the "or" operator, ||:

7 > 5 || 12 < 13 # => true
7 < 5 || 12 < 13 # => true

These two logical operators can be summed up in these points:

  • && (are both this and that truthy?)
  • || (is this or that truthy?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment