Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference global variables and class variables?

Tags:

ruby

I'm new to programming. Right now I'm studying Ruby. To my understanding, global variables are defined in the global namespace (so outside of any classes or functions). I'm reading something and it says global variables have a $ sign before them. What does that mean? Does it mean when I define a function or class and want to reference my global variable (let's say it is edmund = 123) I would have to reference it like this: $edmund?

so:

edmund = 123 def my_function()   456 + $edmund end 

Also are class variables (the ones that begin with @@) like instance variables (@) where you can access them by calling them through Class.classvariable? What is their purpose?

like image 421
bigpotato Avatar asked Aug 24 '12 15:08

bigpotato


People also ask

How do you refer to global variables?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

Can I use global variable in class?

The set of all global variables is known as the global environment or global scope of the program. We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.

How do you reference a global variable in Java?

To define a Global variable in java, the keyword static is used. Java actually doesn't have the concept of Global variable, it is known as class variable ( static field ). These are the variables that can be used by the entire class. // constructer used to initialise a Student object.


1 Answers

Global scope is scope that covers the entire program. Global scope is enjoyed by global variables, which are recognizable by their initial dollar-sign ($) character. They’re available everywhere and creating your own global variables can be tempting, especially for beginning programmers. But they’re not always a good idea.

$gvar = "I'm a global!" class C     def examine_global         puts $gvar     end end  c = C.new c.examine_global # I'm a global! 

Class variables begin with two at signs: @@var, for example. Despite their name, class variables aren’t class scoped. Rather, they’re class-hierarchy scoped. At its simplest, the idea behind a class variable is that it provides a storage mechanism that’s shared between a class and instances of that class, and that’s not visible to any other objects.

class Parent     @@value = 100 end  class Child < Parent     @@value = 200 end  class Parent     puts @@value end 

What gets printed is 200. The Child class is a subclass of Parent, and that means Parent and Child share the same class variables—not different class variables with the same names, but the same actual variables. When you assign to @@value in Child, you’re setting the one and only @@value variable that’s shared throughout the hierarchy— that is, by Parent and Child and any other descendant classes of either of them.


And to give credit where its due - This explanation comes from "The Well Grounded Rubyist" by David A Black, one of the best resources to learn about Ruby.

like image 174
saihgala Avatar answered Sep 30 '22 14:09

saihgala