Last active
August 29, 2015 14:12
-
-
Save lnznt/b3c9516b1a736fc9b4c0 to your computer and use it in GitHub Desktop.
Ruby: OpenStruct のキーは to_sym される ref: http://qiita.com/lnznt/items/f61c269d10d757a4ecab
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
| p :key.eql? 'key' #=> false |
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
| --- | |
| # YAML サンプル | |
| foo: hello, world | |
| bar: have fun |
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
| require 'ostruct' | |
| st1 = OpenStruct.new | |
| st1.foo = 'hello' # この時 foo/foo= が作られる | |
| st1.bar = 10 # この時 bar/bar= が作られる | |
| p st1.foo #=> "hello" # 値を取得 | |
| p st1.bar #=> 10 # 値を取得 | |
| p st1.baz #=> nil # 定義されてないものは nil |
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
| require 'ostruct' | |
| # new の引数にハッシュを渡し、値を設定することもできる | |
| st2 = OpenStruct.new(foo: 'hello', bar: 10) | |
| p st2.foo #=> "hello" # 値を取得 | |
| p st2.bar #=> 10 # 値を取得 |
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
| require 'ostruct' | |
| st3 = OpenStruct.new | |
| st3[:foo] = 'hello' # `OpenStruct#[]=` で値を設定 | |
| p st3[:foo] #=> "hello" # `OpenStruct#[]` で値を取得 | |
| p st3.foo #=> "hello" # getter で値を取得 | |
| st3.bar = 10 # setter で値を設定 | |
| p st3[:bar] #=> 10 # `OpenStruct#[]` メソッドで値を取得 | |
| p st3.bar #=> 10 # getter で値を取得 |
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
| hash = {} | |
| hash[:key] = 'hello' # キーは Symbol | |
| hash['key'] = 'goodbye' # キーは String | |
| p hash[:key] #=> "hello" | |
| p hash['key'] #=> "goodbye" |
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
| require 'ostruct' | |
| p 'key'.to_sym #=> :key # 'key' を to_sym すると :key | |
| st5 = OpenStruct.new | |
| st5[:key] = 'hello' | |
| st5[:key] #=> "hello" | |
| st5['key'] #=> "hello" | |
| st5['key'] = 'goodbye' | |
| st5[:key] #=> "goodbye" | |
| st5['key'] #=> "goodbye" | |
| st5.foo = 100 | |
| st5[:foo] #=> 100 | |
| st5['foo'] #=> 100 | |
| # 適当なオブジェクトでも to_sym で :foo を返すようにすれば... | |
| obj = Object.new | |
| def obj.to_sym | |
| :foo | |
| end | |
| p obj.to_sym #=> :foo | |
| # ... :foo の値にアクセスできるようになる | |
| st5[obj] #=> 100 |
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
| require 'ostruct' | |
| hash = {} | |
| hash[1] = 100 | |
| hash[1] #=> 100 | |
| st6 = OpenStruct.new | |
| st6[1] = 100 #!! NoMethodError: undefined method `to_sym' for 1:Fixnum |
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
| require 'ostruct' | |
| opts = OpenStruct.new(ARGV.getopts 'abc:') | |
| p opts.a # opts[:a] でも同じ | |
| p opts.b # opts[:b] でも同じ | |
| : |
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
| opts = ARGV.getopts 'abc:' | |
| p opts['a'] | |
| p opts['b'] | |
| : |
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
| require 'yaml' | |
| require 'ostruct' | |
| st8 = OpenStruct.new(YAML.load open 'sample.yml') | |
| p st8.foo #=> "hello, world" # p st[:foo] でも同じ | |
| p st8.bar #=> "have fun" # p st[:bar] でも同じ |
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
| require 'yaml' | |
| hash = YAML.load open 'sample.yml' | |
| p hash['foo'] #=> "hello, world" | |
| p hash['bar'] #=> "have fun" |
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
| require 'ostruct' | |
| st9 = OpenStruct.new('' => :foo, 'a b' => :bar, '1' => :baz) | |
| p st9[''] #=> :foo | |
| p st9['a b'] #=> :bar | |
| p st9['1'] #=> :baz |
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
| # ...上の続き (getter の場合) | |
| st9.send '' #=> :foo | |
| st9.send 'a b' #=> :bar | |
| st9.send '1' #=> :baz | |
| st9.method('').() #=> :foo # st9.method('').call と書いても同じ | |
| st9.method('a b').() #=> :bar # st9.method('a b').call と書いても同じ | |
| st9.method('1').() #=> :baz # st9.method('1').call と書いても同じ |
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
| # ...上の続き (setter の場合) | |
| st9.send '=', 10 | |
| st9.send 'a b=', 20 | |
| st9.send '1=', 30 | |
| p st9[''] #=> 10 | |
| p st9['a b'] #=> 20 | |
| p st9['1'] #=> 30 | |
| st9.method('=').(40) # st9.method('=').call(40) と書いても同じ | |
| st9.method('a b=').(50) # st9.method('a b=').call(50) と書いても同じ | |
| st9.method('1=').(60) # st9.method('1=').call(60) と書いても同じ | |
| p st9[''] #=> 40 | |
| p st9['a b'] #=> 50 | |
| p st9['1'] #=> 60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment