class Main
def initialize @foo = 42
end
def run
p @foo
end
end
Main::new unless $0 == __FILE__
save the namespaces!
-a
···
On Thu, 9 Mar 2006, 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
--
knowledge is important, but the much more important is the use toward which it
is put. this depends on the heart and mine the one who uses it.
- h.h. the 14th dali lama
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
···
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?
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:
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!
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.
···
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.
--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer or gad@FreeBSD.org
Rensselaer Polytechnic Institute; Troy, NY; USA
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
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.
···
On Mar 8, 2006, at 8:48 PM, rtilley wrote:
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
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.
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 9, 2006, at 3:38 AM, rtilley wrote:
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.