Forum: Ruby globals outside of OO scripting

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-08 22:21
(Received via mailing list)
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
F1653a5cad4e1859403491de917f8f49?d=identicon&s=25 Bernhard 'elven' Stoeckner (Guest)
on 2006-03-08 22:46
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-08 22:46
(Received via mailing list)
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.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-08 22:49
(Received via mailing list)
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!  ;-)

-a
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-08 22:55
(Received via mailing list)
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?
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-03-08 23:38
(Received via mailing list)
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
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-03-09 00:30
(Received via mailing list)
On 3/8/06, rtilley <rtilley@vt.edu> 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 Fugal
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-03-09 00:39
(Received via mailing list)
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
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-09 02:01
(Received via mailing list)
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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-09 02:25
(Received via mailing list)
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
C5be24289f1471f3da84864a6677af12?d=identicon&s=25 Garance A Drosehn (Guest)
on 2006-03-09 02:25
(Received via mailing list)
On 3/8/06, rtilley <rtilley@vt.edu> 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.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-09 02:49
(Received via mailing list)
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 :) 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?
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-09 02:58
(Received via mailing list)
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.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-09 09:40
(Received via mailing list)
Logan Capaldo 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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-10 00:15
(Received via mailing list)
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')
This topic is locked and can not be replied to.