[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

globals outside of OO scripting

Brad Tilley

3/8/2006 9:15:00 PM

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
14 Answers

Bernhard 'elven' Stoeckner

3/8/2006 9:43:00 PM

0

rtilley wrote:

> 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

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

Logan Capaldo

3/8/2006 9:46:00 PM

0


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

> 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:
>

No

> global_var
> def some_other_function
> ...
> global_var
> end
>

global_var isn't global here.



Ara.T.Howard

3/8/2006 9:47:00 PM

0

Brad Tilley

3/8/2006 9:51:00 PM

0

Bernhard 'elven' Stoeckner wrote:
> rtilley wrote:
>
>
>>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
>
>
> 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?

Gary Wright

3/8/2006 10:36:00 PM

0


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 Wright





Jacob Fugal

3/8/2006 11:28:00 PM

0

On 3/8/06, rtilley <rtilley@vt.edu> wrote:
> Bernhard 'elven' Stoeckner wrote:
> > rtilley wrote:
> >>Or, is it OK to use them like this:
> >>
> >>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.
>
> 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 Fugal


Jacob Fugal

3/8/2006 11:39:00 PM

0

On 3/8/06, Jacob Fugal <lukfugl@gmail.com> wrote:
> $ irb
>
> >> global_var = 2
> => 2
>
> >> def test1
> >> global_var
> >> end
> => nil

<snip>

> >> 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 Fugal


Brad Tilley

3/9/2006 1:00:00 AM

0

gwtmp01@mac.com 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.

Logan Capaldo

3/9/2006 1:24:00 AM

0


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

> gwtmp01@mac.com 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.
>

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



Garance A Drosehn

3/9/2006 1:25:00 AM

0

On 3/8/06, rtilley <rtilley@vt.edu> wrote:
>
>
> 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.


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.

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer or gad@FreeBSD.org
Rensselaer Polytechnic Institute; Troy, NY; USA