Last active
December 15, 2015 07:08
-
-
Save dictav/5220871 to your computer and use it in GitHub Desktop.
悔しかったらDynamic!
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
# Ruby には OpenStruct (require 'ostruct') もあるから微妙だけど。 | |
# | |
module Dynamic | |
def method_missing(action, *args) | |
if args.empty? | |
self[action] | |
elsif action.match(/=$/) | |
self[action.to_s.chop.to_sym] = args.first | |
end | |
end | |
end | |
class Hash | |
include Dynamic | |
def self.ltsv(str) | |
str.gsub!(/:(\w+)\t?/, ':"\1",') | |
eval "{#{str.chop}}" | |
end | |
end | |
hoge = {} | |
hoge.piyo = 1 | |
p hoge #=> {:piyo => 1} | |
p Hash.ltsv "hoge:1\tpiyo:huga" #=> {:hoge => "1", :piyo => "huga"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment