T
tony summerfelt
is there a way to undefine a variable?
tony summerfelt said:is there a way to undefine a variable?
Claus Spitzer said:I concur.
It's probably easier if you think of a variable as just a pointer to
an object, not as the objects themselves.
Claus Spitzer said:well yes... It's just part of the procedural-->OO shift that everyone
needs to go through at some point.
In fact this has nothing to do with procedural vs. OO. The way how
variables (locations) and values are related is completely orthogonal to the
nature of the language (procedural or OO).
in my particular case i'm thinking black box approach. i don't want
the variable to 'exist' any more.
i don't know if that's how it's thought of in perl, but when i
undef($x), $x is no longer there for me to use unless i assign it
something, and i can test that it isn't with:
if ! defined($x) # do something.
as long as ruby 'acts' the same way, it's all i need.
Claus Spitzer said:Again, this is where it's useful to consider the OO approach and think
of variables as just the symbols you use to reference to objects. If
the symbol you're using is not assigned to any object (a.k.a. it
points to nil) then it it undefined.
Christoph said:tony summerfelt wrote:
...
I am aware of this kind of functionality for
instance and class variables - for local or
global variables such methods, afaik (I am not
really sure!!!) don't exist.
I see no problems with a removal method
for global variables but we are probably better
off without removal methods for local variables.
tony said:in my particular case i'm thinking black box approach. i don't want
the variable to 'exist' any more.
i don't know if that's how it's thought of in perl, but when i
undef($x), $x is no longer there for me to use unless i assign it
something, and i can test that it isn't with:
if ! defined($x) # do something.
as long as ruby 'acts' the same way, it's all i need.
I think the best way to get the behaviour you want is to use a hash.
Instead of assigning the value to a variable, you just assign the value
to a hash key.
$h = Hash.new
def test_key(sym)
if $h.has_key?(sym)
...
else
...
end
end
Again, this is where it's useful to consider the OO approach and think
of variables as just the symbols you use to reference to objects. If
the symbol you're using is not assigned to any object (a.k.a. it
points to nil) then it it undefined.
I think the best way to get the behaviour you want is to use a hash.
me or others isn't a concern.
Why is this?
i refer to my earlier message about a blackbox approach...
What do you gain by undefining instance variables? If you access it via
@foo it'll be nil anyway.
What about the logic of your code or design depends on the 'existence'
of a variable? What problem is it you're trying to solve with this
approach?
What about the logic of your code or design depends on the 'existence'
of a variable? What problem is it you're trying to solve with this
approach?
just makes the code shorter. here's an excerpt of a log trimming
program in perl (for the format that binkd generates) ('@' precedes
arrays in perl):
while(<binkd>)
{
# date parsing code was here
@diff=Delta_DHMS(@binkdate,@today) if /(\[\d+\])/;
print $trimmed $_ if $diff[0] < $ARGV[1] && defined(@diff);
next if defined(@diff);
print $trimmed $_ if ! /(\[\d+\])/;
undef(@diff);
}
this goes through each line in the log file.
if the log line contains a date stamp, it's processed and written to
an archive file if it meets the criteria, and the next line in the
file is read.
tony summerfelt said:{
# date parsing code was here
@diff=Delta_DHMS(@binkdate,@today) if /(\[\d+\])/;
print $trimmed $_ if $diff[0] < $ARGV[1] && defined(@diff);
next if defined(@diff);
print $trimmed $_ if ! /(\[\d+\])/;
undef(@diff);
}
while(<binkd>)
{
# date parsing code was here
@diff=Delta_DHMS(@binkdate,@today) if /(\[\d+\])/;
print $trimmed $_ if $diff[0] < $ARGV[1] && defined(@diff);
next if defined(@diff);
print $trimmed $_ if ! /(\[\d+\])/;
undef(@diff);
}
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.