Chapter 1: Newly Ruby And so the journey begins...

In today's world, Object-Oriented Programming is pervasive but every object-oriented language is different in what it are objects how it treats them. This can get confusing so you'll be happy to hear that, in Ruby, EVERYTHING is an object. Take a look here:

puts 'underpants'.length


So we have this word 'underpants' and we have this new thing .length as well. If we run this simple program, Ruby will output 10. That's because the object 'underpants' has a length of 10 characters.

puts 'nirvana'.capitalize


In this example, try to think what Ruby will display on the screen. If you were thinking Nirvana then you would be correct. Both .length and .capitalize are called methods. Methods are actions by objects. So when the method .length acts on the object 'underpants', Ruby checks the length of the object and gives us the resulting 10. We will learn more about methods later in this lesson. We will also return to objects in Ruby in Lesson 3 after you have become Duly Ruby.

Ruby is capable of doing a lot of things without any special configuration, like arithmetic. Let's start out with something simple: 2 + 2

puts 2 + 2


This program puts the result of the expression 2 + 2 which Ruby has automatically calculated. As far as we're concerned, the code above is the same as puts 4.

There are two kinds of numbers in Ruby. There are integers, which are signed numbers like 342, -1, 0, and 23_000. Then there are floating-point numbers or floats, which are numbers with decimal points like 346.05, 0.0, and -76.8. For the most part we only need to concern ourselves with integers in Ruby, since floats are usually reserved programs that require extreme precision like in scientific experiments.

Here are some of the mathematical operators in Ruby:

4 + 3 #addition

843 - 14 #subtraction

7 * -3 #multiplication

934765 / 5 #division

59 % 3 #modulo division



It's of course worth mentioning that the division represented by / is integer division. So an expression like 11 / 2 would evalute as 5.

These numbers and operators are great for mathematic calculation (duh), but we'll soon see that they play well with others.

Strings are lines of text between either single (') or double quotes ("). Just like numbers, in Ruby strings are objects so they behave accordingly. Let's take a look at the following examples:

puts 'Hello world!'


Here we see possibly the most prevalent computer program ever written. It prints the string object 'Hello world!' to the screen. Strings aren't only just letters, they also can include symbols, digits, punctuation. and spaces. In the next example you can see that you can combine strings with the + operator.

puts 'Hello ' + 'world!'


This is not unlike other languages that use + as a concatenation operator. However, in most languages how the + operator works depends on the type of data it is connecting. When the operands are numbers the operator usually performs the addition function, and when they are strings it performs concatenation. Yet you can't combine the two. Because strings are objects in Ruby, we are afforded a lot of flexibility including the ability to do string arithmetic. Pay attention, you are about to see some of Ruby's magic:

puts 'I am Ruby, and Ruby is me.' * 3


There it was! Did you see it? Look again.

In this example we are printing 'I am Ruby, and Ruby is me.' to the screen 3 times. But that's not the crazy part! The important thing is that strings and numbers are interacting. This doesn't happen in most languages. This is great! Next you will learn that there are some formulations of strings and numbers don't interact well.