Advertisement
tomgdow

tomgdowtips iRuby Example

May 8th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 3.69 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "markdown",
  5.    "metadata": {},
  6.    "source": [
  7.     "## Various Ways to Square an Array of Numbers in Ruby"
  8.    ]
  9.   },
  10.   {
  11.    "cell_type": "code",
  12.    "execution_count": 1,
  13.    "metadata": {
  14.     "collapsed": false
  15.    },
  16.    "outputs": [
  17.     {
  18.      "name": "stdout",
  19.      "output_type": "stream",
  20.      "text": [
  21.       "[1, 4, 9, 16, 25, 36]\n"
  22.      ]
  23.     }
  24.    ],
  25.    "source": [
  26.     "arr = [1,2,3,4, 5, 6].map(&lambda{|x|  x * x})\n",
  27.     "puts arr"
  28.    ]
  29.   },
  30.   {
  31.    "cell_type": "code",
  32.    "execution_count": 4,
  33.    "metadata": {
  34.     "collapsed": false
  35.    },
  36.    "outputs": [
  37.     {
  38.      "name": "stdout",
  39.      "output_type": "stream",
  40.      "text": [
  41.       "[1, 4, 9, 16]\n"
  42.      ]
  43.     }
  44.    ],
  45.    "source": [
  46.     "arr2 =[1,2,3,4].map do |x|  x * x end\n",
  47.     "puts arr2"
  48.    ]
  49.   },
  50.   {
  51.    "cell_type": "code",
  52.    "execution_count": 5,
  53.    "metadata": {
  54.     "collapsed": false
  55.    },
  56.    "outputs": [
  57.     {
  58.      "data": {
  59.       "text/plain": [
  60.        "#<Proc:0x0000000176e228@<main>:0>"
  61.       ]
  62.      },
  63.      "execution_count": 5,
  64.      "metadata": {},
  65.      "output_type": "execute_result"
  66.     }
  67.    ],
  68.    "source": [
  69.     "myproc = Proc.new do |x|\n",
  70.     "  x * x\n",
  71.     "  end"
  72.    ]
  73.   },
  74.   {
  75.    "cell_type": "code",
  76.    "execution_count": 7,
  77.    "metadata": {
  78.     "collapsed": false
  79.    },
  80.    "outputs": [
  81.     {
  82.      "name": "stdout",
  83.      "output_type": "stream",
  84.      "text": [
  85.       "[1, 4, 9, 16]\n"
  86.      ]
  87.     }
  88.    ],
  89.    "source": [
  90.     "arr3 = [1,2,3,4].map(&myproc)\n",
  91.     "puts arr3"
  92.    ]
  93.   },
  94.   {
  95.    "cell_type": "markdown",
  96.    "metadata": {},
  97.    "source": [
  98.     "## Some More Fun"
  99.    ]
  100.   },
  101.   {
  102.    "cell_type": "code",
  103.    "execution_count": 9,
  104.    "metadata": {
  105.     "collapsed": false
  106.    },
  107.    "outputs": [
  108.     {
  109.      "data": {
  110.       "text/plain": [
  111.        "[4, 16]"
  112.       ]
  113.      },
  114.      "execution_count": 9,
  115.      "metadata": {},
  116.      "output_type": "execute_result"
  117.     }
  118.    ],
  119.    "source": [
  120.     "arr2.select(&:even?)"
  121.    ]
  122.   },
  123.   {
  124.    "cell_type": "code",
  125.    "execution_count": 11,
  126.    "metadata": {
  127.     "collapsed": false
  128.    },
  129.    "outputs": [
  130.     {
  131.      "data": {
  132.       "text/plain": [
  133.        ":greet"
  134.       ]
  135.      },
  136.      "execution_count": 11,
  137.      "metadata": {},
  138.      "output_type": "execute_result"
  139.     }
  140.    ],
  141.    "source": [
  142.     "def greet(name, informal=false)\n",
  143.     "  if informal\n",
  144.     "    \"hi #{name}\"\n",
  145.     "  else\n",
  146.     "    \"hello #{name}\"\n",
  147.     "  end\n",
  148.     "end"
  149.    ]
  150.   },
  151.   {
  152.    "cell_type": "code",
  153.    "execution_count": 19,
  154.    "metadata": {
  155.     "collapsed": false
  156.    },
  157.    "outputs": [
  158.     {
  159.      "data": {
  160.       "text/plain": [
  161.        "\"hello john\""
  162.       ]
  163.      },
  164.      "execution_count": 19,
  165.      "metadata": {},
  166.      "output_type": "execute_result"
  167.     }
  168.    ],
  169.    "source": [
  170.     "greet 'john'\n"
  171.    ]
  172.   },
  173.   {
  174.    "cell_type": "code",
  175.    "execution_count": 20,
  176.    "metadata": {
  177.     "collapsed": false
  178.    },
  179.    "outputs": [
  180.     {
  181.      "data": {
  182.       "text/plain": [
  183.        "\"hi jane\""
  184.       ]
  185.      },
  186.      "execution_count": 20,
  187.      "metadata": {},
  188.      "output_type": "execute_result"
  189.     }
  190.    ],
  191.    "source": [
  192.     "greet('jane', informal=true)"
  193.    ]
  194.   },
  195.   {
  196.    "cell_type": "code",
  197.    "execution_count": null,
  198.    "metadata": {
  199.     "collapsed": true
  200.    },
  201.    "outputs": [],
  202.    "source": []
  203.   }
  204.  ],
  205.  "metadata": {
  206.   "kernelspec": {
  207.    "display_name": "Ruby 2.1.1",
  208.    "language": "ruby",
  209.    "name": "ruby"
  210.   },
  211.   "language_info": {
  212.    "file_extension": ".rb",
  213.    "mimetype": "application/x-ruby",
  214.    "name": "ruby",
  215.    "version": "2.1.1"
  216.   }
  217.  },
  218.  "nbformat": 4,
  219.  "nbformat_minor": 0
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement