Skip to content

Instantly share code, notes, and snippets.

@lnznt
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save lnznt/b3c9516b1a736fc9b4c0 to your computer and use it in GitHub Desktop.

Select an option

Save lnznt/b3c9516b1a736fc9b4c0 to your computer and use it in GitHub Desktop.
Ruby: OpenStruct のキーは to_sym される ref: http://qiita.com/lnznt/items/f61c269d10d757a4ecab
p :key.eql? 'key' #=> false
---
# YAML サンプル
foo: hello, world
bar: have fun
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
require 'ostruct'
# new の引数にハッシュを渡し、値を設定することもできる
st2 = OpenStruct.new(foo: 'hello', bar: 10)
p st2.foo #=> "hello" # 値を取得
p st2.bar #=> 10 # 値を取得
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 で値を取得
hash = {}
hash[:key] = 'hello' # キーは Symbol
hash['key'] = 'goodbye' # キーは String
p hash[:key] #=> "hello"
p hash['key'] #=> "goodbye"
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
require 'ostruct'
hash = {}
hash[1] = 100
hash[1] #=> 100
st6 = OpenStruct.new
st6[1] = 100 #!! NoMethodError: undefined method `to_sym' for 1:Fixnum
require 'ostruct'
opts = OpenStruct.new(ARGV.getopts 'abc:')
p opts.a # opts[:a] でも同じ
p opts.b # opts[:b] でも同じ
:
opts = ARGV.getopts 'abc:'
p opts['a']
p opts['b']
:
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] でも同じ
require 'yaml'
hash = YAML.load open 'sample.yml'
p hash['foo'] #=> "hello, world"
p hash['bar'] #=> "have fun"
require 'ostruct'
st9 = OpenStruct.new('' => :foo, 'a b' => :bar, '1' => :baz)
p st9[''] #=> :foo
p st9['a b'] #=> :bar
p st9['1'] #=> :baz
# ...上の続き (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 と書いても同じ
# ...上の続き (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