Learn Ruby - Variables, Strings, Numbers

Question Click to View Answer

Assign the variable x to the value 5.

x = 5

x is called a variable and 5 is the value. x is called a variable because it can be reassigned to another value.

What is the datatype of "Matt"?

"Matt" is a String. Strings are text that are surrounded by double or single quotes ("Matt" and 'Matt' are acceptable syntax).

x = 5

Is x a string?

x is a variable and is not a string. x is assigned to the value 5 and 5 is an Integer, not a String. Notice that variables are not surrounded with quotation marks.

What does this expression evaluate to?

"YO" + "LO"
"YOLO"

This is called string concatenation.

What does this expression evaluate to?

"Cool".+("io")
"Coolio"

This is another way to concatenate Strings. The previous example actually uses syntactic sugar (a deviation from formal syntax to enhance code readability) and this example uses the strict syntax.

What does this expression evaluate to?

"bob" + 42

This raises an error because a number (Integer to be precise) cannot be concatenated with a String.

a = "Bat"
b = "woman"

What does this expression evaluate to?

a + b
"Batwoman"

When variables are assigned to values, the variable can be used in place of the value.

What does this expression print?

my_name = "50 Cent"
my_name = "Miley"
p my_name
"Miley"

The my_name variable was assigned to the value "50 Cent", but then it was reassigned to the value "Miley". Once the my_name variable is reassigned to "Miley", it loses all knowledge that it used to point to "50 Cent".

rapper = "Jaydakiss"

Get the first letter from the string "Jaydakiss".

"Jaydakiss"[0]
# OR
rapper[0]

Basically everything in computer science is 0-indexed, so the counting starts at zero, not at 1.

Get the first through 3rd elements from the "Jaydakiss" string.

"Jaydakiss"[0..2]

Get the last element from the "Jaydakiss" string.

"Jaydakiss"[-1]

Replace the "l" in the following string with "m":

word = "lace"
word[0] = "m"
puts word
=> "mace"

Assign the variable my_dawg to the value "DMX"

my_dawg = "DMX"

What does the following expression evaluate to?

"Dead Poet" = fav_bar

This raises an error. When variables are assigned, the variable must be on the left, follow by an equal sign, followed by the value.

What does the following expression print?

something = "cats"
crazy = something
puts crazy

This will print "cats" because the variables something and crazy both point to the same value.

What does the following expression evaluate to?

3 + 4
7

What does the following expression evaluate to?

4 * 7
28

What does the following expression evaluate to?

2 ** 3
8

** is used for exponents and 2 ** 3 is 2 to the power 3 or 2 * 2 * 2.

What does the following expression evaluate to?

8 / 2
4

What does the following expression evaluate to?

3 / 2
1

This outcome is not 1.5 because we are performing Integer division (division between two Integers) and Integers do not have any decimals. Division between two integers always results in an integer.

What does the following expression evaluate to?

3.0 / 2.0
1.5

This outcome is 1.5 because we are performing division on Floating point numbers that have decimals.

3.0 / 2 and 3 / 2.0 also return 1.5.

What does the following expression evaluate to?

"i am not shouting".upcase()
"I AM NOT SHOUTING"

This shows that Ruby has nifty methods that are built in to the String class. The syntax for using the built in methods is the value, followed by dot, followed by the method name.

Convert every letter of "YoLo BrAh" to lowercase.

nice = "YoLo BrAh"
nice.downcase()
# OR
"YoLo BrAh".downcase()

Concatenate the following strings:

first = "Beautiful "
second = "face tattoo"
first + second
# OR
first.+(second)
# OR
first.concat(second)

The +() and concat() methods do the same thing (they're not exactly the same, but very similar). Notice the similarity with the previous examples: there is a string, followed by a dot, followed by the method names with another string as an argument.

Integers have useful built-in methods too. Convert the number 5 to the string "5".

5.to_s

to_s stands for "to string"

What is the problem with the following code?

my variable = "Mr. White"

This will raise an error because variables cannot have spaces in them. Ruby convention is to combine multi-word variable names with an underscore.

my_variable = "Mr. White"

Update the code, so it can run successfully:

band = "Blink" + 182

We cannot concatenate a String and an Integer. However, we can convert the Integer to a String first and then concatenate the values as two Strings.

band = "Blink" + 182.to_s