{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### ■マージ" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{:one=>\"A\", :two=>\"B\", :three=>\"C\"}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {one: 'A', two: nil}\n", "b = {two: 'B', three: 'C'}\n", "\n", "a.merge(b)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{:alice=>\"ありす\", :bob=>\"ボブ\"}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def keywords(**options)\n", " defaults = {alice: 'ありす', bob: 'ぼぶ'}\n", " hash = defaults.merge(options)\n", " \n", " hash\n", "end\n", "\n", "keywords bob: 'ボブ'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{:one=>\"A\", :two=>\"B\"}\n" ] }, { "data": { "text/plain": [ "{:one=>\"A\", :two=>\"B\"}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {one: 'A'}\n", "\n", "p(hash.merge! two: 'B')\n", "hash" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■キーと値の入れ替え" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1=>:one, 2=>:two}\n" ] }, { "data": { "text/plain": [ "{1=>:bar}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p({one: 1, two: 2}.invert)\n", "{foo: 1, bar: 1}.invert" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■キーや値が存在しているか" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil\n", "nil\n", "false\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: nil}\n", "\n", "p(hash[:unknown])\n", "p(hash[:foo])\n", "\n", "p(hash.has_key?(:unknown))\n", "hash.has_key?(:foo)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n", "true\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p(hash.key?(:foo))\n", "p(hash.member?(:foo))\n", "hash.include?(:foo)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{:foo=>nil}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"buzz\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash[:bar] = 'buzz'\n", "hash&.[](:bar)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"buzz\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash&.fetch(:bar)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil\n" ] } ], "source": [ "hash2 = nil\n", "p(hash2&.fetch(:bar))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n", "false\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {foo: 'bar'}\n", "\n", "p(hash.has_value?('bar'))\n", "p(hash.has_value?('buzz'))\n", "\n", "hash.value?('bar') #O(N)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■キーや値の取得" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[:one, :two, :three]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = {one: 'A', two: 'B', three: 'C'}\n", "h.keys" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ":two" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = {one: 'A', two: 'B', three: 'C'}\n", "h.key('B')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"A\", \"B\", \"C\"]\n" ] }, { "data": { "text/plain": [ "[\"C\", \"B\"]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = {one: 'A', two: 'B', three: 'C'}\n", "\n", "p(h.values)\n", "h.values_at(:three, :two)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"B\"\n", "\"C\"\n" ] }, { "data": { "text/plain": [ "[\"B\", \"C\"]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "three, two = h.values_at(:three, :two)\n", "\n", "p(two, three)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1865\n" ] }, { "data": { "text/plain": [ "1865" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {\n", " alice: {books: {wonder: 1865}},\n", " charlie: {}\n", "}\n", "\n", "p(hash[:alice][:books][:wonder])\n", "hash.dig(:alice, :books, :wonder)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NoMethodError", "evalue": "undefined method `[]' for nil:NilClass", "output_type": "error", "traceback": [ "\u001b[31mNoMethodError\u001b[0m: undefined method `[]' for nil:NilClass", "\u001b[37m(pry):70:in `
'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/bin/iruby:5:in `'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `load'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `
'\u001b[0m" ] } ], "source": [ "hash[:charlie][:books][:chocolate]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hash.dig(:charlie, :books, :chocolate)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"alice\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = {a: [{word: 'alice'}, {word: 'alpha'}]}\n", "hash.dig(:a, 0, :word) \n", "# #dig methodがあれば、それを再帰的に掘る" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■デフォルト値" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"undefined\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_default = Hash.new('undefined')\n", "has_default['foo']" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"evian\"" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_default = Hash.new('naive')\n", "value = has_default['foo']\n", "\n", "value.reverse!\n", "\n", "has_default['foo']" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash = Hash.new{|h, k| h[k] = 0}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{\"a\"=>1, \"b\"=>2, \"c\"=>1}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['a', 'b', 'b', 'c'].each do |v|\n", " hash[v] += 1\n", "end\n", "hash" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nil\n" ] }, { "data": { "text/plain": [ "\"bar\"" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_default = {}\n", "p(has_default['foo'])\n", "\n", "has_default.default = 'bar'\n", "has_default['foo']" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2017-06-28 20:17:22 +0900" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "has_default.default_proc = ->(has, key) {Time.now}\n", "has_default['foo']" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"default\"\n", "\"foo\"\n" ] }, { "ename": "KeyError", "evalue": "key not found: \"foo\"", "output_type": "error", "traceback": [ "\u001b[31mKeyError\u001b[0m: key not found: \"foo\"", "\u001b[37m(pry):107:in `fetch'\u001b[0m", "\u001b[37m(pry):107:in `
'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/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'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/lib/ruby/gems/2.4.0/gems/iruby-0.2.9/bin/iruby:5:in `'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `load'\u001b[0m", "\u001b[37m/Users/ariga/.rbenv/versions/2.4.0-preview2/bin/iruby:22:in `
'\u001b[0m" ] } ], "source": [ "hash = {}\n", "p(hash.fetch('foo', 'default'))\n", "p(hash.fetch('foo'){|k| k})\n", "hash.fetch('foo')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ■ハッシュの変換" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[:one, 1], [:two, 2]]\n" ] }, { "data": { "text/plain": [ "[:one, 1]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {one: 1, two: 2}.to_a\n", "p a\n", "\n", "a.assoc(:one)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[:one, 1, \"1\"]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1 = [[:one, 1, '1']]\n", "a1.assoc(:one)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"key1\"=>\"one\", \"key2\"=>\"two\"}\n", "{\"key1\"=>\"one\", \"key2\"=>\"two\"}\n" ] }, { "data": { "text/plain": [ "{\"key1\"=>\"one\", \"key2\"=>\"two\"}" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary = ['key1', 'one', 'key2', 'two']\n", "p(Hash[*ary])\n", "\n", "ary = [['key1', 'one'], ['key2', 'two']]\n", "p(Hash[ary])\n", "\n", "ary.to_h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次回は 5-5-4 Enumerator から" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.0", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.0" } }, "nbformat": 4, "nbformat_minor": 1 }