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 Nirjhor

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

Generally, they're well explained at Local variable - Wikipedia and Global variable - Wikipedia .

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

HTH,
- Markus

···

On 18.10.2011 14:43, Junayeed Ahnaf Nirjhor 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.

···

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

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

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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

--
Posted via http://www.ruby-forum.com/.