Undefine

is there a way to undefine a variable?

"tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
news:p3hmc0p6h00cq59vcl6jk0f61r9ir3gl4e@4ax.com...

is there a way to undefine a variable?

like

x = nil

Note: defined? does not have the same importance as in Perl.

    robert

I concur.
It's probably easier if you think of a variable as just a pointer to
an object, not as the objects themselves.

···

On Sun, 13 Jun 2004 04:48:36 +0900, Robert Klemme <bob.news@gmx.net> wrote:

"tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
news:p3hmc0p6h00cq59vcl6jk0f61r9ir3gl4e@4ax.com...

> is there a way to undefine a variable?

like

x = nil

Note: defined? does not have the same importance as in Perl.

    robert

"Claus Spitzer" <DocBoobenstein@gmail.com> schrieb im Newsbeitrag
news:a7c2a81904061213093afcb11b@mail.gmail.com...

I concur.
It's probably easier if you think of a variable as just a pointer to
an object, not as the objects themselves.

Maybe it's easier because it's more accurate? :wink:

    robert

···

On Sun, 13 Jun 2004 04:48:36 +0900, Robert Klemme <bob.news@gmx.net> wrote:
>
>
> "tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
> news:p3hmc0p6h00cq59vcl6jk0f61r9ir3gl4e@4ax.com...
>
>
> > is there a way to undefine a variable?
>
> like
>
> x = nil
>
> Note: defined? does not have the same importance as in Perl.
>
> robert
>
>

well yes... It's just part of the procedural-->OO shift that everyone
needs to go through at some point.

···

On Sun, 13 Jun 2004 06:48:37 +0900, Robert Klemme <bob.news@gmx.net> wrote:

"Claus Spitzer" <DocBoobenstein@gmail.com> schrieb im Newsbeitrag
news:a7c2a81904061213093afcb11b@mail.gmail.com...
> I concur.
> It's probably easier if you think of a variable as just a pointer to
> an object, not as the objects themselves.

Maybe it's easier because it's more accurate? :wink:

    robert

> On Sun, 13 Jun 2004 04:48:36 +0900, Robert Klemme <bob.news@gmx.net> > wrote:
> >
> >
> > "tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
> > news:p3hmc0p6h00cq59vcl6jk0f61r9ir3gl4e@4ax.com...
> >
> >
> > > is there a way to undefine a variable?
> >
> > like
> >
> > x = nil
> >
> > Note: defined? does not have the same importance as in Perl.
> >
> > robert
> >
> >
>
>

"Claus Spitzer" <DocBoobenstein@gmail.com> schrieb im Newsbeitrag
news:a7c2a81904061215063143a796@mail.gmail.com...

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).

Regards

    robert

···

On Sun, 13 Jun 2004 06:48:37 +0900, Robert Klemme <bob.news@gmx.net> wrote:
>
>
> "Claus Spitzer" <DocBoobenstein@gmail.com> schrieb im Newsbeitrag
> news:a7c2a81904061213093afcb11b@mail.gmail.com...
> > I concur.
> > It's probably easier if you think of a variable as just a pointer to
> > an object, not as the objects themselves.
>
> Maybe it's easier because it's more accurate? :wink:
>
> robert
>
>
>
>
> > On Sun, 13 Jun 2004 04:48:36 +0900, Robert Klemme <bob.news@gmx.net> > > wrote:
> > >
> > >
> > > "tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
> > > news:p3hmc0p6h00cq59vcl6jk0f61r9ir3gl4e@4ax.com...
> > >
> > >
> > > > is there a way to undefine a variable?
> > >
> > > like
> > >
> > > x = nil
> > >
> > > Note: defined? does not have the same importance as in Perl.
> > >
> > > robert
> > >
> > >
> >
> >
>
>

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.

···

On Sun, 13 Jun 2004 20:08:48 +0900, you wrote:

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).

tony summerfelt wrote:
....

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

···

---
class A
@x = 1
@@x = 1
$x = 1
x = 1
remove_instance_variable :@x
remove_class_variable :@@x
begin
remove_gobal_variable :$x
rescue => mes
  puts mes
end

begin
  remove_local_variable :x
rescue => mes
  puts mes
end
---
undefined method `remove_gobal_variable' for A:Class
undefined method `remove_local_variable' for A:Class
---

/Christoph

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.

···

On Mon, 14 Jun 2004 05:50:55 +0900, tony summerfelt <snowzone5@hotmail.com> wrote:

On Sun, 13 Jun 2004 20:08:48 +0900, you wrote:

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.

tony summerfelt wrote:

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

test_key(:foo)

$h[:foo] = "Bar"

test_key(:foo)

$h.delete(:foo)

test_key(:foo)

Now if you don't use .has_key? and you just try to access the hash entry, you'll just get 'nil' back (by default at least). If you want to have an exception raised instead, that's doable too.

Ben

"Claus Spitzer" <DocBoobenstein@gmail.com> schrieb im Newsbeitrag
news:a7c2a81904061400186bf233eb@mail.gmail.com...

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 know I'm picky, but

1) You don't "assign a symbol to an object". If at all it's the other way
round: you assign an object to a symbol. But I'd rather say "you make the
variable point to an instance". The symbol is just the representation of
the memory location that holds a reference to some instance.

2) 'nil' is in fact a valid instance. It has methods that can be invoked
safely:

nil.to_s

=> ""

nil.to_i

=> 0

nil.id

=> 4

This is different from Java where you get a NullPointerException if you
try to invoke any methods on 'null'.

I think you wanted to say the right thing but it was a bit ill formulated
IMHO.

Kind regards

    robert

···

On Mon, 14 Jun 2004 05:50:55 +0900, tony summerfelt > <snowzone5@hotmail.com> wrote:
>
> On Sun, 13 Jun 2004 20:08:48 +0900, you wrote:
>
> 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.
>
>

"Christoph" <chr_mail@gmx.net> schrieb im Newsbeitrag
news:2j4ab0FskcpcU1@uni-berlin.de...

tony summerfelt wrote:
...
> 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 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.

What do you gain by undefining instance variables? If you access it via
@foo it'll be nil anyway.

Regards

    robert

···

---
class A
@x = 1
@@x = 1
$x = 1
x = 1
remove_instance_variable :@x
remove_class_variable :@@x
begin
remove_gobal_variable :$x
rescue => mes
  puts mes
end

begin
  remove_local_variable :x
rescue => mes
  puts mes
end
end
---
undefined method `remove_gobal_variable' for A:Class
undefined method `remove_local_variable' for A:Class
---

/Christoph

hey, accesing globals from a method is ugly, just use a singleton :slight_smile:

H=Hash.new

def H.test_key sym
if has_key? sym
  ...
else
  ...
end
end

···

il Tue, 15 Jun 2004 00:17:55 +0900, Ben Giddings <bg-rubytalk@infofiend.com> ha scritto::

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

i just need the variable to 'not exist' however it's thought about (by
me or others :slight_smile: isn't a concern.

i'm willing to bet that perl coders are having a harder time with ruby
than coming from another language :slight_smile: i'm so used to 'thinking in perl'
that unlearning some of the perl magic is taking some effort...

···

On Mon, 14 Jun 2004 16:18:32 +0900, you wrote:

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.

it seems to be more work (and lines of code) than it needs to be for
just one variable.

i was surprised that ruby didn't have a simple undefine function. and
for the oo'ness of it, it could look like x.undefine. no more x :slight_smile:

···

On Tue, 15 Jun 2004 00:17:55 +0900, you wrote:

I think the best way to get the behaviour you want is to use a hash.

i just need the variable to 'not exist' however it's thought about (by
me or others :slight_smile: isn't a concern.

Why is this?

Robert Klemme wrote:
....

What do you gain by undefining instance variables? If you access it via
@foo it'll be nil anyway.

I did not advocate removing, a.k.a. undefining,
instance variable, I was just pointing out that
these removal methods exists - but since you asked:

Removing an instance or class variable after
its usefulness expired (a typical example are
auxiliary instance variable used in nontrivial
initializers distributed over several auxiliary
methods) is IMO a good practice mainly for
esthetical reasons (akin to the well established
practice of always initialing instance variable
before using them) - on occasion you might even
catch an error this way. Another reason is that
unused instance variable take up unnecessary
memory space.

It seems harder to me to make a similar compelling
stylistic (and memory) case for the existence of
removal methods for class variables and especially
for global variables, and removal method for local
variables make no sense at all.

/Christoph

"tony summerfelt" <snowzone5@hotmail.com> schrieb im Newsbeitrag
news:j3rrc09l54tp03e4smbqvvnsm43flh8j68@4ax.com...

···

On Tue, 15 Jun 2004 00:17:55 +0900, you wrote:

>I think the best way to get the behaviour you want is to use a hash.

it seems to be more work (and lines of code) than it needs to be for
just one variable.

i was surprised that ruby didn't have a simple undefine function. and
for the oo'ness of it, it could look like x.undefine. no more x :slight_smile:

It can't, because "x" and the object pointed at by it (and which thus
receives method invocations) are two different things. The instance
referenced by "x" does not know anything about the context's local binding
of "x"; especially it does not know that the method was invoked through a
reference named "x".

    robert

i refer to my earlier message about a blackbox approach...

for my own purposes, and those of anybody looking at the code, if they
can count on the variable not existing (after being 'undefined')
that's all that matters.

when i'm past the relative newbie stage and start digging into the
internals of ruby, i will want to know the 'why'.

···

On Tue, 15 Jun 2004 03:37:45 +0900, you wrote:

i just need the variable to 'not exist' however it's thought about (by
me or others :slight_smile: isn't a concern.

Why is this?

OTOH things should be undoable. If you believe so
then undefining a local variable (or whatever can
be defined) makes *some* sense. I must confess that
how to use such an undefine action in a clean way
is something I have yet to figure out :wink:

BTW: defined?() tells you about whether a variable
is defined, including local ones.

Yours,

JeanHuguesRobert

···

At 06:03 15/06/2004 +0900, you wrote:

Robert Klemme wrote:
.....

What do you gain by undefining instance variables? If you access it via
@foo it'll be nil anyway.

I did not advocate removing, a.k.a. undefining,
instance variable, I was just pointing out that
these removal methods exists - but since you asked:

Removing an instance or class variable after
its usefulness expired (a typical example are
auxiliary instance variable used in nontrivial
initializers distributed over several auxiliary
methods) is IMO a good practice mainly for
esthetical reasons (akin to the well established
practice of always initialing instance variable
before using them) - on occasion you might even
catch an error this way. Another reason is that
unused instance variable take up unnecessary
memory space.

It seems harder to me to make a similar compelling
stylistic (and memory) case for the existence of
removal methods for class variables and especially
for global variables, and removal method for local
variables make no sense at all.

/Christoph

-------------------------------------------------------------------------
Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17