Ruby: 4 types of variables you need to know
Ruby’s variable naming conventions can often confuse people learning the language — Learn more ➕.

Editorial translation by Impulso.
If you’re just getting started with Ruby, variable naming conventions can be confusing, so this time we’ll talk a bit about them. Let’s get started!
Constants in Ruby
The first type of variable I’ll talk about, though, isn’t a variable… it’s a constant. 🙂
Constants, by definition, start with an uppercase letter and can only be assigned a value once. For example:
2.5.1 :001 > Nome = "Jackson" 2.5.1 :002 > Nome = "Pires" (irb):2: warning: already initialized constant Nome (irb):1: warning: previous definition of Nome was here
Notice that when we try to assign a new value, Ruby tells us that the constant has already been initialized, but it still takes on the new value. It may seem strange that Ruby allows us to assign a new value, since most languages don’t allow it, but Ruby issues a warning (not an error) to let us know about the change.
By convention, we usually write constant names in all caps. Even so, all the assignments in the example below define valid constants.
2.5.1 :001 > Nome = “Jackson” 2.5.1 :002 > CURSO = “Ruby on Rails” 2.5.1 :003 > NomeComposto = “Jackson Pires”
Local variables
Local variables are the ones we use most often in Ruby. To use one, just assign a value to the variable you want. Here’s an example:
2.5.1 :001 > a = “curso”
The important thing to understand about local variables is how their scope works. A local variable is usable only “locally,” where it was defined. Take a look at the example below:
2.5.1 :001 > a = “olá”
2.5.1 :002 > def somar_dois(x) 2.5.1 :003?> y = 2 2.5.1 :004?> puts x + y 2.5.1 :005?> end
2.5.1 :006 > puts a olá
2.5.1 :007 > somar_dois(3) 5
2.5.1 :008 > puts y Traceback (most recent call last): 2: from /usr/local/rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>’ 1: from (irb):8 NameError (undefined local variable or method `y’ for main:Object)
Looking at the code above, we assign a value to “a” right at the start. In lines 2 through 5, we define a method called somar_dois(), which declares the variable “y” on line 3 and assigns it the value 2. Next, line 6 prints the value of “a,” and line 7 calls somar_dois(), which uses “y” internally. But when we try to use “y” on line 8, Ruby raises an error saying it doesn’t exist. That’s because “y,” unlike “a,” “only exists” within the scope of somar_dois(), meaning inside the method.
Local variables can therefore be confined to the scope you’re working in. In our example, “y” exists only inside somar_dois(), and “a” is available throughout the algorithm because it was defined outside any method’s scope.
Global variables $
The name ‘global variables’ sounds self-explanatory, but it’s easy to get mixed up.
These variables are meant to be “shared” across all instances of a class. Here’s an example:
2.5.1 :001 > class Pessoa 2.5.1 :002?> $qtd = 0 2.5.1 :003?> 2.5.1 :004?> def initialize 2.5.1 :005?> $qtd += 1 2.5.1 :006?> end 2.5.1 :007?> 2.5.1 :008?> def qtd_pessoas 2.5.1 :009?> puts $qtd 2.5.1 :010?> end 2.5.1 :011?> end
2.5.1 :012 > 2.5.1 :013 > a = Pessoa.new 2.5.1 :014 > a.qtd_pessoas 1
2.5.1 :015 > 2.5.1 :016 > b = Pessoa.new 2.5.1 :017 > b.qtd_pessoas 2
2.5.1 :018 > 2.5.1 :019 > c = Pessoa.new
2.5.1 :020 > c.qtd_pessoas 3
In the example above, we instantiate the Pessoa class 3 times, and each time we do, the value of $qtd increases. For each new instance, we display the current value by calling “qtd_pessoas(),” so we can see $qtd increase with each new instance.
Instance variables @
Instance variables are available to each instance of a class and scoped to the individual object. Here’s an example:
2.5.1 :001 > class Pessoa 2.5.1 :002?> def initialize 2.5.1 :003?> @nome = “sem nome” 2.5.1 :004?> end 2.5.1 :005?> 2.5.1 :006?> def mostrar_nome 2.5.1 :007?> puts @nome 2.5.1 :008?> end 2.5.1 :009?> 2.5.1 :010?> def guardar_nome(nome) 2.5.1 :011?> @nome = nome 2.5.1 :012?> end 2.5.1 :013?> end
2.5.1 :014 > p1 = Pessoa.new 2.5.1 :015 > p1.mostrar_nome sem nome 2.5.1 :016 > p1.guardar_nome(“Jackson”) 2.5.1 :017 > p1.mostrar_nome Jackson
2.5.1 :018 > p2 = Pessoa.new 2.5.1 :019 > p2.mostrar_nome sem nome 2.5.1 :020 > p2.guardar_nome(“João”) 2.5.1 :021 > p2.mostrar_nome João
2.5.1 :022 > p1.mostrar_nome Jackson 2.5.1 :023 > p2.mostrar_nome João
Notice that we define a class called Pessoa that initializes a variable called @nome to “sem nome” each time we create an instance. In lines 14 through 17, we create an object called p1 and display the initial name stored in @nome, which in this case is “sem nome.” We then assign it the name “Jackson” and can see that the variable now has a new value.
In lines 18 through 21, we repeat the process. Since these are different instances, the new values are stored in the new object. Finally, lines 22 and 23 show that each variable keeps its own value. In other words, “they don’t get mixed up.”
Class variables @@
Of the types of variables we’re looking at today, I think this is the least well known and, as a result, the least used.
The name “class variable” might give you a certain impression, but watch out! Class variables are basically variables that “carry over” through inheritance between classes. Here’s an example:
2.5.1 :001 > class DBConfig
2.5.1 :002?> @@url_conn = 'http://meudbparao.com'
2.5.1 :003?>
2.5.1 :004?> def mostrar_url_conn
2.5.1 :005?> puts @@url_conn
2.5.1 :006?> end
2.5.1 :007?> end
2.5.1 :008 > db1 = DBConfig.new
2.5.1 :009 > db1.mostrar_url_conn
http://meudbparao.com
2.5.1 :010 > class PostgresConf < DBConfig
2.5.1 :011?> @@url_conn = 'http://postgresdb.com'
2.5.1 :012?>
2.5.1 :013?> def mostrar_url_conn
2.5.1 :014?> puts @@url_conn
2.5.1 :015?> end
2.5.1 :016?> end
2.5.1 :017 > db2 = PostgresConf.new
2.5.1 :018 > db2.mostrar_url_conn
http://postgresdb.com
2.5.1 :019 > db1.mostrar_url_conn
http://postgresdb.com
2.5.1 :020 > db2.mostrar_url_conn
http://postgresdb.com
Notice that in the example above, we create a class called DbConfig and a variable called @@url_conn, which we set to “http://meudbparao.com”. We instantiate the class and display the variable’s value. Nothing new so far.
Next, in lines 10 through 16, we define a class called PostgresConf that inherits from DBConfig. Its definition also uses @@url_conn, which, because it’s a class variable, lets us overwrite the value originally assigned in the DBConfig class.
Finally, when we display the connection URL for instances of both classes, we get the same value. The important thing to notice here is that the subclass changed the value of a variable in the parent class.
That wraps up this brief overview of Ruby’s variable types. If you found it interesting, check out another article on the topic, "ElasticSearch with Ruby on Rails", here on the Impulso blog, and learn how to code more efficiently.
See you next time! 😉


