damn simple!!!, define instance variables in ruby way

i was creating a new class where a hash object will be passed with value and key. my class suppose to iterate for each key and value. and those key will be treated as the instance variable.

so the pretty simple job i did is the following lines of code block -

def initialize(p_params = {})
super()
unless p_params.empty?
p_params.each do |key, value|
eval %(
self.#{key} = value
)

end
end
end

surprisingly my instance variable declaration became soooooo simple and easy.

update:
i didn’t know that self.send(“#{key}=”, value) does the same job, obviously send is prefer way to do this job.

my tweets

 

August 2007
S S M T W T F
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Flickr Photos

@kamalapur over bridge

@kamalapur station

cox's bazaar trip oct 09

cox's bazaar trip oct 09

cast ur vote!

More Photos
Follow

Get every new post delivered to your Inbox.