{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 要素の追加と削除" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 4, 2, 3]\n" ] }, { "data": { "text/plain": [ "[5, 4, 2, 3, nil, nil, nil, nil, nil, nil, 1]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2, 3]\n", "\n", "array[0] = 5\n", "p(array)\n", "\n", "array[10] = 1\n", "array" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 4, 2, 3]\n", "[4, 4, 2, 3, 3]\n", "3\n" ] }, { "data": { "text/plain": [ "[4, 4, 2, 3]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2]\n", "\n", "array << 3\n", "p(array)\n", "array.push(3)\n", "p(array)\n", "\n", "p(array.pop)\n", "array" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "[4, 2, 3]\n" ] }, { "data": { "text/plain": [ "[4, 4, 2, 3]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2, 3]\n", "\n", "p(array.shift)\n", "p(array)\n", "array.unshift 4\n", "array" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n", "[2, 4]\n", "[1, 3]\n" ] }, { "data": { "text/plain": [ "[1, 3]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [1, 2, 3, 4]\n", "p(array.select! {|v| v.even?})\n", "p(array)\n", "\n", "array = [1, 2, 3, 4]\n", "p(array.reject!{|v| v.even?})\n", "array" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.even?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.even?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4]\n" ] }, { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [1, 2, 3, 4]\n", "p(array.select {|v| v.even?})\n", "array" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n", "nil\n" ] }, { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [1, 2, 3, 4]\n", "p(array.delete_if {|v| false })\n", "p(array.reject! {|v| false })\n", "array" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] }, { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p(array.reject {|v| false })" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- immutable\n", " - 変更されない変数、値、オブジェクト\n", "- mutable\n", " - 変更できる変数、値、オブジェクト" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4.0, 2, 3]\n", "array.delete 4\n", "array" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 == 4" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 == 4.0" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[4, 2, 3]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array = [4, 4, 2, 3]\n", "array.delete_at 1\n", "array" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.==(4.0)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 == 4.0" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.9999999999999999999999" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "true" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 == 3.9999999999999999999999" ] }, { "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 }