static variable in ruby?

A

aidy

Is it possible to have a static variable in ruby, that will keep its
value after a method invocation?

I am finding it very hard to think in OO terms, when all I need to do
is read a file and depending on it's line contents write to another
one.

Aidy
 
R

Ross Bamford

Is it possible to have a static variable in ruby, that will keep its
value after a method invocation?

I'm not sure what you're after here, maybe a constant? or a global? It
depends on what you're trying to do really. If you're writing a class you
could just store the value in an instance variable (or a class variable).
Can you give more information?
I am finding it very hard to think in OO terms, when all I need to do
is read a file and depending on it's line contents write to another
one.

Then don't :) Why not just use something like:

File.open('output.txt','w') do |output|
File.open('input.txt','r') do |input|
input.each_line do |line|
output << line if line =~ /^\s*def/
end
end
end

or even just (warning - perlisms follow):

$ ruby -ne 'print if ~/^\s*def/' input.txt > output.txt
 
R

Robert Klemme

aidy said:
Is it possible to have a static variable in ruby, that will keep its
value after a method invocation?

I guess you are thinking of C's static like in

void all_max(int i) {
static int max = 0;

if ( i > max ) max = i;

return max;
}

This is not very OO and hardly maintainable. In an OO language you'd do

class AllMax
def initialize() @m = 0 end
def max(i)
@m = i if i > @m
@m
end
end

If you need this globally you could make it a singleton.
I am finding it very hard to think in OO terms, when all I need to do
is read a file and depending on it's line contents write to another
one.

If it's a quick shot you can just use a global variable. Otherwise just
create a class that will do the processing (see also Command Pattern).
Basically this means you create a class that is responsible for a
complex operation. An instance of this class can store all data it
needs to perform this operation. HTH

Kind regards

robert
 
M

Matt Sidesinger

I am new to ruby, so excuse me if this is incorrect, but can't you just
add a "class" variable. I thought that a "class" variable in ruby was
a variable that was shared accross instances? Class variables start
with @@ rather than @.

Someone please stop me if my thinking here is off base.

class Test
@@counter = 0
def initialize
@@counter = @@counter.succ
puts @@counter
end
def add
@@counter = @@counter.succ
end
def value
@@counter
end
end

counter1 = Test.new
counter2 = Test.new
puts counter1.object_id
puts counter2.object_id
puts counter1.value
puts counter1.add
puts counter2.value

1
2
22362936
22362816
2
3
3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top