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
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
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
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 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
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:
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
endc = 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
William Djaja Tjokroaminata wrote:
class Customer
attr_reader :Customerid, :city
def initialize
@Customerid = 123
@city = ‘abc’
end
endc = 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
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/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
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
def initialize
@_Customerid = 123
@_city = 'abc'
@_secret = 'hush hush'
end
end
c = Customer.new
class Object
def class_local_variables_to_hash
result = {}
self.class_local_variables(type).each do |var|
result[var] = eval var
end
result
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