Ruby Basics : Everything is Not an Object

In this article you will learn that every sender and receiver in a message passing interaction is an object.

Object

Number is an Object

Let's ask Ruby for the class of the number 1.

1.class

This prints:

Fixnum

Fixnum is the class used to create an instance of the number one. The number is an object. You can send messages to them.

1.odd?

This prints:

true

String is an Object

String is a sequence of characters strung together. Let's ask Ruby for the class of a string object.

'hello'.class

This prints:

String

We can send messages to the string object 'hello'.

'hello'.reverse

This reverses the string to print:

olleh

Array is an Object

Let's ask Ruby for the class of an array.

[1,2,3,4].class

This prints:

Array

We can send messages to the array object.

[1,2,3,4].reverse

This prints:

[4, 3, 2, 1]

Hash is an Object

Let's ask Ruby the class of a hash.

{a: 1, b: 2}.class

This prints:

Hash

We can send messages to the hash object.

{a: 1, b: 2}.keys

This prints:

[:a, :b]

Messages are Not Objects

The messages we send to an object is not an object. But, we can convert them to an object. Let's convert the keys() message that we sent to a hash to a Method object.

keys_method = {a: 1, b: 2}.method(:keys)
 => #<Method: Hash#keys>
 > keys_method.class
 => Method

We can convert a message to a method object by sending method message to a given object with the argument of the method name as the symbol. In this example, it is of the following format.

hash_object.method(:method_name)

The method_name argument is a symbol. We can now use the keys_method object to invoke the keys() like this:

keys_method.call

This prints:

[:a, :b]

Conditionals and Loops are Not Objects

Control structures do not have special syntax in Smalltalk. They are instead implemented as messages sent to objects. For example, Smalltalk implements if-else by sending a message to a Boolean object. In Ruby, control structures such as if-else, for, while etc. are not objects. For an in-depth discussion on this topic, read Flexing Your Message Centric Muscles.

Rhonda Asks

Why are we comparing Smalltalk with Ruby?

Ruby was influenced by many languages, Smalltalk is one of them.

Fabio Asks

Are blocks objects in Ruby?

The short answer is No. Blocks are not objects in Ruby. We need to use Proc, lambda or literal constructor ->, to convert blocks into objects. In Smalltalk, blocks are objects. The statement: Everything is an object is true for Smalltalk but not for Ruby. We will discuss this topic in detail in the Closures article.

Summary

In this article, you learned that everything is not an object in Ruby. It is much more accurate to say: Every sender and receiver in a message passing interaction is an object. You can explore more objects in Ruby such as Symbols, Integer, Float etc. You can browse the Ruby documentation and experiment.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.