Created
May 18, 2016 09:52
-
-
Save kkyouhei/7c6d688a301c10c3da2c65008c716be4 to your computer and use it in GitHub Desktop.
initializer set attr_accessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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