What is the use of $ in Ruby?

What is the use of $ in Ruby?

-$ in Ruby is treated as a global variable and it is used in a class variable that starts with an @sign and followed by upper or lower case letters.

-Class variable can be shared between many other objects of the class and each of the class variables exists for each given class.

-The $ (global variable) is followed by the name character and it defines the scope of the variable that is to be used.

-The global variable can use other punctuation characters like $_ and $-a. The example is given as:

class Test
def h
 $a = 5
 @b = 4
while $a > 0
puts $a
$a= $a - 1
test = Test.new
puts $a                    # 10
puts @b                   #nil
Post your comment