Values, Types, Operators

What kind of information / data can a variable store in Ruby? Numbers, Strings, Boolean.

  • Numbers are written as 12 or 9.45
  • Strings are written as "awesome"
  • Booleans are written as true or false

Numbers

The main thing to do with numbers is arithmetic. You can add, subtract, multiply and divide numbers in Ruby.

Arithmetic operations:

1 + 1 => 2
2 - 1 => 1
3 * 4 => 12
4 / 2 => 2
5 % 2 => 1
2 ** 3 => 8

% is the modulo method which gives you the remainder value when you divide one number with another.

** is the exponent method, it performs exponential calculation. In other words, it returns 2 to the power of 3.

Other useful methods:

Method Description Example
abs Return the absolute value of num. 12.abs => 12, -34.56.abs => 34.56
to_s Return a string containing the number. 12345.to_s => "12345"
odd? Return true if is an odd number. 7.odd? => true
even? Return true if is an even number. 8.even? => true

Strings

The next basic data type is the string. Strings are used to represent text. They are written by enclosing their content in quotes.

"Hello, my name is ..."

Both single and double quotes can be used to mark strings as long as the quotes at the start and the end of the string match. "Hello World" and 'Hello World' both work.

Almost anything can be put between quotes, but a few characters are more difficult. You can imagine how putting quotes between quotes might be hard. Newlines (the characters you get when you press Enter) also can’t be put between quotes. The string has to stay on a single line.

To make it possible to include such characters in a string, the following notation is used: whenever a backslash (\) is found inside quoted text, it indicates that the character after it has a special meaning. This is called escaping the character.

\n represents a new line, so if you do "This is the first line\nAnd this is the second", the final presentation of the text is this:

This is the first line
And this is the second

Notice that \n doesn't show up

A quote that is preceded by a backslash will not end the string but be part of it. For example, "Say \"Hello!\"" will show up as:

Say "Hello!"

Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them. It does not add, but it concatenates, which means that it joins two strings together. So, if you want to join "ru" and "by", you just do "ru" + "by" => "ruby". Also, if you want to repeat the same characters in a string, you can do "abc" * 5 => "abcabcabcabcabc".

Other useful methods:

Method Description Example
to_i Convert the string into an integer. "1234".to_i => 1234, "hello".to_i => 0
upcase Return a copy with all lowercase letters replaced with their uppercase counterparts. "hEllo".upcase => "HELLO"
downcase Return a copy with all uppercase letters replaced with their lowercase counterparts. "HeLLO".downcase => "hello"
capitalize Return a copy with the first character converted to uppercase and the remainder to lowercase. "hello".capitalize => "Hello"
reverse Return a new string with the characters from str in reverse order. "abcd".reverse => "dcba"

Boolean

You must be familiar with TRUE or FALSE questions in exams. A boolean can store either true or false.

Comparisons

One way to produce Boolean values is to compare two values.

For example,

3 > 2
=> true
4 < 1
=> false

The > and < are for "is greater than" and "is less than", respectively.

There are other comparison operators:

  • >=: greater than or equal to
  • <=: less than or equal to
  • ==: equal to
  • !=: not equal to
4 == 5
=> false
4 != 5
=> true

Logical Operators

Ruby supports three logical operators: and, or and not.

1 + 1 == 2 and 2 + 2 == 4
=> true
1 + 1 == 2 or 1 + 1 == 3
=> true

Since we used or, we only need 1 of the 2 statements to be true. Since 1 + 1 == 2 is true, the logical operator will give us true.

Symbols

A symbol looks like a variable name but it's prefixed with a colon. Examples are :dog and :cat. Symbols are used as consistent names within code. Symbols are more efficient than strings because symbols are single reference values that are only initialized once no matter how many times you use them.

Summary

Values represent data and information that we want to use. There are three main types of value: Numbers, Strings, Boolean. We can manipulate values using methods and compare them using operators.

results matching ""

    No results matching ""