a = {one: 'A', two: nil}
b = {two: 'B', three: 'C'}
a.merge(b)
{:one=>"A", :two=>"B", :three=>"C"}
def keywords(**options)
defaults = {alice: 'ありす', bob: 'ぼぶ'}
hash = defaults.merge(options)
hash
end
keywords bob: 'ボブ'
{:alice=>"ありす", :bob=>"ボブ"}
hash = {one: 'A'}
p(hash.merge! two: 'B')
hash
{:one=>"A", :two=>"B"}
{:one=>"A", :two=>"B"}
p({one: 1, two: 2}.invert)
{foo: 1, bar: 1}.invert
{1=>:one, 2=>:two}
{1=>:bar}
hash = {foo: nil}
p(hash[:unknown])
p(hash[:foo])
p(hash.has_key?(:unknown))
hash.has_key?(:foo)
nil nil false
true
p(hash.key?(:foo))
p(hash.member?(:foo))
hash.include?(:foo)
true true
true
hash
{:foo=>nil}
hash[:bar] = 'buzz'
hash&.[](:bar)
"buzz"
hash&.fetch(:bar)
"buzz"
hash2 = nil
p(hash2&.fetch(:bar))
nil
hash = {foo: 'bar'}
p(hash.has_value?('bar'))
p(hash.has_value?('buzz'))
hash.value?('bar') #O(N)
true false
true
h = {one: 'A', two: 'B', three: 'C'}
h.keys
[:one, :two, :three]
h = {one: 'A', two: 'B', three: 'C'}
h.key('B')
:two
h = {one: 'A', two: 'B', three: 'C'}
p(h.values)
h.values_at(:three, :two)
["A", "B", "C"]
["C", "B"]
three, two = h.values_at(:three, :two)
p(two, three)
"B" "C"
["B", "C"]
hash = {
alice: {books: {wonder: 1865}},
charlie: {}
}
p(hash[:alice][:books][:wonder])
hash.dig(:alice, :books, :wonder)
1865
1865
hash[:charlie][:books][:chocolate]
NoMethodError: undefined method `[]' for nil:NilClass (pry):70:in `<main>' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>' /Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `load' /Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `<main>'
hash.dig(:charlie, :books, :chocolate)
hash = {a: [{word: 'alice'}, {word: 'alpha'}]}
hash.dig(:a, 0, :word)
# #dig methodがあれば、それを再帰的に掘る
"alice"
has_default = Hash.new('undefined')
has_default['foo']
"undefined"
has_default = Hash.new('naive')
value = has_default['foo']
value.reverse!
has_default['foo']
"evian"
hash = Hash.new{|h, k| h[k] = 0}
{}
['a', 'b', 'b', 'c'].each do |v|
hash[v] += 1
end
hash
{"a"=>1, "b"=>2, "c"=>1}
has_default = {}
p(has_default['foo'])
has_default.default = 'bar'
has_default['foo']
nil
"bar"
has_default.default_proc = ->(has, key) {Time.now}
has_default['foo']
2017-06-28 20:17:22 +0900
hash = {}
p(hash.fetch('foo', 'default'))
p(hash.fetch('foo'){|k| k})
hash.fetch('foo')
"default" "foo"
KeyError: key not found: "foo" (pry):107:in `fetch' (pry):107:in `<main>' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:65:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' /Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>' /Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `load' /Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `<main>'
a = {one: 1, two: 2}.to_a
p a
a.assoc(:one)
[[:one, 1], [:two, 2]]
[:one, 1]
a1 = [[:one, 1, '1']]
a1.assoc(:one)
[:one, 1, "1"]
ary = ['key1', 'one', 'key2', 'two']
p(Hash[*ary])
ary = [['key1', 'one'], ['key2', 'two']]
p(Hash[ary])
ary.to_h
{"key1"=>"one", "key2"=>"two"} {"key1"=>"one", "key2"=>"two"}
{"key1"=>"one", "key2"=>"two"}
次回は 5-5-4 Enumerator から