Skip to content

Instantly share code, notes, and snippets.

@kkyouhei
Created May 18, 2016 09:52
Show Gist options
  • Save kkyouhei/7c6d688a301c10c3da2c65008c716be4 to your computer and use it in GitHub Desktop.
Save kkyouhei/7c6d688a301c10c3da2c65008c716be4 to your computer and use it in GitHub Desktop.
initializer set attr_accessor
class DataStore
attr_accessor :hoge
attr_accessor :fuga
def initialize *params
params = params.first
unless params.empty?
params.each do |k, v|
if self.respond_to?(v) && self.respond_to?("#{v}=")
self.instance_variable_set("@#{k}".to_sym, v)
end
end
end
end
end
data = DataStore.new hoge: "hoge", fuga: "fuga", nothing_val: "val"
p data # <DataStore:0x007ff4f8b903a8 @hoge="hoge", @fuga="fuga">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment