Skip to content

Instantly share code, notes, and snippets.

@tomGdow
Created March 11, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomGdow/26b88ea7bc43a70198ec to your computer and use it in GitHub Desktop.
Save tomGdow/26b88ea7bc43a70198ec to your computer and use it in GitHub Desktop.
Some Ruby Code Examples
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2><center>An Example of Jupyter Notebook with Ruby 2.1.1 (iRuby)</center></h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3><center> http://jupyter.org/</center></h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I am written in Markdown! Let's add some TeX:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$ \\frac{-b \\pm \\sqrt{b^2-4ac}}{2b}.$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some Ruby Code Examples "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Various Ways to Square an Array of Numbers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16]\n"
]
}
],
"source": [
"arr =[1,2,3,4].map(&lambda{|x| x * x})\n",
"puts arr\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16]\n"
]
}
],
"source": [
"arr2 =[1,2,3,4].map do |x| x * x end\n",
"puts arr2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"#<Proc:0x00000002a87b20@(pry):28>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tgdproc = Proc.new do |x|\n",
" x * x\n",
" end"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16]\n"
]
}
],
"source": [
"arr3 = [1,2,3,4].map(&tgdproc)\n",
"puts arr3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Some More Fun "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\"Thomas\", \"Gerard\", \"Dowling\"]\n"
]
}
],
"source": [
"arr3 = %w(thomas gerard dowling).map(&:capitalize)\n",
"\n",
"puts arr3.inspect"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HELLO\n",
"HELLO\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" 2.times do\n",
" puts \"hello\".upcase\n",
" end \n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a =[1,2,3,4,5]\n",
"a.select(&:even?)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
":greet"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def greet(name, informal=false)\n",
" if informal\n",
" \"hi #{name}\"\n",
" else\n",
" \"hello #{name}\"\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"hello tom\""
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"greet 'tom'"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"hi tom\""
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"greet('tom', informal=true)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See [here](http://jupyter.org/) for a great link\n",
" http://jupyter.org/"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.1",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment