Created
July 21, 2015 16:13
-
-
Save coopermayne/b44ad8af7975f05e2408 to your computer and use it in GitHub Desktop.
NOTES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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">#=> "s"</span> | |
<span class="hljs-string">"zebra"</span>[-<span class="hljs-number">2</span>] <span class="hljs-comment">#=> "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">#=> "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">#=> "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">#=> 1001</span> | |
<span class="hljs-string">"1001"</span>.to_f <span class="hljs-comment">#=> 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">#=> 2</span> | |
<span class="hljs-number">1</span>-<span class="hljs-number">1</span> <span class="hljs-comment">#=> 0</span> | |
<span class="hljs-number">2</span>*<span class="hljs-number">2</span> <span class="hljs-comment">#=> 4</span> | |
<span class="hljs-number">6</span>/<span class="hljs-number">2</span> <span class="hljs-comment">#=> 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">#=> "1001"</span> | |
<span class="hljs-number">1001</span>.to_f <span class="hljs-comment">#=> 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">#=>4</span> | |
ex2.length <span class="hljs-comment">#=>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">#=> 3</span> | |
ex2[<span class="hljs-number">0</span>] <span class="hljs-comment">#=> "cooper"</span> | |
ex2[-<span class="hljs-number">1</span>] <span class="hljs-comment">#=> "noah"</span> | |
ex2[-<span class="hljs-number">2</span>] <span class="hljs-comment">#=> "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">#=> 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 << <span class="hljs-string">"array"</span> | |
a <span class="hljs-comment">#=> ["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 << <span class="hljs-number">4</span> << <span class="hljs-number">5</span> | |
a2 <span class="hljs-comment">#=> [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">#=> [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">#=> [1,2,4,5]</span> | |
saved_item <span class="hljs-comment">#=> 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">#=> true</span> | |
test_string == <span class="hljs-string">"bilio"</span> <span class="hljs-comment">#=> false</span> | |
test_num = <span class="hljs-number">3</span> | |
test_num == <span class="hljs-number">3</span> <span class="hljs-comment">#=> true</span> | |
test_num == <span class="hljs-number">4</span> <span class="hljs-comment">#=> 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">#=> "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">#=> "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 << number * <span class="hljs-number">2</span> | |
<span class="hljs-keyword">end</span> | |
numbers_doubled <span class="hljs-comment">#=> [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 << <span class="hljs-number">42</span> | |
<span class="hljs-keyword">end</span> | |
test_array <span class="hljs-comment">#=> [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