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/e64ef4d9eaddfd143de4 to your computer and use it in GitHub Desktop.
Save rpearce/e64ef4d9eaddfd143de4 to your computer and use it in GitHub Desktop.
Ruby Fundamentals - Data Types: String, Numbers, and Booleans

Strings, Numbers & Booleans

Programming languages, just like the real world, group different types of information separately. For example, if you want to add together the numbers 1 and 2, you will naturally get back the number 3. If you would rather maintain a list of people's names, then that list is a different type of information than the numbers are.

In this lesson, we will cover most of the Ruby information types and walk through examples of their most commonly used bits of functionality.

Strings — "Hello!"

Ruby strings look a lot like quotations from novels. Here is an example of a quotation from a fictitious novel:

Mom said, "Have fun! Please don't forget to wear your helmet when you're jet packing!"

What exactly is a quotation? It is a series of letters, numbers and symbols that are wrapped within quotation marks in order to group them together in a human-readable way.

Ruby strings can be defined in the same way. How would we write that same quote in Ruby and have it outputted to the Terminal?

print "Have fun! Please don't forget to wear your helmet when you're jet packing!"

Escaping Special String Characters

If you run the code print "She said, "Have fun..."", you will receive a syntax error, for the Ruby interpreter thinks you are trying to close out the string right before the "H, when in reality you just want it to print literal quotation marks and not do anything fancy. In order for us to be able to use print double quotes from within a double quote string, we need to prepend a \ to the quotation marks that we want Ruby to print to the Terminal:

print "She said, \"Have fun...\""

Ruby also allows you to specify other types of characters that can tell your Terminal how to format output, such as new lines (\n) and tabs (\t). Additionally, you can use the puts command as an alternative to print to automatically add a \n to the end of every line:

print "Roses are red\nViolets are blue...\n"

# will produce the same output as

puts "Roses are red"
puts "Violets are blue"

Taking input with gets, inspect, and chomp

We can use name = gets command to prompt for a users's name in the Terminal and store that in a variable. However, if you were to ask the string to show us all characters via puts name.inspect, you would see a \n on the end of that string! Why is that?

When we accept user input using the gets command, we submit that input using the Enter or Return key. Unfornately, this causes our gets command to also store a hidden return character, \n (or sometimes \r), as a part of our string. To ensure that we keep our name string to just the characters related to our name, we can use the chomp command on our gets command to, like an alligator, "chomp" off the new line character (also known as a "carriage return") from our string. With chomp used alongside gets, the \n or \r characters are removed, and we are left with just our name.

Checking the length

With Ruby strings, you can use the length command to ask a string for how many characters it contains:

"Have fun!".length # => 9

Finding and Replacing Characters

You might need to search through a large string and replace certain characters or words with something else. You can use a global substitution command, gsub, to do this:

text = "We went down to the harbour."
text.gsub('harbour', 'harbor') # => "We went down to the harbor."

Interpolation and Combining Strings

When we have a variable, name, that represents a string of your name, and we need to place inside of a string and return its, we can do so by interpolation: "Hello! My name is #{name}." Another way of adding Ruby strings together is by concatenating them together, like this: "Hello! My name is " + name + "."

Numbers — 5 + 10

Math

Ruby can perform calculations, such as add, subtract, multiply, and divide and can also perform such operations in conjunction with variables. You can also group together expressions with parentheses.

13 + 37 # => 50
13 - 37 # => -24
13 * 37 # => 481
13 / 37 # => 0
1 + (3 / 3) + 7 # => 9

number = 13
number * 2 # => 26

Integers vs. Floats

Did you notice in the previous section that 13 / 37 returned the value of 0? You would expect "13 divided by 37" to return the decimal 0.35 (rounded). In Ruby, there is a distinct difference between a number that has a decimal point in it and one that doesn't. If a number has a decimal point, like 3.14, then it is considered to be a float, whereas numbers without decimal points are integers.

When 13 was divided by 37, Ruby saw each of them as whole numbers — integers — and assumed that since you provided two whole numbers, you likely wanted a whole number back as an answer. Since 37 does not evenly divide in to 13, Ruby rounded the actual result, 0.35135135135135137, down to 0.

Contrarily, if you tell Ruby to treat either value as a float, then the final value will also be a float.

13.0 / 37 # => 0.35135135135135137
13 / 37.0 # => 0.35135135135135137

Beware Math With Floats

What is the result of 0.3 - 0.2? Unfortunately, it is not 0.1; it's 0.09999999999999998. Why this is a problem is beyond the scope of this lesson, but there is a way to avoid this. Before performing calculations, convert your floats to integers, perform the calculations, and then convert the result back to a float.

a = 0.3
b = 0.2
((a * 10) - (b * 10)) / 10 # => 0.1

Booleans — true or false

Boolean values are binary and are either true or false. In the real world, you have probably come across other binary systems: "yes" and "no"; "on" and "off"; "they love me..." and "they love me not...". The most common method for interacting with booleans, outside of "control flow", to ask, "Is this value [or variable reference to it] equivalent to this other value?" When you prompt for equivalence between 1 or more things, the response you get back from Ruby will be a boolean value.

1 == 2 # => false

username = "janerules03"
username == "janerules03" # => true

"This is a great example".length > 3 # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment