I Objects

1. An HTML Document

An HTML document is a collection of elements nested within each other. The elements in the head contain information about the document itself, and the elements in the body contain the text, images, and other pieces of information to be displayed to the viewer.

Here’s a simple HTML document.

<html>
 <head>
    <title>I Love HTML!</title>
  </head>
  <body>
    <h1>I Love HTML!</h1>
    <p>This is a paragraph.</p>
  </body>
</html>

When you open it in a web browser, you don’t see any of the HTML code. None of the tags or attributes are printed on the page. Instead, the data from the HTML document is transformed by the browser and presented to the viewer.

HTML is the format required to display content in a web browser. You don’t need to write HTML to make a web page. If you have a way to convert your data into HTML before handing it to the browser, you can store it in a spreadsheet, a text file, or your own data format.

2. The Markdown Format

The markdown text format is a great example of this. Markdown is much easier to read than straight HTML.

NOTE: This book is written entirely in markdown!

# Hello World!
## This is my web page

I hope you like it!

Upon converting it to HTML, you get the following markup.

<h1>Hello World!</h1>
<h2>This is my web page</h2>
<p>I hope you like it!</p>

If you’d like to learn more about markdown, You can use a tool like http://dillinger.io to create a markdown document and convert it into HTML.

In the upcoming chapters, we’ll explore another way to represent a web page. Since this is a book about object-oriented programming in Ruby, we’ll do it using objects!

3. Objects

An object is an entity that has its own data and behavior. In Ruby, just about everything is an object.

Numbers

Numbers are objects that can be used in mathematical calculations.

NOTE: The Ruby interpreter ignores everything after the #. These are called comments. They are used to explain code to the reader. In the code samples throughout this book, comments are used to show the result of certain operations.

1 + 1            # => 2
42 * 2           # => 84
3.14159.round(2) # => 3.14

Strings

Strings are objects that represent a collection of characters. We can reverse them, change their case, and append more characters to them.

'hello'.reverse  # => "olleh"
'hello'.upcase   # => "HELLO"
'hello' << '!'   # => "hello!"

Whenever we create a string, a new object is created even if the string has the same value as a string that already exists.

'hello'.object_id # => 3
'hello'.object_id # => 84

Symbols

Since strings are so widely-used in programming, Ruby provides a special, immutable (unchangeable) object that can be used in place of a string. This is object is called a Symbol. Symbols must start with a :. Whenever we use :hello, it always references the same object.

:hello.object_id # => 584
:hello.object_id # => 584

Using symbols in place of frequently-used strings prevents the creation of extraneous objects. Symbols are used in place of strings throughout this book.

Booleans

Boolean objects represent true and false values. When we ask an object a yes-or-no question, it answers with a boolean object.

3.even? # => false
'hello'.start_with? 'h' # => true

Nil

The nil object is a special object that represents nothing. When we try to get the location of a letter within a string that does not contain that letter, the result is nil.

'hello'.index('j') # => nil

These are just a few types of objects that Ruby provides. As you’ll see in coming chapters, we can even create our own types of objects.

4. Variables

Variables are memory locations that hold objects. We can name them and use them to access our objects. The standard way of naming variables is snake case: lowercase letters separated by underscores.

my_string = 'hello'
my_string.length # => 5
my_string.upcase # => "HELLO"

5. Messages

Objects interact with each other by sending messages. When an object receives a message, it responds by behaving in a certain way. All of the following statements do the same thing.

'hello'.reverse         # => "olleh"
'hello'.send('reverse') # => "olleh"
'hello'.send(:reverse)  # => "olleh"

Because it’s much easier to type object.message than object.send('message'), send is only used in special cases.

If we send an object a message that it doesn’t understand, Ruby will raise a NoMethodError.

'hello'.blah # => NoMethodError: undefined method 'blah'