Skip to content

Instantly share code, notes, and snippets.

@RobertAKARobin
Created March 20, 2015 23:26
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 RobertAKARobin/d0e74f6185b2ec87b484 to your computer and use it in GitHub Desktop.
Save RobertAKARobin/d0e74f6185b2ec87b484 to your computer and use it in GitHub Desktop.
Quiz Feedback MD/HTML
<!DOCTYPE html>
<html><head><link rel="stylesheet" id="theme" href="chrome-extension://febilkbfcbhebfnokafefeacimjdckgl/theme/Clearness.css"></head><body><h1 id="w01d05-quiz-clearing-things-up">W01D05 Quiz, Clearing things up</h1>
<h2 id="implicit-returns-vs-side-effects">Implicit returns vs side effects</h2>
<p>An implicit returned is what is returned by a method that <strong>does not</strong> have a <code>return</code> statement. Remember: <strong>everything</strong> in Ruby returns <em>something</em>, and a method will return the <strong>last thing</strong> that was retured <em>inside</em> the method, <em>unless</em> the method has a <code>return</code> statement.</p>
<p>A side effect is something that takes place <strong>outside</strong> of a method as a result of that method being executed, but is <strong>not</strong> explicitly returned by the method, nor necessarily implicitly returned.</p>
<h4 id="for-example-">For example:</h4>
<pre><code><span class="function"><span class="keyword">def</span> <span class="title">toaster</span><span class="params">(tray)</span></span>
tray = tray + <span class="string">"Toasty!"</span>
puts <span class="string">"It's hot in here!"</span>
<span class="keyword">return</span> tray
<span class="keyword">end</span>
puts toaster(<span class="string">"BLT"</span>)
<span class="comment"># Returns:</span>
<span class="comment"># It's hot in here!</span>
<span class="comment"># BLTToasty!</span>
</code></pre><p><code>It's hot in here!</code> being printed out is a side effect.</p>
<pre><code><span class="function"><span class="keyword">def</span> <span class="title">toaster</span><span class="params">(tray)</span></span>
puts <span class="string">"It's hot in here!"</span>
tray = tray + <span class="string">"Toasty!"</span>
<span class="keyword">end</span>
puts toaster(<span class="string">"BLT"</span>)
<span class="comment"># Returns:</span>
<span class="comment"># It's hot in here!</span>
<span class="comment"># BLTToasty!</span>
</code></pre><p><code>It's hot in here!</code> being printed out is <em>still</em> a side effect. <code>BLTToasty!</code> is printed out even though there's no <code>return</code> statement because it's <strong>implicitly</strong> returned.</p>
<p><code>tray = tray + "Toasty!"</code> returned <code>"BLTToasty!"</code>. This was the last thing returned <em>inside</em> the method. Since there's no <code>return</code> method explicitly telling the method to return something else, the method returns <code>"BLTToasty"</code>.</p>
<hr>
<h2 id="forks-vs-branches">Forks vs branches</h2>
<p>Forking a repository makes a copy of it that <em>you</em> exclusively own. It's as if you had made the repository yourself, from scratch. You can do anything you want to your fork and it will have no impact on the repository from which it originall y came, nor any of the people contributing to that original repository. You can't be stopped from forking repositories.</p>
<p>Making a branch within a repository makes a copy of it that is owned by whoever owns the whole repository. You are still subject to their rules. The changes you make to your branch affect what is seen by everyone else contributing to that repository. The repository's owner can choose to accept or reject your changes.</p>
<h4 id="for-example-">For example:</h4>
<p>The US Bureau of Engraving and Printing makes the nation's money. They want to make some tweaks to the molds they use to stamp coins, and have hired you as a designer. You make a copy of the mold, and make some changes to your copy. If the Bureau likes your changes, they'll swap out the existing mold with your new one. This is like making a <strong>branch</strong>.</p>
<p>Unfortunately, the Bureau doesn't like your changes, and rejects them. You really like your design, though, and so decide to copy the blueprints they used to design the bureau, its factories, and machines, and create your own Bureau, using your mold. This is like making a <strong>fork</strong>.</p>
<hr>
<h2 id="variable-scope">Variable scope</h2>
<p>A method does not have access to variables <em>outside</em> the method. Something outside a method does not have access to variables <em>inside</em> it. The only way you can pass variables into a method is by explicitly passing it arguments, and the only way you can get variables out of a method is by that method returning those variables.</p>
<h4 id="for-example-">For example:</h4>
<p>In the same way, you can't put a sandwich into a Quizno's toaster if there isn't a hatch you can stick your hand into so you can put the sandwich on the tray on the conveyer belt.</p>
<p>You can't get a sandwich <em>out</em> of a Quizno's toaster if there isn't a hatch on the other end through which the sandwich can exit once it's been toasted.</p>
<p>Here are the notes from our discussion of methods and variable scope:</p>
<p><a href="https://github.com/ga-students/addbass/blob/master/w01/d05_methods/lesson_plan.md">https://github.com/ga-students/addbass/blob/master/w01/d05_methods/lesson_plan.md</a></p>
<hr>
<h2 id="truthy-vs-true-vs-falsy-vs-false">Truthy vs true vs falsy vs false</h2>
<p>Ruby considers a value to be "falsy" if the value is either <code>false</code> or <code>nil</code>. This is different from Ruby considering a value to be just "false", which means the value is explicitly <code>false</code> (and not <code>nil</code>).</p>
<p>Ruby considers a value to be "truthy" if the value is anything <em>but</em> <code>false</code> or <code>nil</code>. This is different from Ruby considering a value to be just "true", which means the value is explicitly <code>true</code>.</p>
<h4 id="for-example-">For example:</h4>
<pre><code><span class="comment"># Things that are FALSY:</span>
my_variable
<span class="comment">#Note that my_variable has no value</span>
my_variable = <span class="keyword">nil</span>
my_variable = <span class="keyword">false</span>
<span class="string">"a"</span> == <span class="string">"b"</span>
<span class="number">1</span> &gt; <span class="number">2</span>
<span class="comment"># Things that are TRUTHY:</span>
my_variable = <span class="string">""</span>
my_variable = <span class="number">0</span>
my_variable = -<span class="number">1</span>
my_variable = <span class="string">"false"</span>
<span class="comment">#Note that "false" is a string</span>
my_variable = []
my_variable = {}
my_variable = [<span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>]
my_variable
<span class="comment">#Because my_variable has been assigned a value in the previous lines</span>
<span class="comment">#You can test all these with code like this:</span>
<span class="keyword">if</span>(my_variable)
puts <span class="string">"truthy"</span>
<span class="keyword">else</span>
puts <span class="string">"falsy"</span>
<span class="keyword">end</span>
</code></pre></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment