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

Asked

Viewed 357 times

0

What does arroba (@) in front of variables in Ruby?

For example

@s = gets.to_i
  • 4

    I believe you have the potential to write a title much better than that. Anything, you can see some discussions on [meta] on how make a good title

  • 1

    Dear Misaee, I edited your question, note the difference from the initial title to the current title, this helps people understand your question, it is totally redundant to write "help", everyone has been seeking help here on the site, put focus on describing the problem, thus arouses interest in people in helping you and already makes the problem clearer before anyone even enters your question.

1 answer

3


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

That code of yours is just a line, it’s hard to explain with just that line. 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 above example the person class can access within it the variables @nome and @sobrenome. They are even used in in the method nome_compleo.

But the variable contador_de_letras_do_nome cannot be accessed within the method nome_compleo if I ever needed to. Because this various one doesn’t have the @, that is, its scope is method.

You can see more in this post. ruby various scope

Browser other questions tagged

You are not signed in. Login or sign up in order to post.