Skip to content

Instantly share code, notes, and snippets.

@coopermayne
Created July 21, 2015 16:13
Show Gist options
  • Save coopermayne/b44ad8af7975f05e2408 to your computer and use it in GitHub Desktop.
Save coopermayne/b44ad8af7975f05e2408 to your computer and use it in GitHub Desktop.
NOTES
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NOTES</title>
<link rel="stylesheet" href="https://stackedit.io/res-min/themes/base.css" />
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body><div class="container"><h1 id="notes">NOTES</h1>
<h2 id="strings">STRINGS</h2>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"strings look like this."</span></code></pre>
<h3 id="index-reference">index reference</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"biilliio"</span>[<span class="hljs-number">3</span>]
<span class="hljs-string">"biilliio"</span>[-<span class="hljs-number">2</span>]</code></pre>
<h3 id="index-reference-1">index reference</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"toaster"</span>[<span class="hljs-number">3</span>] <span class="hljs-comment">#=&gt; "s"</span>
<span class="hljs-string">"zebra"</span>[-<span class="hljs-number">2</span>] <span class="hljs-comment">#=&gt; "r"</span></code></pre>
<h3 id="length">length</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"biilliio"</span>.length <span class="hljs-comment">#=&gt; "8"</span></code></pre>
<h3 id="concatenation">concatenation</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"hello "</span> + <span class="hljs-string">"world"</span> <span class="hljs-comment">#=&gt; "hello world"</span></code></pre>
<h3 id="conversion">conversion</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-string">"1001"</span>.to_i <span class="hljs-comment">#=&gt; 1001</span>
<span class="hljs-string">"1001"</span>.to_f <span class="hljs-comment">#=&gt; 1001.0</span></code></pre>
<h2 id="integers">INTEGERS</h2>
<h3 id="math">math</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-number">1</span>+<span class="hljs-number">1</span> <span class="hljs-comment">#=&gt; 2</span>
<span class="hljs-number">1</span>-<span class="hljs-number">1</span> <span class="hljs-comment">#=&gt; 0</span>
<span class="hljs-number">2</span>*<span class="hljs-number">2</span> <span class="hljs-comment">#=&gt; 4</span>
<span class="hljs-number">6</span>/<span class="hljs-number">2</span> <span class="hljs-comment">#=&gt; 3</span></code></pre>
<h3 id="conversion-1">conversion</h3>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-number">1001</span>.to_s <span class="hljs-comment">#=&gt; "1001"</span>
<span class="hljs-number">1001</span>.to_f <span class="hljs-comment">#=&gt; 1001.0</span></code></pre>
<h2 id="inputoutput">INPUT/OUTPUT</h2>
<h3 id="ouput">ouput</h3>
<p>puts is the command you use to print things to the screen.</p>
<pre class="prettyprint"><code class="language-ruby hljs ">puts <span class="hljs-string">"some string"</span></code></pre>
<h3 id="input">input</h3>
<pre class="prettyprint"><code class="language-ruby hljs ">gets </code></pre>
<p>short for <em>get string</em>. <strong>gets</strong> will open a prompt for user input. but to do anything with their response we need to save it to a variable like this:</p>
<pre class="prettyprint"><code class="language-ruby hljs ">some_variable = gets</code></pre>
<h2 id="arrays">ARRAYS</h2>
<p>an array is just a collection of stuff. the computer doesn’t care what kind of stuff you put into the array. it treats it all the same. you can put <strong>strings</strong> in there… <strong>numbers</strong>… even <strong>another array</strong>!</p>
<pre class="prettyprint"><code class="language-ruby hljs ">ex1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
ex2 = [<span class="hljs-string">"cooper"</span>, <span class="hljs-string">"sam"</span>, <span class="hljs-string">"miles"</span>, <span class="hljs-string">"noah"</span>]
ex3 = [ [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>], <span class="hljs-string">"sam"</span>, <span class="hljs-number">3</span>, [<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>] ]</code></pre>
<h3 id="length-1">length</h3>
<pre class="prettyprint"><code class="language-ruby hljs ">ex1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
ex2 = [<span class="hljs-string">"cooper"</span>, <span class="hljs-string">"sam"</span>, <span class="hljs-string">"miles"</span>, <span class="hljs-string">"noah"</span>]
ex1.length <span class="hljs-comment">#=&gt;4</span>
ex2.length <span class="hljs-comment">#=&gt;4</span></code></pre>
<h3 id="index-reference-2">index reference</h3>
<pre class="prettyprint"><code class="language-ruby hljs ">ex1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
ex2 = [<span class="hljs-string">"cooper"</span>, <span class="hljs-string">"sam"</span>, <span class="hljs-string">"miles"</span>, <span class="hljs-string">"noah"</span>]
ex1[<span class="hljs-number">2</span>] <span class="hljs-comment">#=&gt; 3</span>
ex2[<span class="hljs-number">0</span>] <span class="hljs-comment">#=&gt; "cooper"</span>
ex2[-<span class="hljs-number">1</span>] <span class="hljs-comment">#=&gt; "noah"</span>
ex2[-<span class="hljs-number">2</span>] <span class="hljs-comment">#=&gt; "miles"</span></code></pre>
<p>using negative numbers isn’t necessary. it is just a shortcut. you can also use the length method like this:</p>
<pre class="prettyprint"><code class="language-ruby hljs ">ex1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
last_index = ex1.length - <span class="hljs-number">1</span>
ex1[last_index] <span class="hljs-comment">#=&gt; 4</span></code></pre>
<h3 id="append">append</h3>
<pre class="prettyprint"><code class="language-ruby hljs ">a = [<span class="hljs-string">"i'm"</span>, <span class="hljs-string">"in"</span>, <span class="hljs-string">"an"</span>, <span class="hljs-string">"array"</span>]
a &lt;&lt; <span class="hljs-string">"array"</span>
a <span class="hljs-comment">#=&gt; ["i'm", "in", "an", "array"]</span>
a2 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
a2 &lt;&lt; <span class="hljs-number">4</span> &lt;&lt; <span class="hljs-number">5</span>
a2 <span class="hljs-comment">#=&gt; [1,2,3,4,5] </span></code></pre>
<h3 id="deleteat">delete_at</h3>
<p>this is an important method. when you want to remove an item from a collection you need to reference it by its index. see example below.</p>
<pre class="prettyprint"><code class="language-ruby hljs ">ex = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
ex.delete_at(<span class="hljs-number">2</span>)
ex <span class="hljs-comment">#=&gt; [1,2,4,5]</span></code></pre>
<p>and here’s a little trick. you can save what you delete to a variable like this:</p>
<pre class="prettyprint"><code class="language-ruby hljs ">ex = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
saved_item = ex.delete_at(<span class="hljs-number">2</span>)
ex <span class="hljs-comment">#=&gt; [1,2,4,5]</span>
saved_item <span class="hljs-comment">#=&gt; 3</span></code></pre>
<h2 id="conditionals">CONDITIONALS</h2>
<h3 id="booleans-truefalse">booleans - true/false</h3>
<p>a <strong>boolean</strong> a data type having two possible values: “true” or “false.”</p>
<p>when we are checking for equality we use two “=” signs. like this:</p>
<pre class="prettyprint"><code class="language-ruby hljs ">test_string = <span class="hljs-string">"biilliio"</span>
test_string == <span class="hljs-string">"biilliio"</span> <span class="hljs-comment">#=&gt; true</span>
test_string == <span class="hljs-string">"bilio"</span> <span class="hljs-comment">#=&gt; false</span>
test_num = <span class="hljs-number">3</span>
test_num == <span class="hljs-number">3</span> <span class="hljs-comment">#=&gt; true</span>
test_num == <span class="hljs-number">4</span> <span class="hljs-comment">#=&gt; false</span></code></pre>
<h3 id="the-big-if">the big “if”</h3>
<p>conditionals are written like this:</p>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-keyword">if</span> boolean
<span class="hljs-comment">#if boolean is true, this code will run</span>
<span class="hljs-keyword">else</span>
<span class="hljs-comment">#if boolean is false, this code will run</span>
<span class="hljs-keyword">end</span></code></pre>
<p>here are some real examples</p>
<pre class="prettyprint"><code class="language-ruby hljs "><span class="hljs-keyword">if</span> <span class="hljs-number">3</span>==<span class="hljs-number">3</span>
result = <span class="hljs-string">"three is equal to three"</span>
<span class="hljs-keyword">else</span>
result = <span class="hljs-string">"three is not equal to three"</span>
<span class="hljs-keyword">end</span>
result <span class="hljs-comment">#=&gt; "three is equal to three"</span>
name = <span class="hljs-string">"noah"</span>
<span class="hljs-keyword">if</span> name == <span class="hljs-string">"cooper"</span>
result = <span class="hljs-string">"we have the same name!"</span>
<span class="hljs-keyword">else</span>
result = <span class="hljs-string">"we have different names"</span>
<span class="hljs-keyword">end</span>
result <span class="hljs-comment">#=&gt; "we have different names"</span></code></pre>
<h2 id="loops">LOOPS</h2>
<h3 id="each">each</h3>
<p>the <strong>each</strong> method is used on arrays. it allows you to apply the same code many times while only writing it once. so if you want to double 5 numbers or 10000 numbers you wouldn’t have to change you code. </p>
<pre class="prettyprint"><code class="language-ruby hljs ">numbers = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]
numbers_doubled = []
numbers.each <span class="hljs-keyword">do</span> |number|
numbers_doubled &lt;&lt; number * <span class="hljs-number">2</span>
<span class="hljs-keyword">end</span>
numbers_doubled <span class="hljs-comment">#=&gt; [1,4,6,8]</span></code></pre>
<h3 id="times">times</h3>
<pre class="prettyprint"><code class="language-ruby hljs ">test_array = []
<span class="hljs-number">5</span>.times <span class="hljs-keyword">do</span>
test_array &lt;&lt; <span class="hljs-number">42</span>
<span class="hljs-keyword">end</span>
test_array <span class="hljs-comment">#=&gt; [42, 42, 42, 42, 42]</span></code></pre></div></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment