Global Variable in Ruby Last Updated : 25 Sep, 2019 Comments Improve Suggest changes 5 Likes Like Report Global Variable has global scope and accessible from anywhere in the program. Assigning to global variables from any point in the program has global implications. Global variable are always prefixed with a dollar sign ($). If we want to have a single variable, which is available across classes, we need to define a global variable. By default, an uninitialized global variable has a nil value and its use can cause the programs to be cryptic and complex. Global variable can be change anywhere in program. Syntax : $global_variable = 5 Example : Ruby # Ruby Program to understand Global Variable # global variable $global_variable = 10 # Defining class class Class1 def print_global puts "Global variable in Class1 is #$global_variable" end end # Defining Another Class class Class2 def print_global puts "Global variable in Class2 is #$global_variable" end end # Creating object class1obj = Class1.new class1obj.print_global # Creating another object class2obj = Class2.new class2obj.print_global Output : Global variable in Class1 is 10 Global variable in Class2 is 10 In above example, a global variable define whose value is 10. This global variable can be access anywhere in the program. Example : Ruby # Ruby Program to understand global variable $global_variable1 = "GFG" # Defining Class class Author def instance_method puts "global vars can be used everywhere. See? #{$global_variable1}, #{$another_global_var}" end def self.class_method $another_global_var = "Welcome to GeeksForGeeks" puts "global vars can be used everywhere. See? #{$global_variable1}" end end Author.class_method # "global vars can be used everywhere. See? GFG" # => "global vars can be used everywhere. See? GFG" Author = Author.new Author.instance_method # "global vars can be used everywhere. See? # GFG, Welcome to GeeksForGeeks" # => "global vars can be used everywhere. See? # GFG, Welcome to GeeksForGeeks" Output : global vars can be used everywhere. See? GFG global vars can be used everywhere. See? GFG, Welcome to GeeksForGeeks In above example, We define two global variable in a class. we create a object of class Author than call method. Create Quiz Comment D DivyaPareek Follow 5 Improve D DivyaPareek Follow 5 Improve Article Tags : Ruby Ruby-Basics Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like