Skip to content

Instantly share code, notes, and snippets.

@dictav
Last active December 15, 2015 07:08
Show Gist options
  • Save dictav/5220871 to your computer and use it in GitHub Desktop.
Save dictav/5220871 to your computer and use it in GitHub Desktop.
悔しかったらDynamic!
# 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