{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# テキスト\n", "\n", "* [たのしいruby c1](TanoshiiRuby_v5_c1.pdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 文字出力\n", "\n", "* print\n", "* ダブルコーテーションとシングル\n", "* puts, p\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Ruby.\n" ] } ], "source": [ "# print_hello.rb\n", "print(\"Hello, Ruby.\\n\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, \n", "Ruby\n", "!\n" ] } ], "source": [ "print(\"Hello, \\nRuby\\n!\\n\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, \"Ruby.\"\n" ] } ], "source": [ "print(\"Hello, \\\"Ruby.\\\"\\n\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, \\\"Ruby.\\\"\\n" ] } ], "source": [ "print('Hello, \\\"Ruby.\\\"\\n')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, \n", "\tRuby.\n", "\"Hello, \\n\\tRuby.\"\n" ] }, { "data": { "text/plain": [ "\"Hello, \\n\\tRuby.\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# puts_and_p.rb\n", "puts \"Hello, \\n\\tRuby.\"\n", "p \"Hello, \\n\\tRuby.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 数値" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1+1=2\n" ] } ], "source": [ "# arith.rb\n", "print(\"1+1=\",1+1,\"\\n\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sin(3.1415) = 9.265358966049024e-05\n", "sqrt(10000) = 100.0\n" ] } ], "source": [ "include Math\n", "print(\"sin(3.1415) = \", sin(3.1415),\"\\n\")\n", "print(\"sqrt(10000) = \", sqrt(10000),\"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 変数" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "表面積=2200\n", "体積 =6000\n" ] } ], "source": [ "# area_volume.rb\n", "x = 10\n", "y = 20\n", "z = 30\n", "area = (x*y + y*z + z*x)*2\n", "volume = x*y*z\n", "print(\"表面積=\", area, \"\\n\")\n", "print(\"体積 =\", volume, \"\\n\")" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "表面積= 2200\n", "体積 = 6000\n" ] } ], "source": [ "print(\"表面積= #{area}\\n\")\n", "print(\"体積 = #{volume}\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# コメント\n", "\n", "* #以降がコメント\n", "* ブロックコメント(=begin_=end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 制御構造\n", "## 条件判断(if_then_else)\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bigger\n" ] } ], "source": [ "# bigger_smaller.rb\n", "a = 20\n", "if a >= 10\n", " print(\"bigger\\n\")\n", "else\n", " print(\"smaller\\n\")\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 繰り返し(loop)\n", "* while\n", "* times\n", "* each" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "# while_loop.rb\n", "i = 1 \n", "while i<=10\n", " print(i, \"\\n\")\n", " i += 1\n", "end" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. hello. " ] }, { "data": { "text/plain": [ "100" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# times_loop.rb\n", "100.times {\n", " print \"hello. \"\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 課題" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## helloの変形(hello_name.rb)\n", "自分の名前(例えば,name = 'bob')を変数に入れて\n", "> Hello, bob!\n", "\n", "と返すようにしなさい." ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 8回愛してる(hello_8.rb)\n", "\n", "> Hello, bob!\n", "\n", "を8回繰り返すようにmain loopを書き換えなさい.さらに,\n", "```bash\n", "ruby hello_8.rb 28s Mon Sep 4 17:25:47 2017\n", " 0 Hello, bob.\n", " 1 Hello, bob.\n", " 2 Hello, bob.\n", " 3 Hello, bob.\n", " 4 Hello, bob.\n", " 5 Hello, bob.\n", " 6 Hello, bob.\n", " 7 Hello, bob.\n", "```\n", "\n", "というように何回目かを付け足して表示するように書き換えなさい.\n", "\n", "ヒント:printfも試してみなさい." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 今日の日付\n", "\n", "> require 'date'\n", "\n", "してから今日の日付表示させなさい.formatも,含めて,たとえば,\n", "\n", "```bash\n", "Hello, bob!\n", "2017年 09月 04日\n", "```\n", "と返すようにしなさい.どうやるかはネットで調べよ." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.2", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.2" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }