{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 5-5-2 Array" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 1, 1, 1, 1]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Array.new(5, 1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 1, 1, 1, 1]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1] * 5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[\"ikasawak\", \"ikasawak\", \"ikasawak\", \"ikasawak\", \"ikasawak\"]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = Array.new(5, 'kawasaki')\n", "array[0].reverse!\n", "\n", "array" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Array.new(3) {|i| i.succ }" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Array.new(3) {|i| i + 1 }" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"Bob\"]\n", "[\"Bob\"]\n" ] }, { "data": { "text/plain": [ "[]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p Array('Bob')\n", "p Array(['Bob'])\n", "Array(nil)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NoMethodError", "evalue": "undefined method `wrap' for Array:Class", "output_type": "error", "traceback": [ "\u001b[31mNoMethodError\u001b[0m: undefined method `wrap' for Array:Class", "\u001b[37m(pry):14: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": [ "Array.wrap('Bob') # need ActiveSupport" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "false\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2, 3]\n", "\n", "p array.length\n", "p array.empty?\n", "[].empty?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array.include?(4)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3] + [4,5]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"abcdef\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\" + \"def\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 1, 3, 2] - [1, 2]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] & [2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] | [2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] * 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "[2, 3]\n" ] }, { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2, 3]\n", "\n", "p array[2]\n", "p array[2, 2]\n", "array[2..4]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4]\n", "[4, 3]\n" ] }, { "data": { "text/plain": [ "[4, 3]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p array.values_at(1)\n", "p array.values_at(1, 3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "[4, 4]\n", "3\n" ] }, { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p array.first\n", "p array.first(2)\n", "\n", "p array.last\n", "array.last(2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array[-2..-1] == array.last(2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] }, { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p array.sample\n", "array.sample(2)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[:bar, 2]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary = [[:foo, 4], [:bar, 2], [:buzz, 3]]\n", "ary.assoc(:bar)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ary.assoc(3)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[:buzz, 3, \"hogehogehoge\"]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ary2 = [[:foo, 4, 'hoge'], [:bar, 2, 'hogehoge'], [:buzz, 3, 'hogehogehoge']]\n", "ary2.assoc(:buzz)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次回、要素の追加と削除から" ] } ], "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 }