Skip to Content

The Difference Between $, @@, And @ In Ruby (Explained)

The Difference Between $, @@, And @ In Ruby (Explained)

These are typically prefixes for variable names that describe the variable’s range. Global variables are those whose names begin with $ and can be accessed in the same way from anywhere in the program.

Class variables are variables whose names begin with @@. Each object that accesses it receives the same value. 

As soon as the class is needed in the application, they’re typically provided memory space and can only have one instance. These are known as static variables in Java if you are familiar with that language.

Finally, variables that begin with @ are instance variables, meaning they are unique to the object they are now holding. For various instances of the class, their values vary.

Each newly generated item is given its memory area.

In this article, we’ll be discussing the differences between the $, @@, and @ in the context of Ruby. You’ll get to know the details with a bunch of knowledge regarding them.

Overall, it’d be an informative blog.

Let’s get started.

What Is $, @@ And @ In Ruby?

A global variable is $. While, @-> is an instance variable, similar to init in Python. @@ is a class variable. The range of the variable you’re setting is what makes a difference.

A global variable called $ is available across the entire application. All instances of a class have access to the class variable @@.

@ is a class instance variable that is only usable by that particular class instance.

In general, you should rethink your approach to the topic at hand if you are utilizing $global or $, and @class variables because you’re probably doing something wrong.

In Ruby On Rails, What Does “&” Do?

Several options exist for the binary form, depending on the type of input: Bitwise AND, similar to C, for integers like 3 and 1.

Array intersection for arrays like [1, 2, 3] and [3, 4, 5]. (ignoring duplicate values).

The unary form is as follows: &: foo: Transforming the provided item into a proc (essentially an executable block of code).

On the other hand, A = [] denotes that “a” is an object of the “Array” class. This is the syntax for a 2D array that is empty, but we need more details to understand it.

What Makes “Ruby” And “M Ruby” Different From One Another?

The same author created each of them. M Ruby followed Ruby. Many of its linguistic traits, syntaxes, and standard libraries are inherited by it.

However, M Ruby is made for embedded systems, which have more severe resource limitations. Even the bootstrapping of the Ruby source compiler uses Mruby.

They’re binary incompatible and speak separate languages. To summarize, we can say that they are very much alike.

$, @@ and @- What Sets Them Apart?

The scope of the variable you’re setting is the difference. $ is a global variable that can be used throughout the application. It’s a global class variable that affects all instances of the class.

@ is a class instance variable that is only available to that class’s instance.

In general, if you’re utilizing global or class variables to solve a problem, you’re generally doing something incorrectly and should reconsider your approach.

Coming towards $, this is a global variable.

Unless you’re building a very small standalone script, don’t use global variables. Use a constant if you want something global that doesn’t change.

What Are The Exceptions between $ and the Other Variables?

$1, $2 as well as $&, $’, $’, $+, and $, aren’t global variables. They’re regular expression-related threads and method local variables. You can’t even change their values manually.

A variable that is only used once. It’s similar to Java’s non-static field.

The difference between instance variables and Java fields is that you must create an instance variable in an instance method (including the function Object() { [native code] }).

This is the case if you want it to belong to a normal object; otherwise, the instance variable belongs to the class but is not a class variable.

Consider the following scenario:

Foo class @foo = 1 is the class that this instance variable belongs to. This instance variable belongs to a normal object @foo = 2 end def initialize def @bar = value end is the class of this instance variable.

An image showing false and function written
False, Function, True, and Line are some of the pseudo-variables.

What Does Ruby’s @@ Stand For?

@@ denotes a class variable, which means it can be inherited. This means that if you make a subclass of that class, the variable will be passed down to it.

If you have a class vehicle with the class variable @@number of wheels, you can construct a class car vehicle with the same class variable.

Local, global, instance, and class variables are the four types of variables in Ruby. When you use @instance var, all instance methods have access to instance var.

In class methods, we use @@ before a variable to inform Ruby to access that class’s attribute (class variable). When you use @@class var, class var becomes available to the entire class.

A class variable is created in Ruby by using the double at-sign (@@) before a variable name (e.g., @@variable name).

These are the variables:

  • Static—that is, regardless of how many instances of the class you create, just one copy of the variable exists;
  • Within the context of the inheritance hierarchy, they are globally scoped—that is, they are shared by a class and all of its subclasses.

We’re almost halfway through the article, I hope you’re quite familiar with these variables now. Aren’t you?

An image showing words and other data of a computer language.
Functions and prototypes include parenthesis as an input.

In Ruby, What Is The Difference Between & & And and ‘||’ And ‘or’ In Ruby?

The order of precedence is the distinction between & & and and and or in Ruby. The operator has a higher priority than the operator.

However, until combined with operators that are in between these two, such as the ternary and assignment operators, this is usually not an issue. 

This may come as a pleasant surprise!

Let me use an example to demonstrate.

 s stands for “true” and “false.”

true => false

Let’s look at the value of s, which was returned by the statement.

This may come as a pleasant surprise! What happened was that the assignment was completed first, followed by the “and” condition.

In Ruby, How Do You Tell The Difference Between @variable And @@variable?

The at-sign (@) is used before a variable name in Ruby to construct a class instance variable (e.g., @variable name). These are the variables:

This is the only variable that is available within the class methods; directly. unique to each instantiated object of the class in which they’re defined (i.e., each class object instance has its copy of these variables).

Variables with a single at-sign (i.e. @variable) are class instance variables, but variables with two at-signs (i.e. @@variable) are class variables in Ruby. That is to say:

@variable is only directly accessible within the class methods. it’s defined in, whereas @@variable is shared between a class and all its subclasses and ancestors.

As in;

@@total = 0 type Person

@name = name @@total += 1 end def initialize(name)

end def @@total

name:attr accessor

An image showing a computer setup.
A computer comprises a Java script and other binary languages and variables.

What Are Pseudo- Variables In Ruby?

They are unique variables that seem like local variables but behave like constants. You can’t give these variables any value.

The following tables describe the pseudo-variables in Ruby.

SELFThe current method’s receiver object is self.
FALSEFalse is a value that represents the word “false.”
_LINE_The source file’s current line number.
TRUE A value that represents the fact that something is true.
_FILE_The current source file’s name.
The Description Of Pseudo-Variables

What Are Global Variables?

A “$” symbol precedes the name of a global variable. It has a global scope, which means it can be accessed at any point in a program.

A nil value will be assigned to an uninitialized global variable. They’re not recommended for use since they make applications obscure and difficult.

There are numerous global variables included in Ruby.

Let’s have a look at an example:


class var $global = “GLOBAL”

“Global variable in One is #$global var,” says one of the class’s def displays.

The two-def display concludes, “Global variable in Two is # $global var.” oneobj.display = oneobj

In the preceding example, twoobj.display = twoobj@states name is the instance variable.

I think now it’s quite clear how we categorize various variables in Ruby.

Check out this video to learn more about the class variables in Ruby.


To conclude:

  • $, @@, and @ denote distinct variable types in Ruby. These are global, class, and instance variables.
  • Global variables ($ prefix) have a global scope accessible throughout the program.
  • Class variables (@@ prefix) are shared among instances of a class. In contrast, instance variables (@ prefix) are unique to each object.
  • Using global or class variables may suggest a flawed approach. Reconsider their use for better code design.
  • Operators like && and || differ in precedence in Ruby. These influence their execution order.
  • @ denotes an instance variable accessible within class methods and unique to each instance.
  • Class variables with @@ are shared among a class and its subclasses. It maintains a single copy.
  • Pseudo-variables in Ruby, like SELF or FILE, behave like constants but can’t be assigned new values.

I’ve explained the variations with the help of some examples so that you can have a deep understanding of the distinctions among them.

Want to find out the difference between SSD and NVME? Take a look at this article: The Difference Between SSD & NVMe (Answered)

Other Articles

Is 1ml of 200mg Testosterone Cypionate too Little to Make a Difference in Low Testosterone? (Facts)

Skateboard vs. Bike Helmet (Difference Explained)

Chopper Vs. Helicopter- A Detailed Comparison

Skip to content