Lesson Weekend

Ruby has four kinds of variables. In this lesson, we’ll focus on local variables, which we’ve already used in JavaScript. Here’s an example of a local variable in Ruby:

name = "Fuzzy"

There’s no need for var. The Ruby interpreter reads this line and realizes we’re trying to assign a value to a variable.

We can pass the return value of a method into a variable. For instance:

number = 5
favorite_number = number + 8

In Ruby, variables should be lowercased unless they are constants. We will discuss constants later in this lesson. When a variable is multiple words, the Ruby convention is to use an underscore _. No more lower camel case!

When we call a method, the object that the method is called on is known as the receiver. Some methods change the receiver while others don’t. Here are a few examples:

> greeting = "hello"
=> "hello"
> greeting.upcase()
=> "HELLO"
> greeting
=> "hello"
> greeting.upcase!()
=> "HELLO"
> greeting
=> "HELLO"

We stored the string "hello" inside a variable and then called upcase() on it. However, upcase() didn’t change the receiver (in this case, the variable where the object is stored). On the other hand, upcase!() permanently changed the value of the receiver.

What’s the difference between these methods? The first method doesn’t change the receiver in place. Instead, it has a return value of "HELLO", but that’s a different string. The second method is what is known as a bang method. In Ruby, adding an ! turns a method into a bang method. Bang methods are usually destructive, which means they permanently change the receiver. Note that this is usually but not always the case; a bang really just signifies that the method is significantly different than the non-bang method and might be “dangerous."

Note that we could have updated our greeting without using the bang method. We’d do so by passing in the return value of greeting.upcase() into a variable. For instance, we could do this:

upcased_greeting = greeting.upcase()
=> "HELLO"
> upcased_greeting
=> "HELLO"

This is a more common approach than using a bang method.

Many methods don’t have a bang equivalent, and many non-bang methods are also destructive. For example, the Array methods push(), shift(), unshift() and pop() all permanently change the receiver they’re called on.

Constants


If we want our variable to be a constant, we should capitalize it instead. Here’s an example:

FREEZING_POINT = 32

Since the freezing point shouldn’t change, we can make it a constant. While this signals our intentions to other developers, Ruby actually doesn’t care if we change the constant or not. You probably won’t be using constants much at Epicodus (if at all), but if you do, make sure you communicate your intentions by capitalizing the variable.

Symbols


Let’s look at one other class: Symbol. It’s unlikely that you’ll be creating your own symbols much, but you’ll see them regularly in the code around you, including in Rails. Here's an example of a Symbol:

> :i_am_a_symbol

A Symbol is kind of like a String:

> :watermelon.length()
=> 10
> :peach_pie.upcase()
=> :PEACH_PIE

However, they lack many of the methods of String and will return a NoMethodError:

> :cucumber.reverse()
=> NoMethodError: undefined method 'reverse' for :cucumber:Symbol
> :milk.concat(:shake)
=> NoMethodError: undefined method 'concat' for :milk:Symbol

The main difference between a String and a Symbol is that a symbol is immutable. It can't be changed.

A symbol is also more efficient and takes less system memory. You can see this for yourself by going into irb. Try typing in "string".object_id, then type in the same command again. You'll see the object_id has changed; Ruby has allocated additional memory for it.

This makes sense because we could have two first_name strings in our application that are both "John". However, if you do the same thing for :symbol.object_id, the object_id doesn't change. Ruby allocates memory for the symbol the first time it's used and then refers back to that instance each time the symbol is called again.

If symbols seem a bit confusing at first, don’t worry. You probably won’t be using them much in your code, but at the very least you’ll have some basic context when you see them used in Rails, Active Record and elsewhere.

Terminology


Bang method: A method with an ! at the end. These methods usually permanently change the receiver.

Constant: A variable that doesn't change. Constants are capitalized like this in Ruby: THIS_IS_A_CONSTANT.

Destructive: When a method permanently changes a receiver.

Local variable: A variable in a local scope. Local variables are usually scoped to methods where they are created.

Receiver: The object that a method is called on. Methods always have a receiver in Ruby.

Symbol: These are immutable. Symbols are written with a : like this: :i_am_a_symbol. They are very efficient and are often used in programs like Rails.

Lesson 3 of 10
Last updated more than 3 months ago.