Local and Global Variable

Hello,

I still don’t fully understand what is local and what is global
variable. Can any of you please explain that to me?

Peace be upon you –

Junayeed Ahnaf N.

Twitter - @Nirjhor http://twitter.com/nirjhor

Content preview: On 18.10.2011 14:43, Junayeed Ahnaf N. wrote: > I
still
don’t fully understand what is local and what is global variable.
Can any
of you please explain that to me? Generally, they’re well explained
at http://en.wikipedia.org/wiki/Local_variable
and http://en.wikipedia.org/wiki/Global_variable . […]

Content analysis details: (-2.9 points, 5.0 required)

pts rule name description



-1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP
-1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1%
[score: 0.0000]
X-Cloudmark-Analysis: v=1.1
cv=7AjrSHUygkxmgKj9+ZdWPzZoKYzIcpgZMIt1Yxqn8hE=
c=1 sm=0 a=yAP4_T4JbDAA:10 a=IkcTkHD0fZMA:10 a=8pif782wAAAA:8
a=JAvMkbpGytu0pAvRg08A:9 a=QEXdDO2ut3YA:10
a=HpAAvcLHHh0Zw7uRqdWCyQ==:117
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Precedence: bulk
Lines: 11
List-Id: ruby-talk.ruby-lang.org
List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
List-Post: mailto:[email protected]
List-Owner: mailto:[email protected]
List-Help: mailto:[email protected]?body=help
List-Unsubscribe: mailto:[email protected]?body=unsubscribe
Received-SPF: none (Address does not pass the Sender Policy Framework)
SPF=FROM; [email protected];
remoteip=::ffff:221.186.184.68; remotehost=carbon.ruby-lang.org;
helo=carbon.ruby-lang.org; receiver=eq4.andreas-s.net;

On 18.10.2011 14:43, Junayeed Ahnaf N. wrote:

I still don’t fully understand what is local and what is global variable. Can
any of you please explain that to me?

Generally, they’re well explained at
http://en.wikipedia.org/wiki/Local_variable and
http://en.wikipedia.org/wiki/Global_variable .

Or do yo have any specific question local vs. global regarding Ruby?

HTH,

  • Markus

On 10/18/2011 08:43 AM, Junayeed Ahnaf N. wrote:

I still don’t fully understand what is local and what is global variable. Can
any of you please explain that to me?

It’s about the scope of the variable; i.e., what parts of your code can
see those variables.

Global variables are visible from any piece of code because their scope
is global. They exist outside of any method, class or module. Globals
start with $; i.e., $ARGV, $ANYBODY_CAN_SEE_ME and the like. And, the
big one, global variables exist from the moment they are created until
the application exits.

Local variables only exist within a method or code block of some sort.
So in the following code snippet:

def farkle
updated_count = $FARKLE_COUNT
updated_count += 1
$FARKLE_COUNT = updated_count
end

$FARKLE_COUNT is a global variable, while updated_count is a local
variable; i.e., when the method farkle() exits then updated_count will
no longer exist.

HTH.

hi Junayeed,

check out this link for more info on ruby variables and constants…
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants

in general, global variables are a bad idea - and it’s best to try and
stay away from them if you can. you’ll soon discover why yourself, as
you continue to use ruby more. class, instance, and local variables
will usually cover all your needs.

class variables exist and are the same across all instances and
methods of a given class. instance variables exist and can be accessed
from all methods of an instance of a class, but are not shared by two
instances of the same class (changing one will not change the other.)
local variables exist only within the method or block in which they were
created.

here’s a quick comparison of instance variables and local variables:

#######
class VariableTester

def initialize
@instance = “I can travel between methods of this class”
local = “I only exist in the initialize method”

puts @instance
puts local

print_variables

end

def print_variables
local = “I’m also called local, and I only exist in the
print_variables method”

puts
puts @instance
puts local

@instance = "I've been changed!"

print_again

end

def print_again
local = “I’m yet another local, and I can’t leave the print_again
method.”

puts
puts @instance
puts local

end

end

vt = VariableTester.new
#######

hth,

  • j

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs