# Object attributes list

**URL:** <https://rubytalk.org/t/object-attributes-list/2001>\
**Category:** ruby-talk\
**Created:** [26 September 2002 15:54 UTC](https://rubytalk.org/t/object-attributes-list/2001 "2002-09-26T15:54:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajksharma1](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ajksharma1](https://rubytalk.org/u/Ajksharma1)\
**Post date:** [26 September 2002 15:54 UTC](https://rubytalk.org/t/object-attributes-list/2001/1 "2002-09-26T15:54:58Z")

</div>

HI,  
I have a Object  
Customer  
.Customerid=“Alfki”  
.city = "Belin"  
etc  
I want to get a list of these attributes and their values,  
Possibly in a hash form.  
Customers = { “Customerid” =\> “Alfki”,  
“city” =\> “Belin”  
}

Regards  
Ajay

---

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [26 September 2002 17:55 UTC](https://rubytalk.org/t/object-attributes-list/2001/2 "2002-09-26T17:55:49Z")

</div>

i did this using #instance\_variables

-transami

> **···**
>
> On Thu, 2002-09-26 at 09:54, ajksharma wrote:
> 
> > HI,  
> > I have a Object  
> > Customer  
> > .Customerid=“Alfki”  
> > .city = “Belin”  
> > etc  
> > I want to get a list of these attributes and their values,  
> > Possibly in a hash form.  
> > Customers = { “Customerid” =\> “Alfki”,  
> > “city” =\> “Belin”  
> > }
> > 
> > Regards  
> > Ajay
> 
> –  
> tom sawyer, aka transami  
> [transami@transami.net](mailto:transami@transami.net)

---

<div class="post-metadata">

**Author:** ![William\_Djaja\_Tjokr1](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@William\_Djaja\_Tjokr1](https://rubytalk.org/u/William_Djaja_Tjokr1)\
**Post date:** [26 September 2002 18:15 UTC](https://rubytalk.org/t/object-attributes-list/2001/3 "2002-09-26T18:15:57Z")

</div>

Hi,

I think the key to what you want to accomplish it the  
Object#instance\_variables method. Here is an example; I am sure other  
people can write better Ruby code:

class Customer  
attr\_reader :Customerid, :city  
def initialize  
@Customerid = 123  
@city = ‘abc’  
end  
end

c = Customer.new

hash = {}  
c.instance\_variables.each do |var|  
var = var.slice (1…(var.length))  
val = eval “c.#{var}”  
hash[var] = val  
end  
hash.each\_key {|key| puts “"#{key}" =\> "#{hash[key]}"”}

Regards,

Bill

> **···**
>
> ============================================================================  
> ajksharma [ajksharma@yahoo.com](mailto:ajksharma@yahoo.com) wrote:
> 
> > HI,  
> > I have a Object  
> > Customer  
> > .Customerid=“Alfki”  
> > .city = “Belin”  
> > etc  
> > I want to get a list of these attributes and their values,  
> > Possibly in a hash form.  
> > Customers = { “Customerid” =\> “Alfki”,  
> > “city” =\> “Belin”  
> > }
> 
> > Regards  
> > Ajay

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Mirra4](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Massimiliano\_Mirra4](https://rubytalk.org/u/Massimiliano_Mirra4)\
**Post date:** [29 September 2002 11:22 UTC](https://rubytalk.org/t/object-attributes-list/2001/4 "2002-09-29T11:22:40Z")

</div>

> HI,  
> I have a Object  
> Customer  
> .Customerid=“Alfki”  
> .city = “Belin”  
> etc  
> I want to get a list of these attributes and their values,  
> Possibly in a hash form.  
> Customers = { “Customerid” =\> “Alfki”,  
> “city” =\> “Belin”  
> }

Put them in a hash in the first place.

class Customer  
def initialize  
@data = {  
‘Customerid’ =\> nil,  
‘city’ =\> nil  
}  
end  
attr\_reader :data

def method\_missing(id, \*args, &block)  
possible\_key = id.to\_s  
is\_setter = possible\_key.chomp!(‘=’)

```
if @data.has_key?(possible_key)
  if is_setter
    @data[possible_key] = *args
  else
    return @data[possible_key]
  end
else
  super
end     

```

end  
end

c = Customer.new  
c.Customerid = ‘Alfki’  
c.city = ‘Belin’  
p c.data

> > > {“city”=\>[“Belin”], “Customerid”=\>[“Alfki”]}

Massimiliano

> **···**
>
> On Fri, Sep 27, 2002 at 12:54:58AM +0900, ajksharma wrote:

---

<div class="post-metadata">

**Author:** ![Ajksharma1](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Ajksharma1](https://rubytalk.org/u/Ajksharma1)\
**Post date:** [27 September 2002 10:21 UTC](https://rubytalk.org/t/object-attributes-list/2001/5 "2002-09-27T10:21:45Z")

</div>

Hi,  
Thanks , it works.  
Regards  
Ajay

> **···**
>
> > I think the key to what you want to accomplish it the  
> > Object#instance\_variables method. Here is an example; I am sure other  
> > people can write better Ruby code:
> > 
> > class Customer  
> > attr\_reader :Customerid, :city  
> > def initialize  
> > @Customerid = 123  
> > @city = ‘abc’  
> > end  
> > end
> > 
> > c = Customer.new
> > 
> > hash = {}  
> > c.instance\_variables.each do |var|  
> > var = var.slice (1…(var.length))  
> > val = eval “c.#{var}”  
> > hash[var] = val  
> > end  
> > hash.each\_key {|key| puts “"#{key}" =\> "#{hash[key]}"”}
> > 
> > Regards,
> > 
> > Bill

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [27 September 2002 12:01 UTC](https://rubytalk.org/t/object-attributes-list/2001/6 "2002-09-27T12:01:59Z")

</div>

William Djaja Tjokroaminata wrote:

> class Customer  
> attr\_reader :Customerid, :city  
> def initialize  
> @Customerid = 123  
> @city = ‘abc’  
> end  
> end
> 
> c = Customer.new
> 
> hash = {}  
> c.instance\_variables.each do |var|  
> var = var.slice (1…(var.length))  
> val = eval “c.#{var}”  
> hash[var] = val  
> end  
> hash.each\_key {|key| puts “"#{key}" =\> "#{hash[key]}"”}

This works wonders as long as

1. all instance variables are exposed with a reader
2. no method has the name of an instance variable and does anything  
different from returning the variable.

I’d be more inclined to do something like this (mercilessly refactored  
from the above):  
`
class Customer
attr_reader :Customerid, :city
def initialize
@Customerid = 123
@city = ‘abc’
@secret = ‘hush hush’
end
def secret
“[confidential]”
end
end`

`
c = Customer.new
class Object
def instance_variables_to_hash
result = {}
self.instance_variables.each do |var|
result[var] = eval var
end
result
end
end
`

`hash = c.instance_variables_to_hash
hash.each_key {|key| puts “"#{key}" => "#{hash[key]}"”}
`

Not sure this will work too well when we get truely private instance  
variables (i.e. protected from subclasses… Will they also be protected  
from superclasses?)

> **···**
>
> –  
> ([Kent Dahl]/)\_ _~_**[[http://www.stud.ntnu.no/~kentda/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 5. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [27 September 2002 12:20 UTC](https://rubytalk.org/t/object-attributes-list/2001/7 "2002-09-27T12:20:03Z")

</div>

> Not sure this will work too well when we get truely private instance  
> variables (i.e. protected from subclasses... Will they also be protected  
> from superclasses?)

Well, actually my stupid implementation do this

pigeon% cat b.rb  
#!./ruby  
class Customer  
&nbsp;&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@\_Customerid = 123  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@\_city = 'abc'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@\_secret = 'hush hush'  
&nbsp;&nbsp;&nbsp;end  
end

c = Customer.new

class Object  
&nbsp;&nbsp;def class\_local\_variables\_to\_hash  
&nbsp;&nbsp;&nbsp;&nbsp;result = {}  
&nbsp;&nbsp;&nbsp;&nbsp;self.class\_local\_variables(type).each do |var|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[var] = eval var  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;end  
end

hash = c.class\_local\_variables\_to\_hash  
hash.each\_key {|key| puts "\"#{key}\" =\> \"#{hash[key]}\""}  
pigeon%

pigeon% b.rb  
"@\_city" =\> ""  
"@\_secret" =\> ""  
"@\_Customerid" =\> ""  
pigeon%

#eval is evaluated with current\_class == rb\_cObject

Guy Decoux
