What does arroba (@) mean in variables in Ruby?

0

What does at (@) mean in front of variables in Ruby?

For example

@s = gets.to_i
    
asked by anonymous 13.08.2018 / 16:50

1 answer

2

It means that the scope of this variable, ie the limit that it can be accessed, is from within a class. It is an instance variable.

Your code is just one line, so it's hard to explain with this line alone. But follow the example below

class Pessoa
   def initialize(nome, sobrenome)
     contador_de_letras_do_nome = nome.size
     @nome = nome
     @sobrenome = sobrenome
   end

   def nome_completo
     @nome + @sobrenome
   end
end

In the example above the class person can access the various variables @nome and @sobrenome . Eleas are even used inside the nome_compleo method.

But the variable contador_de_letras_do_nome can not be accessed within the nome_compleo method if I happen to need it. Because this variant does not have @ , that is, its scope is method.

You can see more in this post. ruby variant scope

    
13.08.2018 / 17:03