instance variables by name? 
Author Message
 instance variables by name?

i'd like to have a method that operates on an instance variable given its
name...

class cat
        def initialize()

        end

        def set(name, value)

        end
end

cat.new().set('says', 'arf')

i figure it can be done but i cant find out how based on the documentation
i've found.

thanks
 aaron



Tue, 06 Jan 2004 02:18:31 GMT  
 instance variables by name?

Quote:


> > i'd like to have a method that operates on an instance variable given its
> > name...

> > class cat
> >       def initialize()

> >       end

> >       def set(name, value)

> >       end
> > end

> > cat.new().set('says', 'arf')

> > i figure it can be done but i cant find out how based on the documentation
> > i've found.

> > thanks
> >  aaron

> here ya go

> class Cat
>     def initialize

>     end

>     attr_accessor :says

>     def set(name, value)
>         self.send(name+"=", value)

Or without defining an attr_accessor before:


Regards,

  Michael

--
Michael Neumann
merlin.zwo InfoDesign GmbH
http://www.merlin-zwo.de



Tue, 06 Jan 2004 04:12:13 GMT  
 instance variables by name?

Quote:

> i'd like to have a method that operates on an instance variable given its
> name...

> class cat
>    def initialize()

>    end

>    def set(name, value)

>    end
> end

> cat.new().set('says', 'arf')

> i figure it can be done but i cant find out how based on the documentation
> i've found.

> thanks
>  aaron

here ya go

class Cat
    def initialize

    end

    attr_accessor :says

    def set(name, value)
        self.send(name+"=", value)
    end
end

Cat.new.set('says','arf')

--

Doubt is a pain too lonely to know that faith is his twin brother.
- Kahlil Gibran



Tue, 06 Jan 2004 02:48:44 GMT  
 instance variables by name?

Quote:
> self.send(name+"=", value)

thanks for the reply, i tried it and i get...

        undefined method `says=' for #<Cat:0xblahblah>



Tue, 06 Jan 2004 03:23:37 GMT  
 instance variables by name?

ah!  'says=' becomes defined when i set it as attr_writer, not exactly what
i wanted to do, i wanted to protect direct access and have this method
control it.  i'd also like to do something similar for reading.  i'm doing
this because i want to filter certain values.

aaron

Quote:
-----Original Message-----
From: Barnett, Aaron
Sent: Thursday, July 19, 2001 3:24 PM

Subject: [ruby-talk:18142] Re: instance variables by name?

> self.send(name+"=", value)

thanks for the reply, i tried it and i get...

        undefined method `says=' for #<Cat:0xblahblah>



Tue, 06 Jan 2004 03:28:03 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to name instance variables

2. Variable component and instance names

3. Using a variable name for a variable name??

4. Instances incorrectly sharing instance variables?

5. field name and key name as a variable

6. field name and key name as a variable

7. instances of classes and instances of instances

8. variable contents to variable name

9. array name in variable variable

10. getting variable name from the variable itself?

11. Variables containing variable names

12. name of a variable stored in another variable

 

 
Powered by phpBB® Forum Software