How to test for existence of instance variable? 
Author Message
 How to test for existence of instance variable?

I have an existing class Foo, and existing objects of that class.

I now extend the class by adding a method, which makes use of an instance
variable which previously did not exist.

How can I make it test that the instance variable is uninitialised, so I can
prevent a warning? This is what I am trying to do:

  class Foo
    attr_reader :things
    def add_thing(n)


      end

    end
  end



if ruby is running with -w. (This is 1.6.8)

I am hoping that I don't have to keep track of all existing Foo objects,
just so that I can send them a message 'reset_things'

Thanks...

Brian.



Sat, 06 Aug 2005 02:33:53 GMT  
 How to test for existence of instance variable?

Quote:

> How can I make it test that the instance variable is uninitialised, so I can
> prevent a warning? This is what I am trying to do:

>   class Foo
>     attr_reader :things
>     def add_thing(n)


>       end

>     end
>   end



> if ruby is running with -w. (This is 1.6.8)

> I am hoping that I don't have to keep track of all existing Foo objects,
> just so that I can send them a message 'reset_things'
>> class A
>> def add_thing

>> end
>> attr_reader :foo
>> end
=> nil
>> A.new.foo


=> nil
Quote:
>> a = A.new
=> #<A:0x4021d7c8>
>> a.add_thing
=> "test"
>> a.foo

=> "test"

--
 _           _                            
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
        Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

* LG loves czech girls.
<vincent> LG: do they have additional interesting "features" other girls don't have? ;)
        -- #Debian



Sat, 06 Aug 2005 02:42:52 GMT  
 How to test for existence of instance variable?
On Tue, 18 Feb 2003 03:33:53 +0900

<snip>

Quote:
> How can I make it test that the instance variable is uninitialised, so
> I can prevent a warning? This is what I am trying to do:

With "defined?" like any other variable:

Quote:
>   class Foo
>     attr_reader :things
>     def add_thing(n)

<snip>

Quote:

>       end

>     end
>   end

<snip>

--

"You mean easily pawned valuable crystal orb thingie." - 8BT



Sat, 06 Aug 2005 02:55:12 GMT  
 How to test for existence of instance variable?


Quote:
>I have an existing class Foo, and existing objects of that class.

>I now extend the class by adding a method, which makes use of an instance
>variable which previously did not exist.

>How can I make it test that the instance variable is uninitialised, so I can
>prevent a warning? This is what I am trying to do:

>  class Foo
>    attr_reader :things
>    def add_thing(n)


>      end

>    end
>  end



>if ruby is running with -w. (This is 1.6.8)




else

end

Phil



Sat, 06 Aug 2005 03:41:03 GMT  
 How to test for existence of instance variable?

Quote:

> I have an existing class Foo, and existing objects of that class.

> I now extend the class by adding a method, which makes use of an
> instance variable which previously did not exist.

> How can I make it test that the instance variable is uninitialised,
> so I can prevent a warning? This is what I am trying to do:

>   class Foo
>     attr_reader :things
>     def add_thing(n)


>       end

>     end
>   end

This won't generate a warning.

    def add_thing(n)


    end

--
matt



Sat, 06 Aug 2005 04:30:45 GMT  
 How to test for existence of instance variable?
Hi,


Quote:
> How can I make it test that the instance variable is uninitialised, so I can
> prevent a warning?

I think defined? might do the trick:

Quote:
>> class Foo
>>   def initialize



>>   end
>> end
=> nil
>> Foo.new

nil                   # the result of the 1st defined? test
"instance-variable"   # the result of the 2nd

HTH,

Bill



Sat, 06 Aug 2005 04:39:34 GMT  
 How to test for existence of instance variable?

Quote:

> I think defined? might do the trick:

> >> class Foo
> >>   def initialize




Doh, how did I miss that! I keep looking at the class reference and forget
that there are actually some reserved words in the language as well :-)

Thanks to everyone.

B.



Sat, 06 Aug 2005 05:27:47 GMT  
 How to test for existence of instance variable?

Quote:

> > How can I make it test that the instance variable is uninitialised, so I can
> > prevent a warning?

> I think defined? might do the trick:

OK, how about this one :-)

I have a Struct, and have dynamically added a new member.

I want to check whether the instance variable corresponding to that
attribute is already initialised. How can I do that? Example:

  foo = Struct.new("Foo", "a", "b", "c")
  obj = foo.new

  newmember="d"
  foo.module_eval { attr_accessor newmember }

  obj.send("#{newmember}=", []) if obj.send(newmember).nil?    # ***
  obj.send(newmember)<< "hello"
  puts obj.send(newmember)

How can I write the line '***' so it does not generate a warning under -w ?
Note that 'respond_to?' is not the answer, because the accessor methods do
exist - it's just the associated instance variable which has not been
initialised.

Cheers,

Brian.



Sat, 06 Aug 2005 20:11:50 GMT  
 How to test for existence of instance variable?

B> I want to check whether the instance variable corresponding to that
B> attribute is already initialised.

 First test if the instance variable exist.

Guy Decoux



Sat, 06 Aug 2005 20:30:46 GMT  
 How to test for existence of instance variable?
Hi,

In message "Re: How to test for existence of instance variable?"

|How can I write the line '***' so it does not generate a warning under -w ?

Attribute readers should not report warning for uninitialized instance
variables.  I will fix.

                                                        matz.



Sat, 06 Aug 2005 20:52:12 GMT  
 How to test for existence of instance variable?

Quote:
> B> I want to check whether the instance variable corresponding to that
> B> attribute is already initialised.

>  First test if the instance variable exist.

That was essentially what I was asking. What is the way to check if an
instance variable exists, when its name is dynamic? Preferably not something
incredibly inefficient like


(since all I'm trying to do is avoid a warning).

Regards,

Brian.



Sat, 06 Aug 2005 21:02:15 GMT  
 How to test for existence of instance variable?

B> That was essentially what I was asking. What is the way to check if an
B> instance variable exists, when its name is dynamic? Preferably not something
B> incredibly inefficient like

 #instance_variables give you the name of all instance variables for an
 object.

Guy Decoux



Sat, 06 Aug 2005 21:22:39 GMT  
 How to test for existence of instance variable?

Quote:

> Hi,

> In message "Re: How to test for existence of instance variable?"

> |How can I write the line '***' so it does not generate a warning under -w ?

> Attribute readers should not report warning for uninitialized instance
> variables.  I will fix.

Why should this be fixed?  The warning looks reasonable to me.

Paul



Sun, 07 Aug 2005 00:31:18 GMT  
 How to test for existence of instance variable?
Hi,

In message "Re: How to test for existence of instance variable?"

|> |How can I write the line '***' so it does not generate a warning under -w ?
|>
|> Attribute readers should not report warning for uninitialized instance
|> variables.  I will fix.
|
|Why should this be fixed?  The warning looks reasonable to me.

Because it's non intuitive.  There's no logical relationship between

users.

                                                        matz.



Sun, 07 Aug 2005 01:08:07 GMT  
 How to test for existence of instance variable?

Quote:
----- Original Message -----


Sent: Tuesday, February 18, 2003 2:19 PM
Subject: Re: How to test for existence of instance variable?


> > In message "Re: How to test for existence of instance variable?"

> > |Why should this be fixed?  The warning looks reasonable to me.

> > Because it's non intuitive.  There's no logical relationship between

> > users.

> Creating an attribute reader and not initializing the attribute (either

> warning except for buggy code.

What Paul says makes sense to me.

Hal



Sun, 07 Aug 2005 05:08:12 GMT  
 
 [ 42 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. Test the existence of a variable...

2. Clipper: Test for Existence of a Variable

3. Testing for QueryString variable existence

4. how to test the existence of a variable

5. Test for existence of environment variable...

6. Tcl-Tk Test of existence of a variable

7. Instances incorrectly sharing instance variables?

8. Test for existence of web file (URL)

9. Clipper 5.2e / W95 - Test for the existence of a subdirectory

10. test existence of file/directory

11. Testing for a class existence

12. Test for existence of files

 

 
Powered by phpBB® Forum Software