Globals outside of OO scripting

When writing methods as functions (I know they’re methods pretending to
be functions) do you still need to use globals… like this:

$global_var
def some_other_function

$global_var
end

Or, is it OK to use them like this:

global_var
def some_other_function

global_var
end

rtilley wrote:

global_var
def some_other_function

global_var
end

No, you still need to use either global or instance variable syntax,
since
it is, as you correctly state, still a class.

def some_other_function
@n = 1
$m = 2
end
def some_other_function2
puts @n
puts $m
end

some_other_function
some_other_function2

On Mar 8, 2006, at 4:18 PM, rtilley wrote:

Or, is it OK to use them like this:

No

global_var
def some_other_function

global_var
end

global_var isn’t global here.

On Thu, 9 Mar 2006, rtilley wrote:

global_var
def some_other_function

global_var
end

but why? at the very least do

class Main
def initialize
@foo = 42
end
def run
p @foo
end
end

Main::new unless $0 == FILE

save the namespaces! :wink:

-a

Bernhard ‘elven’ Stoeckner wrote:

end
No, you still need to use either global or instance variable syntax, since
it is, as you correctly state, still a class.

OK, but the script works wheter I use $ for globals and @ for instance
variables or not. With small scripts, why does it matter?

On Mar 8, 2006, at 4:53 PM, rtilley wrote:

OK, but the script works wheter I use $ for globals and @ for
instance variables or not. With small scripts, why does it matter?

Whether you use globals or top-level instance variables for a small
script is pretty much
irrelevant but what happens when your small script gets bigger or you
want to reuse it in a larger
project? Or you want to merge two small scripts to solve a slightly
bigger problem?
You’ll have to wrap you little script in a class anyway and in that
context the globals/instance
variable solution isn’t going to work.

Much easier just to wrap it all in a trivial class to start with as
Ara suggested.
The very fact that you are using globals to provide a common state
across functions
or function calls cries out for a class/object solution to package
that state.

If it was drudgery to create a class then maybe it would be an issue
but it is soooo easy in Ruby, so why fight it?

Gary W.

On 3/8/06, Jacob F. [email protected] wrote:

$ irb

global_var = 2
=> 2

def test1
global_var
end
=> nil

test1
=> nil

D’oh, I should have actually tested that before posting. This
actually raises a NameError exception, since the global_var referred
to in test1 doesn’t even exist!

Jacob F.

On 3/8/06, rtilley [email protected] wrote:

No, you still need to use either global or instance variable syntax, since
it is, as you correctly state, still a class.

OK, but the script works wheter I use $ for globals and @ for instance
variables or not. With small scripts, why does it matter?

I’m curious what your script is then. Because the variable will not be
shared – the global_var inside some_other_function is different than
the global_var outside the function. Try this:

$ irb

global_var = 2
=> 2

def test1
global_var
end
=> nil

def test2
global_var = 5
end
=> nil

global_var
=> 2

test1
=> nil

test2
=> 5

global_var
=> 2

Jacob F.

On Mar 8, 2006, at 7:58 PM, rtilley wrote:

def method
@method_var = “a_string”

end

end

How can I get to these variables outside of the class.

You can’t, that’s kind of the point. You can write accessors (or
setters and getters, whatever terminology you prefer)

e.g.

class Thing
def method_var=(value)
@method_var = value
end
def method_var()
@method_var
end
end

To save typing you can use
attr_accessor

class Thing
attr_accessor :method_var
end

t = Thing.new

t.method_var = 3

t.method_var
#=> 3

[email protected] wrote:

Much easier just to wrap it all in a trivial class to start with as Ara
suggested.

OK, say I have this:

class Thing

@@class_var = “filename”

def method
@method_var = “a_string”

end

end

How can I get to these variables outside of the class.

On 3/8/06, rtilley [email protected] wrote:

   ...
 end

end

How can I get to these variables outside of the class.

class Thing
def Thing.get_cvar
return @@class_var
end
def get_mvar
return @method_var
end
end

Elsewhere in your code:

puts Thing.get_cvar
puts Thing.new.get_mvar


Note that in the case of method-variables, you can use
the even easier shorthand of:

attr_reader :method_var

and then you’d get the value via:

puts Thing.new.method_var

where ‘@method_var’ would be the name of the instance-
variable whose value will be returned by the method named
‘method_var’.

Also note that you may confuse yourself by thinking of
@-variables as “method variables”. They are instance
variables. The same value is seen by all methods for
any given instance of the class.

Thanks for the examples and info guys!

I’m still trying to get my head around the whole OO concept… as you
can probably tell :slight_smile: I don’t know if this is proper or not, but I ended
up making a constant in the class. Inside, the methods could access it
by name. And outside, I could access it like this Class_Name::Constant

Is that good or bad?

Logan C. wrote:

Depends. Why are you accessing it outside the class? If its to change
its value, than thats probably not a good idea and that functionality
should be put into a method. Of course if its really a constant, well
then its ok.

It’s a filepath (c:\name_of_file) I only wanted to define the path once
and be able to read it inside and outside the class. I am not changing
it.

On Mar 9, 2006, at 3:38 AM, rtilley wrote:

Hmm, this makes me wonder if maybe your class should look like this:

class NeedsAPath
def initialize(path, …)
@path = path

end

end

obj = NeedsAPath.new(‘c:\name_of_file’)

On Mar 8, 2006, at 8:48 PM, rtilley wrote:

Depends. Why are you accessing it outside the class? If its to change
its value, than thats probably not a good idea and that functionality
should be put into a method. Of course if its really a constant, well
then its ok.