Lesson Weekend

Welcome to Ruby! We'll start with one of the most famous and widely known sayings in computer programming.

"Hello world!"

In Ruby, this is an object. In fact, everything in Ruby is an object. 5 is an object. So is "Hi!" and [2,3,4].

Strings


So what kind of object is "Hello world!"? Let's open IRB and find out. Type this in your prompt:

> "Hello world!".class()
=> String

We just called a method on "Hello world!" Method is just another term for a function that is called on an object. In JavaScript, we often call these functions. In Ruby, we call them methods because everything is an object and all functions are called on objects. All methods are functions, but the reverse isn't necessarily true.

"Hello world!" is an instance of the class String. We can make a string in Ruby by enclosing a sequence of characters in either single or double quotation marks. These characters can include letters, numbers and symbols.

All objects are instances of a class. In fact, classes are objects, too. Ruby has a number of commonly used classes; String is one of them. We'll be covering a number of other classes in the next few lessons in addition to String, including Array, Number, Float, Range and Hash. Most of these classes (such as String and Number) are already familiar to you from JavaScript.

Let's see what methods are available to "Hello world!"

> "Hello world!".methods()

You'll see a wide range of methods from concat() to reverse() to length(). Let's try a few methods out.

> "Hello world!".length()
=> 12
> "Hello world!".reverse()
=> "!dlrow olleH"
> "Hello world!".concat("!!!")
=> "Hello world!!!!"

length() returns the total number of characters in the string. reverse() reverses the string. And concat() works just as it does in JavaScript: it concatenates two strings together.

In the last example, we passed an argument to concat(), just as we passed arguments into functions in JavaScript.

An interesting side note: adding 1+3 is an example of passing an argument to a method and calling it on an object. When we call 1+3, we’re actually calling 1.+(3). 1 is the object the method is called on, + is the method, and (3) is the argument. One of the wonderful things about Ruby is that it provides a great deal of syntactic sugar to make it easy to write code. Syntactic sugar is exactly what it sounds like: a sweetener added to the language to make it more pleasant to read and write.

Number and Float


Let's take a quick look at the Number class and try out a few methods available to instances of Number:

> 5.even?()
=> false
> 5.next()
=> 6
> 5.positive?()
=> true
> 5.methods()

Float is quite similar to Number. It's really just another word for decimal.

> 5.3.class()
=> Float
> 5.3.round()
=> 5
> 8.1.ceil()
=> 9

Calling the round() method on a Float will tell it to round up or down to the nearest integer. The ceil() method is just like round(), except that it only rounds up.

Chaining Methods


When we call a method on an object, we get a return value. The return value of 8.1.ceil() is 9, and it is also an object. Because it's another object, we can chain methods together:

> 5.3.round().next()
=> 6
> 5.3.ceil().next()
=> 7

Calling Undefined Methods


What happens if we try to call reverse() on a Number? We'll get an error.

> 15.reverse()
NoMethodError: undefined method `reverse' for 15:Integer

This is a very common error for Ruby beginners and you should familiarize yourself with it now. The error is self-explanatory: there is no reverse() method for instances of the Integer class.

Arrays


You’re already familiar with Arrays from JavaScript. An array is a collection of items. We can put objects of any class into an array. For example, we could do this:

> [1, 2, 3, "a", "b", "c", -1.1, -2.2, -3.3]

Let’s try out a few array methods:

> ["a", "b", "c"].length()
=> 3
> [1, 2, 3].shuffle()
=> [2, 3, 1]
> [1, 2, 3].push(4)
=> [1, 2, 3, 4]

Here’s where your familiarity with JavaScript should come in handy even though you’re learning a new language. You’ve already used methods like push(); in fact, this method operates exactly the same way in both Ruby and JavaScript. Here are a few other array methods that are functionally the same in both languages: shift(), unshift(), pop() and join().

Here are a few methods for returning a specific element from an array:

[1,2,3].first
=> 1
> [1,2,3].last
=> 3
> [1,2,3][1]
=> 2
> [1,2,3].at(1)
=> 2

The last two examples are functionally the same; they both return the element at position 1 of the array. Arrays start at position 0 just as they do in JavaScript and many other languages.

Ranges


Another useful class is Range. Ranges represent intervals, such as of numbers or letters.

> (1..10)
> ("a".."z")

Ranges have many methods, too. Here are a few:

> (1..10).size()
=> 10
> ("a".."z").end()
=> "z"

Summary


In Ruby, everything is an object, including classes. All objects belong to classes. We can communicate with objects by sending them messages called methods. We can pass arguments into methods and chain methods together.

We’ve also covered most of the basic built-in classes we’ll be working with. These include String, Integer, Float, Array and Range. We’ll cover the Hash class in a future lesson. We’ve only given a very brief overview of what we can do with instances of these classes; take some time on your own to explore Ruby’s documentation and see what methods are available within each class.

In Ruby, everything is an object, including classes. All objects belong to classes. We can communicate with objects by sending them messages called methods. We can pass arguments into methods and chain methods together.

Ruby built-in classes include String, Integer, Float, Array and Range.

Terminology


Chaining: A technique used to call multiple methods together.

methods(): A method to check what methods are available on the class an instance belongs to.

Return value: The value a method returns. Not all methods have a return value.

Syntactic sugar: a sweetener added to the language to make it more pleasant to read and write.

Lesson 2 of 10
Last updated August 7, 2022