<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ethan Gustafson</title>
    <description>The latest articles on DEV Community by Ethan Gustafson (@ethanmgustafson).</description>
    <link>https://dev.to/ethanmgustafson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F333505%2F2cd0d4be-c87b-438e-be1f-c017c9a5c58e.png</url>
      <title>DEV Community: Ethan Gustafson</title>
      <link>https://dev.to/ethanmgustafson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ethanmgustafson"/>
    <language>en</language>
    <item>
      <title>C - Basic Operators</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 23 Dec 2020 04:45:52 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/c-basic-operators-23d1</link>
      <guid>https://dev.to/ethanmgustafson/c-basic-operators-23d1</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Overview&lt;/li&gt;
&lt;li&gt;Prefix, Infix, and Postfix&lt;/li&gt;
&lt;li&gt;Assignment&lt;/li&gt;
&lt;li&gt;Arithmetic&lt;/li&gt;
&lt;li&gt;Logical&lt;/li&gt;
&lt;li&gt;Relational&lt;/li&gt;
&lt;li&gt;Bitwise&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;What are operators? &lt;/p&gt;

&lt;p&gt;Operators are used to manipulate data. There are many different kinds of operators, such as assignment, arithmetic, logical, relational, and bitwise operators.&lt;/p&gt;

&lt;p&gt;In C, the operators themselves are represented by symbols. For example, the addition operator would be the plus sign: &lt;code&gt;+&lt;/code&gt;. But what do these symbols represent in a program? &lt;strong&gt;Operators themselves are &lt;em&gt;functions&lt;/em&gt; that accept arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary, Unary, Prefix, Infix, and Postfix
&lt;/h3&gt;

&lt;p&gt;An operand is what an operator operates on.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In mathematics an operand is the object of a mathematical operation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Operators will use one of the three 'fix' styles above, depending on the operator used.&lt;/p&gt;

&lt;p&gt;Binary operators work with two operands. The addition &lt;code&gt;+&lt;/code&gt; function grabs the left operand and the right operand, adding them together, then returning the result.&lt;/p&gt;

&lt;p&gt;Unary operators work with one operand. The increment operator is a good example of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is the Postfix style: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// This is the Postfix style result: 10&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Now the variable number is assigned: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Now the variable number is assigned: 11&lt;/span&gt;

&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is the Prefix style result: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// This is the Prefix style result: 12&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The variable number was already assigned: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// The variable number was already assigned: 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Assignment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_assignment_operators.htm"&gt;Assignment Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assignment operators use the Infix style. The values of the right operand are assigned to the left operand. &lt;/p&gt;

&lt;p&gt;Numbers 1 - 6 are straight forward assignment operators. 7 &amp;amp; 8 are shift AND assignment operators. 9 - 11 are &lt;strong&gt;Bitwise&lt;/strong&gt; assignment operators. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;=&lt;/code&gt; - Assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+=&lt;/code&gt; - Add AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-=&lt;/code&gt; - Subtract AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*=&lt;/code&gt; - Multiply AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/=&lt;/code&gt; - Division AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%=&lt;/code&gt; - Modulus AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&amp;lt;=&lt;/code&gt; - Left shift AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; - Right shift AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;=&lt;/code&gt; - Bitwise AND assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;^=&lt;/code&gt; - Bitwise exclusive OR and assignment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|=&lt;/code&gt; - Bitwise inclusive OR and assignment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You would typically use these operators to store calculated values into variables without writing out the calculation yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arithmetic
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm"&gt;Arithmetic Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the arithmetic operators use the Infix style to perform calculations on two operands. Numbers 6 and 7 can use either the Prefix or Postfix style.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; - Addition&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt; - Subtraction&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; - Multiplication&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\&lt;/code&gt; - Division&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt; - Modulus&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;++&lt;/code&gt; - Increment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--&lt;/code&gt; - Decrement
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is the difference in the increment/decrements 'fix' style? Running the operator before the variable &lt;code&gt;num&lt;/code&gt; ensures the value incremented/decremented is assigned and returned right away.&lt;/p&gt;

&lt;p&gt;Running the increment/decrement operator &lt;strong&gt;after&lt;/strong&gt; the variable &lt;code&gt;num&lt;/code&gt; will return the original value of the variable, and then assign the new value. &lt;/p&gt;

&lt;h2&gt;
  
  
  Logical
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_logical_operators.htm"&gt;Logical Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Logical operators use the Infix style, except number 3, which uses the Prefix style. These are boolean operators that return a &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; value. They evaluate the two operands and run them against each other to test equality in some way.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; - Logical AND (Returns true if both operands are true )&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;||&lt;/code&gt; - Logical OR (Returns true if only one operand is true)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!&lt;/code&gt; - Logical NOT (Returns the inverse logical state of the operand it is being evoked on) &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Relational
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_relational_operators.htm"&gt;Relational Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Relational operators also use the Infix style. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt; - if both operands are equal, return true.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!=&lt;/code&gt; - if the left operand is not equal to the right operand, return true&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; - if the left operand is greater than the right operand, return true&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&lt;/code&gt; - if the left operand is less than the right operand, return true&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;=&lt;/code&gt; - if the left operand is greater than or equal to the right operand, return true&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;=&lt;/code&gt; - if the left operand is less than or equal to the right operand, return true&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Bitwise
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm"&gt;Bitwise Operators&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bitwise operators are a bit different. These operators operate on the bits in integer values. They're also different from Logical operators, as Logical operators will use two of these symbols whereas Bitwise will only use one.&lt;/p&gt;

&lt;p&gt;They all use the Infix style, except for number 4. Number 4 uses the Postfix style.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;&amp;amp;&lt;/code&gt; - Binary AND&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|&lt;/code&gt; - Binary OR&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; - Binary XOR&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;~&lt;/code&gt; - Binary One's Complement&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; - Binary Left Shift&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; - Binary Right Shift&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I will go more in-depth on bitwise operators, operator precedence, plus other operators like cast and &lt;code&gt;sizeof&lt;/code&gt; in the next few entries of this series. Thank you for reading!&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>What is the Clear Definition of an API?</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 16 Dec 2020 04:52:31 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/what-is-the-clear-definition-of-an-api-gmn</link>
      <guid>https://dev.to/ethanmgustafson/what-is-the-clear-definition-of-an-api-gmn</guid>
      <description>&lt;p&gt;When you search for the definition of an API online, there will be some very ambiguous results. I found myself and many others a bit confused when it came to the explanations provided.&lt;/p&gt;

&lt;p&gt;I will explain a little bit of how various entities use the term API below but if you would just like the straight answer, jump &lt;strong&gt;here&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an API?
&lt;/h2&gt;

&lt;p&gt;As an overview, I was taught that an API is a way for one system to communicate with other external systems. Let's define more of what an API is.&lt;/p&gt;

&lt;p&gt;The Wikipedia definition of an API is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An &lt;em&gt;Application Programming Interface&lt;/em&gt; (API) is a computing interface that defines interactions between multiple software intermediaries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;a href="https://en.wikipedia.org/wiki/Application_software"&gt;&lt;em&gt;Application&lt;/em&gt;&lt;/a&gt; is a program or a group of programs designed for an end-user (a user that is intended to use the application)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Programming&lt;/em&gt; is the process of creating a set of instructions that tell a computer how to perform a task.&lt;/li&gt;
&lt;li&gt;An &lt;a href="https://en.wikipedia.org/wiki/Interface_(computing)"&gt;&lt;em&gt;Interface&lt;/em&gt;&lt;/a&gt; is a shared boundary across which two or more separate components of a computer system exchange information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You've probably seen various entities use the term API. Let's see some examples:&lt;/p&gt;

&lt;p&gt;1.) &lt;a href="https://reactjs.org/docs/react-api.html"&gt;React.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the very top of the page:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React is the entry point to the React library. If you load React from a script tag, these top-level APIs are available on the React global. If you use ES6 with npm, you can write import React from 'react'. If you use ES5 with npm, you can write var React = require('react').&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2.) &lt;a href="https://github.com/rails/rails"&gt;Ruby on Rails API&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ruby on Rails is a web-application framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.&lt;/p&gt;

&lt;p&gt;When you navigate to the GitHub link above for the Ruby on Rails repo and scroll down to the bottom, you'll see a link for the Ruby on Rails &lt;a href="https://api.rubyonrails.org/"&gt;API Documentation&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;API documentation is a technical content deliverable, containing instructions about how to effectively use and integrate with an API&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;3.) &lt;a href="https://graphql.org/learn/"&gt;Graphql&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4.) &lt;a href="https://en.wikipedia.org/wiki/Google_APIs"&gt;Google APIs&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Google APIs are application programming interfaces developed by Google which allow communication with Google Services and their integration to other services.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;5.) &lt;a href="https://developer.twitter.com/en/docs"&gt;The Twitter API&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Twitter API enables programmatic access to Twitter in unique and advanced ways. Use it to analyze, learn from, and interact with Tweets, Direct Messages, users, and other key Twitter resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;6.) &lt;a href="https://guides.rubyonrails.org/api_app.html"&gt;Using Ruby on Rails as an API&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Traditionally, when people said that they used Rails as an "API", they meant providing a programmatically accessible API alongside their web application.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;strong&gt;In all of these cases, one entity interacted and outputted a result to a separate entity.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All the definitions and examples above still didn't give a straight forward answer as to what an API actually &lt;em&gt;is&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;The definition of an &lt;em&gt;interface&lt;/em&gt; is a shared boundary across which two or more separate components of a computer system exchange information. Let's use React as an example. &lt;/p&gt;

&lt;p&gt;What is React.js? A JavaScript library for building user interfaces.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A library is a collection of programs and software packages made generally available, often loaded, and stored on disk for immediate use.&lt;/p&gt;

&lt;p&gt;A user interface is the space where interactions between humans and machines occur.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;graphical user interface&lt;/em&gt; (GUI) is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicators such as primary notation, instead of text-based user interfaces, typed command labels, or text navigation.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;command-line interface&lt;/em&gt; (CLI) processes commands to a computer program in the form of lines of text.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Essentially, React is used to create a way in which users can interact with the browser and/or other frameworks, such as Gatsby.js.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A web application (or web app) is application software that runs on a web server, unlike computer-based software programs that are run locally on the operating system (OS) of the device.&lt;/p&gt;

&lt;p&gt;Web applications are accessed by the user through a web browser with an active internet connection.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Straight Forward API Definition
&lt;/h3&gt;

&lt;p&gt;So what is the clear-cut definition of an API?&lt;/p&gt;

&lt;p&gt;The Application Programming Interface is a programmatic interface. A programmatic interface is literally the code utilized in interacting with another program that has its own special methods, functions, and syntax.&lt;/p&gt;

&lt;p&gt;For example, let's write a class component with React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;myComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="nx"&gt;Header&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React uses a declarative programming style. This means you programmatically declare what the program should do, &lt;em&gt;but&lt;/em&gt; &lt;strong&gt;you don't explicitly define how the program will do it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The React functions, methods, syntax, and rules for a component is the interface we utilized in order to interact with the browser. &lt;strong&gt;&lt;code&gt;Component&lt;/code&gt; doesn't show us what it does under the hood&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The API of React is its syntax and features. &lt;strong&gt;You will use its syntax and features to interact with another program&lt;/strong&gt;, typically the browser.&lt;/p&gt;




&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>javascript</category>
      <category>react</category>
      <category>graphql</category>
    </item>
    <item>
      <title>C - Intro To Data Types &amp; Variables</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 09 Dec 2020 03:13:14 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/c-intro-to-data-types-variables-3gj3</link>
      <guid>https://dev.to/ethanmgustafson/c-intro-to-data-types-variables-3gj3</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;RAM&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variables and Constants&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Declaration and Initialization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Types&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Primitive&lt;/li&gt;
&lt;li&gt;int&lt;/li&gt;
&lt;li&gt;float&lt;/li&gt;
&lt;li&gt;double&lt;/li&gt;
&lt;li&gt;_Bool, bool&lt;/li&gt;
&lt;li&gt;enum&lt;/li&gt;
&lt;li&gt;char&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other Type Specifiers&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;short&lt;/li&gt;
&lt;li&gt;long&lt;/li&gt;
&lt;li&gt;long long&lt;/li&gt;
&lt;li&gt;signed&lt;/li&gt;
&lt;li&gt;unsigned&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Final Note&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;I was really surprised when I began learning about variables and data types in C. After understanding what happens under the hood for those languages, I began seeing how this language influences and shapes them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory
&lt;/h2&gt;

&lt;p&gt;Programs need to store the memory it allocates upon runtime. In C, you are allowed to make your program as efficient as possible with how much memory you wish to allocate. Computer memory is stored in binary, 1's, and 0's. Bits are grouped together in sets of 8. A set of 8 bits is a byte.&lt;/p&gt;

&lt;p&gt;Now, how does the computer know where to find the memory it allocates? Each set of bytes are given a unique address. A label to let the computer know where it is. The address of the byte is uniquely referenced in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  RAM
&lt;/h3&gt;

&lt;p&gt;But where is the memory stored? In the hard drive and RAM (Random-Access-Memory). Persistent data is stored in the hard drive, while memory in RAM is temporary. It clears out when the computer shuts down. The more RAM and the speed of that RAM you have, the more efficient your program is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables and Constants
&lt;/h2&gt;

&lt;p&gt;One of the ways your program will allocate memory is with variables and constants. Variable and Constant names become identifiers that represent the unique address of values stored in RAM by your program. Your program uses these names to find the unique address of values in RAM.&lt;/p&gt;

&lt;p&gt;A Constant is a type of data that doesn't change. They keep their values through the lifecycle of a program.&lt;/p&gt;

&lt;p&gt;A Variable is a type of data that may be modified or assigned values through the lifecycle of a program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaration and Initialization
&lt;/h3&gt;

&lt;p&gt;If you are familiar with JavaScript, there is a concept of declaring and initializing variables. JavaScript is written in C++. You can learn what declaring and initializing actually means in C.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you declare a variable in JavaScript&lt;/strong&gt;, you specify which type of variable it is, followed by the variable identifier(name).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you type the above into the console of the web browser, you'll see that the variable is initialized with a value of &lt;code&gt;undefined&lt;/code&gt;. The variable was declared, but it wasn't initialized with a specified value. JavaScript did that for you, with &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initializing literally means assigning a variable an initial value.&lt;/strong&gt; When you first assign a value to a variable, it becomes initialized.&lt;/p&gt;

&lt;p&gt;In JavaScript, you would commonly specify the type of variable with &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;const&lt;/code&gt;. In C, you &lt;strong&gt;must&lt;/strong&gt; specify the data type of the variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you declare a variable in C without initializing it, you will encounter compilation errors. C reads code top-to-bottom. Variables cannot be referenced before they are declared. It is better to declare and initialize your variables in one step.&lt;/p&gt;

&lt;p&gt;If the compiler sees that a declared variable is referenced later without a value, it will return a compilation error. Variable names in C &lt;strong&gt;&lt;em&gt;must&lt;/em&gt;&lt;/strong&gt; begin with a letter or underscore in order to be valid.&lt;/p&gt;

&lt;p&gt;You can also declare and/or initialize many variables of the same data type on one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ethanAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numOfBlogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;randomNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;350&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a better practice to not mix declared and uninitialized variables on one line. Either write straight declared or initialized variables only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ethanAge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numOfBlogs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;randomNum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Data Types
&lt;/h2&gt;

&lt;p&gt;There are many data types in C, as well as many variations of those data types. Variations of data types allow for the accuracy of use cases pertaining to memory efficiency. Data types let the compiler know just how much memory to allocate.&lt;/p&gt;

&lt;p&gt;The amount of memory that is allocated is dependent on the type of computer you use. A data type may take up more or fewer bits on your computer.&lt;/p&gt;

&lt;p&gt;This list will show you C data types and how much memory is allocated for each: &lt;a href="https://www.tutorialspoint.com/cprogramming/c_data_types.htm"&gt;C - Data Types&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Primitive
&lt;/h3&gt;

&lt;p&gt;Primitive data types are not objects. Everything in C is primitive because C isn't object-oriented. They can store a great variety of data.&lt;/p&gt;

&lt;p&gt;You are able to specify the type of variable with a reserved keyword. These keywords are predefined by C. These keywords are called &lt;strong&gt;Type Specifiers&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;int&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;int&lt;/code&gt; is a reserved keyword (type specifier) for the integer data type. It will only store negative, positive, or zero integers. It will not store numbers with decimal places. No commas or spaces are allowed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;int&lt;/code&gt; allows you to assign numbers and hexadecimal numbers. &lt;a href="https://en.wikipedia.org/wiki/Hexadecimal"&gt;Hexadecimal numbers&lt;/a&gt; are Base-16:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xf5d7ad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hex number: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outputs: Hexa number: 16111533&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler will know the value is hexadecimal by the initial &lt;code&gt;0x&lt;/code&gt;, followed by the actual hex number.&lt;/p&gt;

&lt;p&gt;The amount of bytes the &lt;code&gt;int&lt;/code&gt; data type takes up is usually 2 or 4 bytes. Its number range is -32,768 to 32,767 &lt;strong&gt;or&lt;/strong&gt; -2,147,483,648 to 2,147,483,647.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;float&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;float&lt;/code&gt; data type is reserved for decimal point numbers. These are called 'floating-point' numbers, which are just numbers containing decimal places.&lt;/p&gt;

&lt;p&gt;They usually take up 4 bytes. Its value range is 1.2E-38 to 3.4E+38, and its precision is 6 decimal places.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;myFloat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;227233&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of myFloat: %.2f&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myFloat&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outputs: The value of myFloat: 120.23&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;double&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;double&lt;/code&gt; data type is the same as &lt;code&gt;float&lt;/code&gt;, except you are allowed to store much larger numbers within it. It takes up 8 bytes, has a number range of 2.3E-308 to 1.7E+308, and a precision of 19 decimal places.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;myDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;223456789&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of myDouble: %lf&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myDouble&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outputs: The value of myDouble: 2.22345679&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;_Bool&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;_Bool&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt; data type is reserved for two boolean values only: True and False. A &lt;code&gt;0&lt;/code&gt; is false, and a &lt;code&gt;1&lt;/code&gt; is true.&lt;/p&gt;

&lt;p&gt;The difference between &lt;code&gt;_Bool&lt;/code&gt; and &lt;code&gt;bool&lt;/code&gt; is syntax. With &lt;code&gt;_Bool&lt;/code&gt;, you can only assign the value of a 1 or 0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;myBoolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
  &lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;secondBoolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;bool&lt;/code&gt; allows you to actually assign the variable values of &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;. In order to use it in your program, you have to include the &lt;code&gt;&amp;lt;stdbool.h&amp;gt;&lt;/code&gt; header file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdbool.h&amp;gt;&lt;/span&gt;&lt;span class="c1"&gt; &lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;myBoolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of the myBoolean variable is: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myBoolean&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outputs: The value of the myBoolean variable is: 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;enum&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;enum&lt;/code&gt; data type allows you to define your own data type for a variable. The data it can contain is defined by you. These values will be the only ones permitted within the &lt;code&gt;enum&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To create an &lt;code&gt;enum&lt;/code&gt;, type the keyword &lt;code&gt;enum&lt;/code&gt;, followed by the identifier name, followed by curly braces.&lt;/p&gt;

&lt;p&gt;You can set the values within the curly braces, separated by commas, with identifier names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Sports&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Soccer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Baseball&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Swimming&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tennis&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create more variables of the same &lt;code&gt;enum&lt;/code&gt; data type, write the &lt;code&gt;enum&lt;/code&gt; keyword followed by the same identifier name, then assign it to one of the values in the list.&lt;/p&gt;

&lt;p&gt;The items in the curly braces are indexed, starting at zero. &lt;code&gt;Soccer&lt;/code&gt; would have an index of &lt;code&gt;0&lt;/code&gt;. Printing out the results of the &lt;code&gt;enum&lt;/code&gt; would return the number of the item in the &lt;code&gt;enum&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Sports&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;SOCCER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BASEBALL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SWIMMING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TENNIS&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Sports&lt;/span&gt; &lt;span class="n"&gt;soccer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SOCCER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;baseball&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BASEBALL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\soccer value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;soccer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\b&lt;/span&gt;&lt;span class="s"&gt;aseball value: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;baseball&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;char&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;char&lt;/code&gt; data type is reserved for a single character. It is &lt;strong&gt;different&lt;/strong&gt; from a character string. Strings in C strictly use double quotes. Characters &lt;strong&gt;strictly&lt;/strong&gt; use single quotes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;letterOfMyFirstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is the letter of my first name? %c&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;letterOfMyFirstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other Type Specifiers
&lt;/h2&gt;

&lt;p&gt;There are three keywords that allow you to modify the amount of memory allocated to type &lt;code&gt;int&lt;/code&gt; and in one case, a &lt;code&gt;double&lt;/code&gt;: &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;, and &lt;code&gt;unsigned&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;short&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;short&lt;/code&gt; specifier is used to decrease the number of bytes the &lt;code&gt;int&lt;/code&gt; type will take up. It will ensure the &lt;code&gt;int&lt;/code&gt; only takes up 2 bytes. It can either be used by itself, or with the &lt;code&gt;int&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;myShortInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondShortInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"myShortInteger: %d, secondShortInteger: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myShortInteger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondShortInteger&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// outputs: myShortInteger: 10, secondShortInteger: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;long&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;long&lt;/code&gt; specifier is used to increase the number of bytes a variable of type &lt;code&gt;int&lt;/code&gt; or &lt;code&gt;double&lt;/code&gt; will take up. For &lt;code&gt;int&lt;/code&gt;, it will take 8 bytes or (4bytes for 32 bit OS). For &lt;code&gt;double&lt;/code&gt;, it will take 10 bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;longerInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9223372036854&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;longerDouble&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9406564584124654&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"longerInt: %ld, longerDouble: %Lf&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longerInt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longerDouble&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// outputs: longerInt: 9223372036854, longerDouble: 4.940656&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;long long&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;long&lt;/code&gt; specifier is used to almost doubly increase the number of bytes a variable will take up. It takes up 8 bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;longerLongerInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9223372036854775807&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"longerLongerInt: %lli&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longerLongerInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// outputs: longerLongerInt: 9223372036854775807&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;signed&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;signed&lt;/code&gt; specifier isn't necessary to use, but it can make your semantic meaning of a variable to be explicit. It specifies that a variable will have a value of zero, positive or negative number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;signed&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;negativeNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;signed&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;smallerInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/* is the same as:

  int negativeNum = -10;
  short int smallerInt = 4;

  */&lt;/span&gt; 

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"negativeNum: %d, smallerInt: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;negativeNum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;smallerInt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// outputs: negativeNum: -10, smallerInt: 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;unsigned&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;unsigned&lt;/code&gt; specifier specifies that the variable will &lt;strong&gt;only&lt;/strong&gt; contain positive values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;positiveShortNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"positiveShortNumber: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;positiveShortNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// outputs: positiveShortNumber: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Note: Format Specifiers and the &lt;code&gt;printf()&lt;/code&gt; Function
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;printf()&lt;/code&gt; statement above in all the examples will output text to standard out (&lt;code&gt;stdout&lt;/code&gt;). In order to use variable arguments to the &lt;code&gt;printf()&lt;/code&gt; function, you must translate what the data type of the variable arguments are.&lt;/p&gt;

&lt;p&gt;Format specifiers will be used to display variables as output. They begin with the percent symbol (&lt;code&gt;%&lt;/code&gt;), followed by an argument specifying a data type. &lt;code&gt;%d&lt;/code&gt; specifies that a variable will be of type &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Read more on the &lt;code&gt;printf()&lt;/code&gt; function, data types, escape characters, format specifiers and the standard input/output stream below. Thank you for reading!&lt;/p&gt;

&lt;p&gt;Resources: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_data_types.htm"&gt;C - Data Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/C_data_types"&gt;Wiki C Data Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Escape_sequences_in_C"&gt;Escape Characters(sequences)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/format-specifiers-in-c"&gt;Format Specifiers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm"&gt;printf() function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_input_output.htm"&gt;C Standard in and Standard out&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>javascript</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>C - Program Structure</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 02 Dec 2020 04:05:36 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/c-program-structure-1l5b</link>
      <guid>https://dev.to/ethanmgustafson/c-program-structure-1l5b</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Recap&lt;/li&gt;
&lt;li&gt;
Program Structure 

&lt;ul&gt;
&lt;li&gt;Case Sensitivitiy&lt;/li&gt;
&lt;li&gt;Semicolons&lt;/li&gt;
&lt;li&gt;Indentation&lt;/li&gt;
&lt;li&gt;Comments&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;main&lt;/code&gt; Function&lt;/li&gt;
&lt;li&gt;&lt;code&gt;int&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The Preprocesser&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;In the last blog, we configured our environment to use the C language. We had to use a simple code example in order to run a program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we'll learn a bit about the basic program structure in C.&lt;/p&gt;

&lt;h2&gt;
  
  
  Program Structure
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Case Sensitivity
&lt;/h3&gt;

&lt;p&gt;Everything in C is case sensitive. Lowercase and uppercase code matters here. If something is out of place, there will be compilation errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Semicolons
&lt;/h3&gt;

&lt;p&gt;Every statement in C ends with a semicolon. Statements are lines of code within the code block. The code block is set by curly braces &lt;code&gt;{}&lt;/code&gt;. The statements are within the code block above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those are required to end with a semicolon, otherwise, there will be compilation errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Indentation
&lt;/h3&gt;

&lt;p&gt;Although the code above doesn't need to be indented in any way, it is written to be readable. Readable code is easy to understand. When you don't write readable code, it will be difficult to follow later on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comments
&lt;/h3&gt;

&lt;p&gt;Comments will be ignored by the compiler. They won't produce errors. There are two ways to write comments, multi-line and single-line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="cm"&gt;/* Multi-line */&lt;/span&gt;
    &lt;span class="c1"&gt;// Single-line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The &lt;code&gt;main&lt;/code&gt; Function
&lt;/h3&gt;

&lt;p&gt;In the example above, &lt;code&gt;main&lt;/code&gt; is a function. The parenthesis after the name &lt;code&gt;main()&lt;/code&gt; denotes that it is a function. The &lt;code&gt;main&lt;/code&gt; function is special, as it is the first function that runs upon program execution. You are only allowed to write one &lt;code&gt;main&lt;/code&gt; function. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;int&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you're wondering what the &lt;code&gt;int&lt;/code&gt; is before the &lt;code&gt;main()&lt;/code&gt; function, that is used to define what the function will return. It is a return type. There are other ones besides &lt;code&gt;int&lt;/code&gt;, but &lt;code&gt;int&lt;/code&gt; defines &lt;code&gt;main()&lt;/code&gt; as a function that will return an integer.&lt;/p&gt;

&lt;p&gt;The function above returned zero. That is an &lt;a href="https://www.geeksforgeeks.org/exit-codes-in-c-c-with-examples/"&gt;exit code&lt;/a&gt;. Zero is the exit code for &lt;code&gt;EXIT_SUCCESS&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html"&gt;Exit Status&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Preprocessor
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/C_preprocessor"&gt;C Preprocessor&lt;/a&gt; is an instruction sent to the compiler before the actual compilation takes place. It will analyze special statements in your code before the program runs.&lt;/p&gt;

&lt;p&gt;They are usually defined at the top of a code file but can actually be placed anywhere in the code. Preprocessor statements begin with a pound sign &lt;code&gt;#&lt;/code&gt; and follow with a directive. There are many different directives.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;#include&lt;/code&gt; directive was used above in the code example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;#include&lt;/code&gt; statement is a preprocessor directive that allows you to include code from other locations. It means that the code you're writing is dependent on code from another file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stdio.h&lt;/code&gt; is a file name. This is a &lt;em&gt;header&lt;/em&gt; file. The &lt;code&gt;#include&lt;/code&gt; directive is going to include filenames ending with a &lt;code&gt;.h&lt;/code&gt; extension. The &lt;a href="https://www.tutorialspoint.com/c_standard_library/stdio_h.htm"&gt;&lt;code&gt;&amp;lt;stdio.h&amp;gt;&lt;/code&gt;&lt;/a&gt; header file is a part of the C Standard Library. It stands for standard input-output. It provides functionality for displaying input and output.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;stdio.h&amp;gt;&lt;/code&gt; allows us to use the &lt;a href="https://en.wikibooks.org/wiki/C_Programming/stdio.h/printf"&gt;&lt;code&gt;printf&lt;/code&gt;&lt;/a&gt; function. Without &lt;code&gt;&amp;lt;stdio.h&amp;gt;&lt;/code&gt;, the program wouldn't compile.&lt;/p&gt;

&lt;p&gt;You can also write the name of the file in two ways:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1.) #include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 2.) #include "stdio.h"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Big O Notation, Time &amp; Space Complexity Overview</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 25 Nov 2020 03:02:43 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/big-o-notation-time-space-complexity-44l9</link>
      <guid>https://dev.to/ethanmgustafson/big-o-notation-time-space-complexity-44l9</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Big O Notation&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Runtime&lt;/li&gt;
&lt;li&gt;Time Complexity&lt;/li&gt;
&lt;li&gt;Space Complexity&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notations&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Linear: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Constant: &lt;code&gt;O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Quadratic: &lt;code&gt;O(n^2)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Factorial: &lt;code&gt;O(n!)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Logarithmic: &lt;code&gt;O(log N)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Log Linear: &lt;code&gt;O(n log(n))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Exponential: &lt;code&gt;O(2^n)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Big O Cheatsheet&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Big O Notation
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Big_O_notation"&gt;&lt;strong&gt;Big O Notation&lt;/strong&gt;&lt;/a&gt; is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It will completely change how you write code. It is used to help make code readable and scalable. &lt;/p&gt;

&lt;p&gt;Readable code is maintainable code. It is easy to read and contains meaningful names of variables, functions, etc.&lt;/p&gt;

&lt;p&gt;Scalable code refers to speed and memory. The reason code needs to be scalable is because we don't know how many users will use our code. We have to be able to determine solutions for algorithms that weigh in on the costs of speed and memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big O Notation will be used in two ways&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To classify the time complexity(speed) of an algorithm.&lt;/li&gt;
&lt;li&gt;To classify the space complexity(memory) of an algorithm.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Runtime
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer science, &lt;a href="https://en.wikipedia.org/wiki/Runtime_(program_lifecycle_phase)"&gt;&lt;strong&gt;runtime&lt;/strong&gt;&lt;/a&gt;, run time, or execution time is the final phase of a computer program's life cycle, in which the code is being executed on the computer's central processing unit (CPU) as machine code. In other words, "runtime" is the running phase of a program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Essentially, the runtime is the period of time when an algorithm is running. This is an important term to know for later on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Time_complexity"&gt;&lt;strong&gt;time complexity&lt;/strong&gt;&lt;/a&gt; is the computational complexity that describes the amount of time it takes to run an algorithm.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many pros and cons to consider when classifying the time complexity of an algorithm: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What's the worst-case scenario?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The worst-case scenario will be considered first, as it is difficult to determine the average or best-case scenario.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What data structure should you use?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some notations are used specifically for certain data structures. I will show you down below in the Notations section. There is also a Big O Cheatsheet further down that will show you what notations work better with certain structures.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Which one would be a better solution over another?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which structure has a time-efficient notation? A more memory-efficient notation?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;You'll have to trade-off between the pros and cons of space and time.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Big O is used to determine the time and space complexity of an algorithm. There may be solutions that are better in speed, but not in memory, and vice versa. Just depends on which route is advocated for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Space Complexity
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Space_complexity"&gt;&lt;strong&gt;space complexity&lt;/strong&gt;&lt;/a&gt; of an algorithm or a computer program is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Space complexity is caused by variables, data structures, allocations, etc. What you create takes up space. Space complexity is determined the same way Big O determines time complexity, with the notations below, although this blog doesn't go in-depth on calculating space complexity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Notations
&lt;/h2&gt;

&lt;p&gt;The order of the notations is set from best to worst:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Constant&lt;/strong&gt;: &lt;code&gt;O(1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logarithmic&lt;/strong&gt;: &lt;code&gt;O(log N)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linear&lt;/strong&gt;: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log Linear&lt;/strong&gt;: &lt;code&gt;O(n log(n))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quadratic&lt;/strong&gt;: &lt;code&gt;O(n^2)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponential&lt;/strong&gt;: &lt;code&gt;O(2^n)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Factorial&lt;/strong&gt;: &lt;code&gt;O(n!)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this blog, I will only cover constant, linear, and quadratic notations. The other notations will include a description with references to certain data structures and algorithms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constant: &lt;code&gt;O(1)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Constant Notation is excellent. In terms of speed, the runtime of the function is always the same. If the input increases, the function will still output the same result at the same amount of time.&lt;/p&gt;

&lt;p&gt;Let's say we had an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;D&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Array_data_structure"&gt;Array&lt;/a&gt; is an ordered data structure containing a collection of elements.&lt;/p&gt;

&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Associative_array"&gt;Associative Array&lt;/a&gt; is an unordered data structure consisting of key-value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;associativeArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ethan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When accessing an element of either one of these data structures, the Big O will always be constant time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="c1"&gt;// =&amp;gt; C&lt;/span&gt;

  &lt;span class="nx"&gt;associativeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;
  &lt;span class="c1"&gt;// =&amp;gt; hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because neither element had to be searched for. The location of the element was known by its index or identifier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logarithmic: &lt;code&gt;O(log N)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Binary_search_tree"&gt;Binary Search Tree&lt;/a&gt; would use the Logarithmic Notation. A &lt;a href="https://en.wikipedia.org/wiki/Binary_tree"&gt;Binary Tree&lt;/a&gt; is a tree data structure consisting of nodes that contain two children max.&lt;/p&gt;

&lt;p&gt;In a Binary Search Tree, there are no duplicates. The left subtree of a node contains children nodes with a key value that is less than their parental node value. The right subtree is the opposite, where children nodes have values greater than their parental node value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear: &lt;code&gt;O(n)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;As the input increases, the amount of time needed to complete the function increases. The runtime grows as the input size increases. Also, the &lt;code&gt;n&lt;/code&gt; can be anything. An &lt;code&gt;x&lt;/code&gt;, an &lt;code&gt;o&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;An example of &lt;code&gt;O(n)&lt;/code&gt; would be a loop on an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;group&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jolene&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ethan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ali&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;logan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bob&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;findEthan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ethan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I found him!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;findEthan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The input size of the function can dramatically increase. What if there were 500 people in the crowd? The function would take longer to execute, especially if my name is the very last item in the array.&lt;/p&gt;

&lt;p&gt;Can you imagine having an input way higher? Let's say 10,000? The length of time it takes to execute the algorithm is dependent on the size of the input. As the size increases, the length increases. This is Linear Notation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quadratic: &lt;code&gt;O(n^2)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Quadratic Notation is Linear Notation, but with one nested loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// console.log('')&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't know the size of the input, and there are two &lt;code&gt;for&lt;/code&gt; loops with one nested into the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Log Linear: &lt;code&gt;O(n log(n))&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Quicksort"&gt;Quicksort algorithm&lt;/a&gt; has the best time complexity with Log-Linear Notation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exponential: &lt;code&gt;O(2^n)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;There are not many examples online of real-world use of the Exponential Notation. &lt;/p&gt;

&lt;h3&gt;
  
  
  Factorial: &lt;code&gt;O(n!)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This Notation is the absolute worst one. When you have a nested loop for every input you possess, the notation is determined as Factorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Big O Cheatsheet
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.bigocheatsheet.com/"&gt;Big O Cheatsheet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cheatsheet shows the space complexities of a list consisting of data structures and algorithms.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Rails 6 ActionCable Navigation &amp; Turbolinks</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Tue, 03 Nov 2020 12:26:25 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/rails-6-actioncable-navigation-turbolinks-2k67</link>
      <guid>https://dev.to/ethanmgustafson/rails-6-actioncable-navigation-turbolinks-2k67</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents:
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
ActionCable LifeCycle

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;connection.rb&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#connect&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#disconnect&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;code&gt;channel.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;comments_channel.rb&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#subscribed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#receive(data)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#unsubscribed&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;javascript/channels/comments_channel.js&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#connected()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#received(data)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#appendComment(data)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Views

&lt;ul&gt;
&lt;li&gt;JavaScript TurboLinks&lt;/li&gt;
&lt;li&gt;&lt;code&gt;link_to&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Visiting a URL&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is part two of navigating ActionCable. In the last blog, I configured ActionCable connections and channels. Streams could start, on the condition that there is a project &lt;code&gt;param&lt;/code&gt; &lt;code&gt;id&lt;/code&gt; in the URL.&lt;/p&gt;

&lt;p&gt;But there was a big issue: Streams &lt;strong&gt;wouldn't&lt;/strong&gt; start unless a user purposefully reloaded the page on a &lt;code&gt;projects#show&lt;/code&gt; route. Users should be able to visit that route and have the stream start immediately.&lt;/p&gt;

&lt;p&gt;What's going on? A stream must start based on whether or not it found a project instance. No &lt;code&gt;Project.find_by_id&lt;/code&gt; method was called between page visits. Page visits didn't send requests to the server.&lt;/p&gt;

&lt;p&gt;When are ActionCable methods called, and how can we make sure that those methods run &lt;strong&gt;when we need them to&lt;/strong&gt;? &lt;/p&gt;

&lt;h2&gt;
  
  
  ActionCable LifeCycle
&lt;/h2&gt;

&lt;p&gt;When a page loads, that is when ActionCable begins calling its methods. A request is sent to the server. A page-load is &lt;strong&gt;different&lt;/strong&gt; than a page visit. &lt;/p&gt;

&lt;p&gt;A page visit is when a user visits a link and no page load happens. The navigated page appears in the DOM, but the entire page didn't load from scratch. This is what a single page application does. &lt;/p&gt;

&lt;p&gt;Rails uses &lt;a href="https://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks"&gt;JavaScript Turbolinks&lt;/a&gt;. Turbolinks allow a Rails application to perform as a single page application without the client-side JavaScript framework. Because of this, ActionCable methods will not run when we need them to. To get past that, we can turn off Turbolinks or purposely trigger page-loads. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;connection.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When a user opens their browser and navigates to the website, that is when the server will start firing off Action Cable methods. There are two main methods in it: &lt;code&gt;connect&lt;/code&gt; and &lt;code&gt;disconnect&lt;/code&gt;. A private third method is used to find the &lt;code&gt;current_user&lt;/code&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;connect&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is where the Connection current user is set. This connection becomes the parent to all channel subscriptions the current user subscribes to. When a user navigates to the website, ActionCable will begin the process of creating the connection between client and server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/channels/application_cable/connection.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;
  &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_verified_user&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I am using &lt;code&gt;devise&lt;/code&gt;, I'm finding the current user through &lt;code&gt;warden&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/channels/application_cable/connection.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_verified_user&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;verified_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'warden'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;
    &lt;span class="n"&gt;verified_user&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="c1"&gt;# You can find the reject_unauthorized_connection method&lt;/span&gt;
  &lt;span class="c1"&gt;# here -&amp;gt; https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/connection/authorization.rb&lt;/span&gt;
    &lt;span class="n"&gt;reject_unauthorized_connection&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;disconnect&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In this method, you would do any cleanup work when the connection is cut.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/channels/application_cable/connection.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;disconnect&lt;/span&gt;
  &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;reason: &lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;reconnect: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;close&lt;/code&gt; method can be found here in the &lt;a href="https://github.com/rails/rails/blob/fe76a95b0d252a2d7c25e69498b720c96b243ea2/actioncable/lib/action_cable/connection/base.rb#L98"&gt;repo&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# rails/actioncable/lib/action_cable/connection/base.rb&lt;/span&gt;

&lt;span class="c1"&gt;# Close the WebSocket connection.&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;reason: &lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;reconnect: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;transmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="no"&gt;ActionCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;INTERNAL&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:message_types&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:disconnect&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="ss"&gt;reason: &lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;reconnect: &lt;/span&gt;&lt;span class="n"&gt;reconnect&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;websocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;channel.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We don't need to do anything in this file.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;comments_channel.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the channel I generated. This is where users can subscribe to streams. Channels generated inherit from class &lt;code&gt;ApplicationCable::Channel&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;subscribed&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;If there is a project, start a stream, else reject the subscription.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/channels/comments_channel.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;subscribed&lt;/span&gt;
  &lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;
    &lt;span class="n"&gt;stream_for&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;reject&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;receive(data)&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This method is used when you rebroadcast a message. I will not be doing any rebroadcasting in my application, so this method is blank.&lt;/p&gt;

&lt;p&gt;You would send data from the javascript channel back to the ruby channel. That data will go to the receive method, where it will be broadcasted to other users. It will also be broadcasted to the user who sent the message to be rebroadcasted.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;unsubscribed&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is where you do any cleanup when a subscriber unsubscribes. By using the &lt;code&gt;stop_all_streams&lt;/code&gt;, all streams with the channel will be cut.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/channels/comments_channel.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unsubscribed&lt;/span&gt;
  &lt;span class="c1"&gt;# stop_all_streams -&amp;gt; Unsubscribes all streams associated with this channel from the pubsub queue&lt;/span&gt;
  &lt;span class="n"&gt;stop_all_streams&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;javascript/channels/comments_channel.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is where you will manipulate the DOM with data sent from the server.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;connected()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;If there is work you would like to implement when the user is connected to a stream, this is where you'll put it.&lt;/p&gt;

&lt;p&gt;For example, when a user is connected to the stream, I display a message on the screen stating that they are connected. In ten seconds, the message disappears.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/javascript/channels/comments_channel.js&lt;/span&gt;
&lt;span class="nx"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription is ready for use on the server&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;projectsNav&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#projects-nav&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// connectedMessage appears as the first child element of the project nav links header&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectedMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;connectedMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;welcome-message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;connectedMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Welcome to this project's stream! Comments will display in real time. Removing in &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;...`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// The insertAdjacentElement() method of the Element interface inserts a given element node at a given position relative to the element it is invoked upon&lt;/span&gt;
  &lt;span class="nx"&gt;projectsNav&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertAdjacentElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;afterend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;connectedMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;countDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;connectedMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Welcome to this project's stream! Comments will display in real time. Removing in &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;...`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countDown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;connectedMessage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;received(data)&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;When data is sent from the server, it is captured here. You can do whatever you wish with this data. In my &lt;code&gt;received&lt;/code&gt; function, I implement a &lt;code&gt;switch&lt;/code&gt; statement using the data's &lt;code&gt;action&lt;/code&gt; from the server that determines which function will run next.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/javascript/channels/comments_channel.js&lt;/span&gt;
&lt;span class="nx"&gt;received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Called when there's incoming data on the websocket for this channel&lt;/span&gt;

  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;create&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;containerDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;containerDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`comment_&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;containerDiv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;update&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;updateComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;destroy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deleteComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No match found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;appendComment(data)&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is a method I created that handles appending new data to the DOM. The only methods ActionCable provides are &lt;code&gt;connected()&lt;/code&gt;, &lt;code&gt;disconnected()&lt;/code&gt;, and &lt;code&gt;received()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;p&gt;We are able to purposefully trigger page loads by turning off Turbolinks on anchors.&lt;/p&gt;

&lt;h4&gt;
  
  
  JavaScript TurboLinks
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://github.com/turbolinks/turbolinks"&gt;JavaScript Turbolinks&lt;/a&gt; enable a Rails application to act as a single page application, where page visits will swap out the &lt;code&gt;body&lt;/code&gt; and merge the &lt;code&gt;head&lt;/code&gt; so that full-page loads don't happen.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;link_to&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;link_to&lt;/code&gt; allows options of disabling the Turbolink on an &lt;code&gt;a&lt;/code&gt; tag. This ensures a page load occurs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Turbolinks can be disabled on a per-link basis by annotating a link or any of its ancestors with &lt;code&gt;data-turbolinks="false"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= link_to project.name, project, data: {turbolinks: "false"} %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Visiting a URL will also cause a page load.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>Build a Rails 6 API Featuring Graphql</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 28 Oct 2020 03:36:24 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/build-a-rails-6-api-featuring-graphql-6a8</link>
      <guid>https://dev.to/ethanmgustafson/build-a-rails-6-api-featuring-graphql-6a8</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Initial Configuration&lt;/li&gt;
&lt;li&gt;Generate a New Rails API&lt;/li&gt;
&lt;li&gt;Gem Additions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rack-cors&lt;/code&gt; Gem Configuration&lt;/li&gt;
&lt;li&gt;First Migration&lt;/li&gt;
&lt;li&gt;New Graphql Generator&lt;/li&gt;
&lt;li&gt;Schema, Types&lt;/li&gt;
&lt;li&gt;Resources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this blog, I'll be covering how to create an initial Rails backend API utilizing &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;Graphql&lt;/a&gt;. If you don't know Graphql, if you're familiar with it or just need a refresher, this blog is for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Initial Configuration&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Generate a New Rails API
&lt;/h3&gt;

&lt;p&gt;Let's generate a new API-only application. I won't be using most of the gems that come with a new rails generation, so the command I will use with numerous options is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new vacationme_backend --api --database=postgresql --skip-action-mailer --skip-action-mailbox --skip-action-cable&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Gem Additions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Uncomment the &lt;code&gt;rack-cors&lt;/code&gt; gem&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;gem 'graphql'&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;gem 'graphiql-rails'&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;bundle install&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unfortunately, the &lt;code&gt;graphiql-rails&lt;/code&gt; hasn't been updated since January of 2019. There are many pull requests and issues stated, and quite a few direct you to how the application can begin working again. I will show you how you can do so further down.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;rack-cors&lt;/code&gt; Configuration
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;config/initializers/cors.rb&lt;/code&gt;, uncomment the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert_before&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Cors&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;allow&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;origins&lt;/span&gt; &lt;span class="s1"&gt;'*'&lt;/span&gt;
    &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s1"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;headers: :any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;methods: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:patch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:put&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For Rails, you'll need to add this middleware on application startup. A practical way to do this is with an initializer file. For example, the following will allow GET, POST, PATCH, or PUT requests from any origin on any resource:&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;insert_before&lt;/code&gt; to make sure &lt;code&gt;Rack::Cors&lt;/code&gt; runs at the beginning of the stack to make sure it isn't interfered with by other middleware (see &lt;code&gt;Rack::Cache&lt;/code&gt; note in Common Gotchas section). Basic setup examples for Rails 5 &amp;amp; Rails 6 can be found in the examples/ directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In &lt;code&gt;config/application.rb&lt;/code&gt;, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hosts&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"localhost"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Rails 6 has support for blocking requests from unknown hosts, so origin domains will need to be added there as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A more detailed reason for this addition can be found here in the &lt;a href="https://guides.rubyonrails.org/configuring.html#configuring-middleware" rel="noopener noreferrer"&gt;Ruby on Rails Guide&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Migration
&lt;/h3&gt;

&lt;p&gt;Graphql will be interacting with ActiveRecord. We will have to create our migrations and models as we normally would. I am using the &lt;code&gt;bcrypt&lt;/code&gt; gem, so instead of a &lt;code&gt;password&lt;/code&gt; field, it will be a &lt;code&gt;password_digest&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;Run: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rails g model user name:string username:string password_digest:string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rails db:create&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember to include &lt;code&gt;has_secure_password&lt;/code&gt; in &lt;code&gt;app/models/user.rb&lt;/code&gt;. Now let's write two seeds in &lt;code&gt;db/seeds.rb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"JohnDoe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"123"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Ethan Gustafson"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"GoodGuyGuf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"123"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;rails db:seed&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Mounting The Engine
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;graphiql-rails&lt;/code&gt; gem enables one route in your application where you can open a browser and develop using an IDE for graphql.&lt;/p&gt;

&lt;p&gt;After navigating through the issues, I stumbled onto the best solution I could use without changing much of the code I currently have. It can be found here in this &lt;a href="https://github.com/rmosolgo/graphiql-rails/issues/13#issuecomment-640366886" rel="noopener noreferrer"&gt;issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow the &lt;a href="https://github.com/rmosolgo/graphiql-rails" rel="noopener noreferrer"&gt;&lt;code&gt;graphiql-rails&lt;/code&gt;&lt;/a&gt; guide normally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="s2"&gt;"/graphql"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s2"&gt;"graphql#execute"&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;development?&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;GraphiQL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;at: &lt;/span&gt;&lt;span class="s2"&gt;"/graphiql"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;graphql_path: &lt;/span&gt;&lt;span class="s2"&gt;"graphql#execute"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would navigate to "/graphiql" in your browser, and the path would still use the &lt;code&gt;graphql#execute&lt;/code&gt; action.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;config/application.rb&lt;/code&gt;, uncomment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"sprockets/railtie"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sprockets is a Ruby library for compiling and serving web assets. &lt;/p&gt;

&lt;p&gt;To create the last fix, run &lt;code&gt;touch app/assets/config/manifest.js&lt;/code&gt; to create a manifest.js file. Put this inside of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//= link graphiql/rails/application.css&lt;/span&gt;
&lt;span class="c1"&gt;//= link graphiql/rails/application.js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all. This is so the gem can link to the CSS and js it defined in its own files. Now we can begin building the types.&lt;/p&gt;

&lt;h3&gt;
  
  
  New Graphql Generator
&lt;/h3&gt;

&lt;p&gt;Now that we have a user and a record, we can generate the &lt;code&gt;graphql&lt;/code&gt; install. Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;graphql&lt;/span&gt;&lt;span class="ss"&gt;:install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;graphql_controller&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;A &lt;code&gt;graphql&lt;/code&gt; directory containing two subdirectories and one file: 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mutations&lt;/code&gt;, which contains 1 file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;types&lt;/code&gt;, which contains 10 files&lt;/li&gt;
&lt;li&gt;&lt;code&gt;project_name_schema.rb&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To keep it simple I won't go into detail about the mutations directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Schema, Types
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;GraphQL cannot execute a query without a type system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are object, mutation, query, scalar, enumeration, interfaces, union, and input types.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every GraphQL service &lt;strong&gt;defines a set of types&lt;/strong&gt; which completely describe the set of possible data you can query on that service. Then, when queries come in, they are validated and executed &lt;strong&gt;against that schema&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://graphql-ruby.org/getting_started#build-a-schema" rel="noopener noreferrer"&gt;Before building a schema&lt;/a&gt;, you have to define an entry point to your system, which is called the “query root”. The query root will be the &lt;code&gt;query_type.rb&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/graphql/types/query_type.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Types&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;QueryType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
    &lt;span class="c1"&gt;# Add root-level fields here.&lt;/span&gt;
    &lt;span class="c1"&gt;# They will be entry points for queries on your schema.&lt;/span&gt;

     &lt;span class="c1"&gt;# First describe the field signature:&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;UserType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="s2"&gt;"Find a User by ID"&lt;/span&gt;
      &lt;span class="n"&gt;argument&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;required: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
      &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;User&lt;/code&gt; Type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Types&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;BaseObject&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt; 
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;null: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Including the root query in the schema ensures that the application is now functional. Run &lt;code&gt;rails s&lt;/code&gt;. Your application should be working normally, and you can run queries on the types above without a problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/graphql/vacationme_backend_schema.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VacationmeBackendSchema&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;GraphQL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Schema&lt;/span&gt;
  &lt;span class="n"&gt;mutation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;MutationType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Types&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;QueryType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;# Opt in to the new runtime (default in future graphql-ruby versions)&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;GraphQL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Execution&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Interpreter&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;GraphQL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Analysis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;AST&lt;/span&gt;

  &lt;span class="c1"&gt;# Add built-in connections for pagination&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;GraphQL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Pagination&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Connections&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1CofjtyEaU-OqedOwSPTOaZ2JxZma7scv" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1CofjtyEaU-OqedOwSPTOaZ2JxZma7scv" alt="Graphiql Result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Resources:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;Graphql&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://graphql-ruby.org/getting_started" rel="noopener noreferrer"&gt;Getting Started With The Graphql Gem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://graphql-ruby.org/guides" rel="noopener noreferrer"&gt;Graphql Gem Guides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/rmosolgo/graphiql-rails" rel="noopener noreferrer"&gt;Graphiql-Rails Gem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/cyu/rack-cors" rel="noopener noreferrer"&gt;Rack-Cors Gem&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rails</category>
      <category>graphql</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Action Cable Configuration &amp; Subscriptions in Rails</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 21 Oct 2020 02:15:53 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/action-cable-configuration-subscriptions-4b9</link>
      <guid>https://dev.to/ethanmgustafson/action-cable-configuration-subscriptions-4b9</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Table Of Contents&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Action Cable&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;WebSockets&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TCP/IP&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terminology&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Queue Data Structure&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Rails/JavaScript Code&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Server-Side Components&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client-Side Components&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client-Server Interactions&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Configuration&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Redis&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action Cable Server&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;NOTE: At the time this blog was written, the Rails version in use was 6.0. &lt;/p&gt;




&lt;p&gt;In this blog, I will walk through configuring and implementing &lt;a href="https://guides.rubyonrails.org/action_cable_overview.html" rel="noopener noreferrer"&gt;Action Cable&lt;/a&gt; into a Rails Application. As I'm writing, I don't know how Action Cable works and what underlying processes make it functional. &lt;/p&gt;

&lt;p&gt;This is why I love writing technical blogs. It is such a great way to learn and document processes to reference later. There's no way I'm going to remember everything, but as long as the fundamentals are there I will know where to look when I need to remember.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Action Cable&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Action Cable is a bundle of code providing a &lt;strong&gt;client-side JavaScript framework&lt;/strong&gt; and a &lt;strong&gt;server-side Ruby framework.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It integrates &lt;strong&gt;WebSockets&lt;/strong&gt; with the rest of a rails application. This allows the application to have certain real-time features to be written in Ruby.&lt;/p&gt;

&lt;p&gt;For an example, I'm currently writing an application called &lt;strong&gt;FilmPitch&lt;/strong&gt;, where filmmakers can fund their dream movies. A &lt;strong&gt;Project&lt;/strong&gt; &lt;code&gt;has_many :comments&lt;/code&gt;. When a user comments, the browser will update so that the comment will display in real-time.&lt;/p&gt;

&lt;p&gt;So what are WebSockets and how do they make real-time features possible? &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Web Sockets&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;There is a lot of Wikipedia information in this section. I wanted to piece together the important bits to know before going forward.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/WebSocket" rel="noopener noreferrer"&gt;&lt;strong&gt;WebSocket&lt;/strong&gt;&lt;/a&gt; is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Communication_protocol" rel="noopener noreferrer"&gt;&lt;strong&gt;communications protocol&lt;/strong&gt;&lt;/a&gt; is a system of rules that allow two or more entities of a communication system to transmit information via any kind of variation of a physical quantity.&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Duplex_(telecommunications)#FULL-DUPLEX" rel="noopener noreferrer"&gt;&lt;strong&gt;full-duplex&lt;/strong&gt;&lt;/a&gt; (FDX) system, or sometimes called double-duplex, allows communication in both directions, and, unlike half-duplex, allows this to happen simultaneously.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;The WebSocket protocol is &lt;strong&gt;&lt;em&gt;different&lt;/em&gt;&lt;/strong&gt; from HTTP, the Hypertext Transfer protocol, although it is compatible with HTTP. Essentially, the &lt;strong&gt;WebSocket protocol facilitates real-time data transfer from and to the server.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;HTTP is a request-response protocol. It doesn't keep a connection open. It only sends data when requested. The WebSocket protocol sends data back and forth between client and server &lt;strong&gt;continuously&lt;/strong&gt;, without being requested by the client.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;This is made possible by providing a &lt;strong&gt;standardized way for the server to send content to the client without being first requested by the client&lt;/strong&gt;, and allowing messages to be passed back and forth while keeping the connection open. In this way, a two-way ongoing conversation can take place between the client and the server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, cell phones are full-duplex, as two callers are allowed to speak and hear the other at the same time. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;TCP/IP&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol" rel="noopener noreferrer"&gt;&lt;strong&gt;Transmission Control Protocol&lt;/strong&gt;&lt;/a&gt; (TCP) is one of the main protocols of the Internet protocol suite. TCP provides reliable, ordered, and error-checked delivery of a stream of octets (bytes) between applications running on hosts communicating via an IP network.&lt;/p&gt;

&lt;p&gt;TCP is connection-oriented, and &lt;strong&gt;a connection between client and server is established before data can be sent.&lt;/strong&gt; The server must be listening (passive open) for connection requests from clients before a connection is established. Three-way handshake (active open), &lt;a href="https://en.wikipedia.org/wiki/Retransmission_(data_networks)" rel="noopener noreferrer"&gt;retransmission&lt;/a&gt;, and error-detection adds to reliability but lengthens latency. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;Applications that do not require reliable data stream service may use the &lt;a href="https://en.wikipedia.org/wiki/User_Datagram_Protocol" rel="noopener noreferrer"&gt;User Datagram Protocol (UDP)&lt;/a&gt;, which provides a connectionless datagram service that prioritizes time over reliability. TCP employs network congestion avoidance. However, there are vulnerabilities to TCP including denial of service, connection hijacking, TCP veto, and reset attack. For network security, monitoring, and debugging, TCP traffic can be intercepted and logged with a packet sniffer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Network_function" rel="noopener noreferrer"&gt;&lt;strong&gt;Network Function&lt;/strong&gt;&lt;/a&gt; section of the TCP Wiki will detail more on how the protocol functions.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Internet_protocol_suite" rel="noopener noreferrer"&gt;&lt;strong&gt;Internet protocol suite&lt;/strong&gt;&lt;/a&gt; is the conceptual model and set of communications protocols used in the Internet and similar computer networks. It is commonly known as TCP/IP because the foundational protocols in the suite are the Transmission Control Protocol (TCP) and the Internet Protocol (IP).&lt;/p&gt;

&lt;p&gt;The Internet protocol suite provides &lt;em&gt;end-to-end data communication&lt;/em&gt; &lt;strong&gt;specifying how data should be packetized, addressed, transmitted, routed, and received.&lt;/strong&gt; This functionality is organized into four abstraction layers, which classify all related protocols according to the scope of networking involved. From lowest to highest, the layers are the link layer, containing communication methods for data that remains within a single network segment (link); the internet layer, providing internetworking between independent networks; the transport layer, handling host-to-host communication; and the application layer, providing process-to-process data exchange for applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Terminology&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://guides.rubyonrails.org/action_cable_overview.html#terminology" rel="noopener noreferrer"&gt;Action Cable Terminology&lt;/a&gt; section of the Ruby on Rails Guide will detail all of the terms I list below. I'll stitch together everything so that it makes more sense. If it doesn't, the configuration section will help make it clear.&lt;/p&gt;

&lt;p&gt;Action Cable can handle many connection instances. There is one &lt;strong&gt;connection instance&lt;/strong&gt; for every WebSocket. A user could have more than one tab open in their browser, which means that there can be more than one connection instance in a user's browser.&lt;/p&gt;

&lt;p&gt;The client is referred to as the browser. The client of a WebSocket connection is called the &lt;strong&gt;&lt;em&gt;consumer&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each &lt;strong&gt;&lt;em&gt;consumer&lt;/em&gt;&lt;/strong&gt; can &lt;strong&gt;&lt;em&gt;subscribe&lt;/em&gt;&lt;/strong&gt; to &lt;strong&gt;&lt;em&gt;multiple&lt;/em&gt;&lt;/strong&gt; cable &lt;strong&gt;&lt;em&gt;channels&lt;/em&gt;&lt;/strong&gt;. When a consumer is subscribed to a channel, they act as a &lt;strong&gt;&lt;em&gt;subscriber&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Pub/Sub&lt;/strong&gt;, or Publish-Subscribe, refers to a &lt;strong&gt;message queue paradigm&lt;/strong&gt; whereby senders of information (&lt;strong&gt;publishers&lt;/strong&gt;), send data to an abstract class of recipients (&lt;strong&gt;subscribers&lt;/strong&gt;), &lt;em&gt;without specifying individual recipients.&lt;/em&gt; Action Cable uses this approach to communicate between the server and many clients.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Essentially, all users(consumers) subscribed to a channel will get updates without requesting them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In software architecture, publish-subscribe is a messaging pattern where senders of messages, called &lt;em&gt;publishers&lt;/em&gt;, &lt;strong&gt;do not program the messages to be sent directly to specific receivers, called subscribers&lt;/strong&gt;, but &lt;em&gt;instead categorize published messages into classes&lt;/em&gt; &lt;strong&gt;without knowledge of which subscribers, if any, there may be.&lt;/strong&gt; Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The connection between the subscriber and the channel is called a &lt;strong&gt;&lt;em&gt;subscription&lt;/em&gt;&lt;/strong&gt;. A consumer could subscribe to multiple chat rooms at the same time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Each channel encapsulates a logical unit of work&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;similar to what a controller does&lt;/em&gt; &lt;strong&gt;in a regular MVC setup.&lt;/strong&gt; For example, you could have a ChatChannel and an AppearancesChannel, and a &lt;strong&gt;consumer could be subscribed to either or to both of these channels.&lt;/strong&gt; At the very least, a consumer should be subscribed to one channel.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each &lt;strong&gt;channel can stream zero or more broadcastings.&lt;/strong&gt; A broadcasting is a pubsub link where &lt;strong&gt;anything transmitted by the broadcaster&lt;/strong&gt; is &lt;strong&gt;&lt;em&gt;sent directly to the channel subscribers&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;who are streaming that named broadcasting.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Controllers will work like normal. In my &lt;code&gt;Commentscontroller&lt;/code&gt;, the &lt;code&gt;#create&lt;/code&gt; action is what will create, save, and call the &lt;strong&gt;job&lt;/strong&gt; that will broadcast the newly saved comment to the channel. &lt;strong&gt;ActiveJob&lt;/strong&gt; will then handle broadcasting the information to channel subscribers.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Queue Data Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.geeksforgeeks.org/queue-data-structure/" rel="noopener noreferrer"&gt;Queue Data Structure&lt;/a&gt; is just like the &lt;a href="https://www.geeksforgeeks.org/stack-data-structure/" rel="noopener noreferrer"&gt;Stack Data Structure&lt;/a&gt;. Stacks follow a LIFO (Last-in First-out) principle. Queues follow the FIFO (First-in First-out) principle. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Rails/JavaScript Code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This section details the purpose behind the files in &lt;code&gt;app/channels&lt;/code&gt; and &lt;code&gt;app/javascript/channels&lt;/code&gt;. Don't worry about configuration for now. &lt;/p&gt;

&lt;p&gt;A lot of it is from the Action Cable guide, and that is on purpose. The important bits are set in bold. The Terminology section introduces the terms, this section introduces what you'll be working with, and the configuration section pieces everything together in a linear fashion.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Server-Side Components&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Connections&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;For every WebSocket accepted by the server, a connection object is instantiated. This object becomes the parent of all channel subscriptions that are created from thereon.&lt;/p&gt;

&lt;p&gt;The connection itself &lt;strong&gt;does not deal&lt;/strong&gt; with any specific application logic beyond &lt;strong&gt;&lt;em&gt;authentication and authorization.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;blockquote&gt;
&lt;p&gt;Connections are instances of &lt;code&gt;ApplicationCable::Connection.&lt;/code&gt; In this class, &lt;strong&gt;you authorize the incoming connection&lt;/strong&gt;, and proceed to establish it &lt;strong&gt;if the user can be identified.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# app/channels/application_cable/connection.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ApplicationCable&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Connection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
    &lt;span class="n"&gt;identified_by&lt;/span&gt; &lt;span class="ss"&gt;:current_user&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_verified_user&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_verified_user&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;verified_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user_id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
          &lt;span class="n"&gt;verified_user&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
          &lt;span class="n"&gt;reject_unauthorized_connection&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;identified_by&lt;/code&gt; is a connection identifier that can be used to find this specific connection later.&lt;/p&gt;

&lt;p&gt;The above example assumes that you authenticated your user somewhere else in your app and set a signed cookie with the &lt;code&gt;user_id&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The cookie is then automatically sent to the connection instance when a new connection is attempted, and you use that to set the &lt;code&gt;current_user&lt;/code&gt;. By identifying the connection by this same current user, you're also ensuring that you can later retrieve all open connections by a given user (and potentially disconnect them all if the user is deleted or unauthorized).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Channels&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;A channel encapsulates a logical unit of work, similar to what a controller does in a regular MVC setup. By default, Rails creates a parent &lt;code&gt;ApplicationCable::Channel&lt;/code&gt; class for encapsulating shared logic between your channels.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# app/channels/application_cable/channel.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ApplicationCable&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Channel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Channel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is the &lt;strong&gt;parent channel.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You don't have to adjust anything here. Any new channel you create will inherit from &lt;code&gt;ActionCable::Channel&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g channel --help&lt;/code&gt; will detail the ways in which you can generate a new channel. I will be creating a comments channel, so my command will be &lt;code&gt;rails g channel Comments&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Subscriptions&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Consumers subscribe to channels, acting as subscribers. Their connection is called a subscription. &lt;strong&gt;Produced messages are then routed to these channel subscriptions&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;based on an identifier sent by the cable consumer.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Data is broadcasted to this channel. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# app/channels/comments_channel.rb&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommentsChannel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Channel&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;subscribed&lt;/span&gt;
    &lt;span class="c1"&gt;# this is called when the consumer has successfully&lt;/span&gt;
    &lt;span class="c1"&gt;# become a subscriber to this channel.&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unsubscribed&lt;/span&gt;
    &lt;span class="c1"&gt;# Any cleanup needed when channel is unsubscribed&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Client-Side Components&lt;/strong&gt;
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Connections&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Consumers require an instance of the connection on their side. This can be established using the following JavaScript, which is generated by default by Rails:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// app/javascript/channels/consumer.js&lt;/span&gt;
&lt;span class="c1"&gt;// Action Cable provides the framework to deal with WebSockets in Rails.&lt;/span&gt;
&lt;span class="c1"&gt;// You can generate new channels where WebSocket features live using the `rails generate channel` command.&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createConsumer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@rails/actioncable&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;createConsumer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;From the Action Cable guide, &lt;code&gt;createConsumer&lt;/code&gt; will connect to &lt;code&gt;"/cable"&lt;/code&gt; automatically if you don't specify a URL argument for it. There's not much else to this file.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Subscriber&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In order for a user to subscribe to a channel, you have to create a subscription in your channel -&amp;gt; &lt;code&gt;app/javascript/channels/${channel_name}.js.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;My comments channel was generated like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./consumer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Generated with `rails g channel Comments`&lt;/span&gt;

&lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CommentsChannel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription is ready for use on the server&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;disconnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription has been terminated by the server&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when there's incoming data on the websocket for this channel&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The channel name needs to match your rails channel name and/or room. If you've never seen the syntax above, it is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions" rel="noopener noreferrer"&gt;Method Properties&lt;/a&gt; shorthand feature in ES6.&lt;/p&gt;

&lt;p&gt;It's essentially the same as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;disconnected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;received&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you need to see the flow of their &lt;code&gt;Consumer&lt;/code&gt;, &lt;code&gt;Subscription(s)&lt;/code&gt; classes, you can find them &lt;a href="https://github.com/rails/rails/tree/master/actioncable/app/javascript/action_cable" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Client-Server Interactions&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Streams&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Streams enable a channel to route broadcasts to subscribers. When new data is sent, the stream allows the channel to route that data to clients connected to the channel.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stream_for&lt;/code&gt; and &lt;code&gt;stream_from&lt;/code&gt; basically do the same thing. Here is their &lt;a href="https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/channel/streams.rb" rel="noopener noreferrer"&gt;code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stream_for&lt;/code&gt; is more used for a related model. It automatically generates broadcasting from the model and channel for you.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Broadcasting&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;A broadcasting is a pub/sub link where anything transmitted by a publisher is routed directly to the channel subscribers who are streaming that named broadcasting.&lt;/p&gt;

&lt;p&gt;The default pubsub queue for Action Cable is &lt;code&gt;redis&lt;/code&gt; in production and &lt;code&gt;async&lt;/code&gt; in development and test environments.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I will show you how to use &lt;code&gt;ActiveJob&lt;/code&gt; with rails so that Action Cable can use Redis in the configuration section. ActiveJob allows jobs to run in queueing backends.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Subscriptions&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;When a consumer subscribes to a channel, they become a subscriber. The connection between the two is a subscription. The &lt;strong&gt;data sent by the rails channel will be available as an argument to the method properties objects in the channel js file.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;received(data)&lt;/code&gt; method is called when there's incoming data on the WebSocket for a channel. In my &lt;code&gt;comments_channel.js&lt;/code&gt; file, the &lt;code&gt;data&lt;/code&gt; is an already rendered &lt;code&gt;erb&lt;/code&gt; template. It's already in HTML, so I'm just appending it where I want it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log("Recieving...")&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log("Appending...")&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log("I have appended!")&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Passing Parameters to Channels&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;If you're looking at your &lt;code&gt;${name}_channel.rb&lt;/code&gt; &lt;code&gt;#subscribed&lt;/code&gt; method confused about where the &lt;code&gt;params&lt;/code&gt; are coming in from, they're coming from the &lt;code&gt;${name}_channel.js&lt;/code&gt; file. If you start &lt;code&gt;byebug&lt;/code&gt; when the &lt;code&gt;subscribed&lt;/code&gt; method is called, the only params you will get is the channel name because it was defined where the subscription was created at the top: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CommentsChannel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription is ready for use on the server&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;disconnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription has been terminated by the server&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when there's incoming data on the websocket for this channel&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Configuration&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Note: I'm using Postgres &amp;amp; Devise in this application.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Redis&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I will be using &lt;a href="https://en.wikipedia.org/wiki/Redis" rel="noopener noreferrer"&gt;Redis&lt;/a&gt; as the queueing backend.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis is an in-memory data structure project implementing a distributed, in-memory key–value database with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you don't have it installed on Mac, install it with &lt;code&gt;brew install redis&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Install the Redis gem with &lt;code&gt;gem install redis&lt;/code&gt;. In case this gem isn't in your &lt;code&gt;Gemfile&lt;/code&gt;, add it and run &lt;code&gt;bundle install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;config/cable.yml&lt;/code&gt; file, make sure the adapter for your environments is Redis. For some reason, Redis was having errors with the other adapters set with &lt;code&gt;async&lt;/code&gt;, so I set them all to &lt;code&gt;redis&lt;/code&gt;. Also set the URL, which should already be present in the environment file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="ss"&gt;development:
  adapter: &lt;/span&gt;&lt;span class="n"&gt;redis&lt;/span&gt;
  &lt;span class="ss"&gt;url: &lt;/span&gt;&lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="ss"&gt;:/&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;localhost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;In order for Rails to connect to Redis, you have to start a server in another terminal.&lt;/strong&gt; Start a Redis server by running &lt;code&gt;redis-server&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Action Cable Server&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The Action Cable Server can run either separately from or alongside your application. &lt;strong&gt;I have it set so that it runs when I launch my Rails server.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;config/application.rb&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In &lt;code&gt;config/application.rb&lt;/code&gt;, you have to mount the path for Action Cable: &lt;code&gt;config.action_cable.mount_path = '/cable'&lt;/code&gt;. &lt;strong&gt;This is where it will listen for WebSocket requests.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;views/layouts/application/html.erb&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In the &lt;code&gt;views/layouts/application/html.erb&lt;/code&gt;, add an &lt;code&gt;action_cable_meta_tag&lt;/code&gt; in the head. &lt;code&gt;ActionCable.createConsumer()&lt;/code&gt; will connect the path from this &lt;code&gt;meta_tag&lt;/code&gt; and use it as an argument.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= action_cable_meta_tag %&amp;gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;To configure the URL, add a call to &lt;code&gt;action_cable_meta_tag&lt;/code&gt; in your HTML layout HEAD. &lt;strong&gt;This uses a URL or path typically set via &lt;code&gt;config.action_cable.url&lt;/code&gt; in the environment configuration files.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;config/environments/development&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In &lt;code&gt;config/environments/development&lt;/code&gt;, add:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_cable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ws:localhost:3000/cable"&lt;/span&gt;

  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_cable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;allowed_request_origins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;/http:\/\/*/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sr"&gt;/https:\/\/*/&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;action_cable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;worker_pool_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Action Cable will only accept requests from specified origins, which are passed to the server config as an array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Set the pool size equal to what you have in your &lt;code&gt;config/database.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;...your server must provide at least the same number of database connections as you have workers. The default worker pool size is set to 4, so that means you have to make at least 4 database connections available. You can change that in &lt;code&gt;config/database.yml&lt;/code&gt; through the &lt;code&gt;pool&lt;/code&gt; attribute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;config/routes.rb&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;I don't believe I saw this in the Action Cable guide nor the example application they had, but it is present in many other blog examples. Not sure why it's omitted in the guide, have to look into it later.&lt;/p&gt;

&lt;p&gt;Mount the Action Cable Server in &lt;code&gt;config/routes.rb&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;ActionCable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'/cable'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;app/channels/application_cable&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In this directory, you'll find two files: &lt;code&gt;channel.rb&lt;/code&gt; and &lt;code&gt;connection.rb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That channel is the parent channel, so you don't need to alter that file at all.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;connection.rb&lt;/code&gt; is where you will authenticate and authorize your user for their connection. I'm using &lt;strong&gt;Devise&lt;/strong&gt;, so my user is authenticated like so:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;ApplicationCable&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Connection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
    &lt;span class="n"&gt;identified_by&lt;/span&gt; &lt;span class="ss"&gt;:current_user&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;current_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_verified_user&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;disconnect&lt;/span&gt;
      &lt;span class="c1"&gt;# Any cleanup work needed when the cable connection is cut.&lt;/span&gt;
      &lt;span class="c1"&gt;# close(reason: nil, reconnect: true)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="kp"&gt;private&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_verified_user&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;verified_user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'warden'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;
          &lt;span class="n"&gt;verified_user&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
          &lt;span class="c1"&gt;# You can find the reject_unauthorized_connection method here -&amp;gt; https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/connection/authorization.rb&lt;/span&gt;
          &lt;span class="n"&gt;reject_unauthorized_connection&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Essentially, logged in users connect to the action cable server. They don't become a subscriber yet, though. The channel's &lt;code&gt;#subscribed&lt;/code&gt; method will handle that portion. This class is all about authenticating and authorizing the user for this specific connection, allowing Action Cable to find the connection later.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;reject_unauthorized_connection&lt;/code&gt; is a method given to you by &lt;a href="https://api.rubyonrails.org/classes/ActionCable/Connection/Authorization.html#method-i-reject_unauthorized_connection" rel="noopener noreferrer"&gt;&lt;code&gt;ActionCable::Connection::Authorization&lt;/code&gt;&lt;/a&gt;. You can also find this method here in the &lt;a href="https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/connection/authorization.rb" rel="noopener noreferrer"&gt;Github&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;comments_channel.rb&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;I generated my comments channel with the &lt;code&gt;rails g channel&lt;/code&gt; command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommentsChannel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationCable&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Channel&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;subscribed&lt;/span&gt;
    &lt;span class="n"&gt;project&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="c1"&gt;# in Rails 6.1, a new method for handling the below control structure is defined as&lt;/span&gt;
    &lt;span class="c1"&gt;# stream_or_reject_for(record), which houses this code:&lt;/span&gt;

    &lt;span class="c1"&gt;# if there is a record, subscribe the user and start a stream, else reject&lt;/span&gt;
    &lt;span class="c1"&gt;# the user and don't start a new stream.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;
      &lt;span class="n"&gt;stream_for&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;reject&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Rebroadcast a message sent by one client to any other connected clients&lt;/span&gt;
    &lt;span class="c1"&gt;# ActionCable.server.broadcast(project, data)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unsubscribed&lt;/span&gt;
    &lt;span class="c1"&gt;# Any cleanup needed when channel is unsubscribed&lt;/span&gt;
    &lt;span class="c1"&gt;# stop_all_streams() -&amp;gt; Unsubscribes all streams associated with this channel from the pubsub queue&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Right now, &lt;strong&gt;only&lt;/strong&gt; the &lt;code&gt;#subscribed&lt;/code&gt; method is functional. The params &lt;code&gt;id&lt;/code&gt; is given to me from javascript. If the URL doesn't have a project &lt;code&gt;id&lt;/code&gt;, the subscription won't be set, and no stream will start.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;comments_channel.js&lt;/code&gt;
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./consumer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Generated with `rails g channel Comments`&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CommentsChannel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription is ready for use on the server&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Connected to the comments channel!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;disconnected&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when the subscription has been terminated by the server&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Called when there's incoming data on the websocket for this channel&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log("Recieving...")&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// console.log("Appending...")&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log("I have appended!")&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nf"&gt;appendComment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commentSection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;comments&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;commentSection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertAdjacentHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;afterbegin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For right now, the server gets the &lt;code&gt;id&lt;/code&gt; from the URL. It sends it as a param to the rails channel subscribed method.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;ActiveJob&lt;/code&gt; &amp;amp; Broadcasting
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommentBroadcastJob&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationJob&lt;/span&gt;
  &lt;span class="n"&gt;queue_as&lt;/span&gt; &lt;span class="ss"&gt;:default&lt;/span&gt;

  &lt;span class="c1"&gt;# Broadcast a hash to a unique broadcasting for this &amp;lt;tt&amp;gt;model&amp;lt;/tt&amp;gt; in this channel.&lt;/span&gt;

    &lt;span class="c1"&gt;# def broadcast_to(model, message)&lt;/span&gt;
    &lt;span class="c1"&gt;#   ActionCable.server.broadcast(broadcasting_for(model), message)&lt;/span&gt;
    &lt;span class="c1"&gt;# end&lt;/span&gt;

  &lt;span class="c1"&gt;# Active Job objects can be defined by creating a class that inherits from the &lt;/span&gt;
  &lt;span class="c1"&gt;# ActiveJob::Base class. The only necessary method to implement is the “perform” method.&lt;/span&gt;


  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;CommentsChannel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;broadcast_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This class is used to send the broadcasts. What I'm doing here is having the project and partial broadcasted. It gets called in the &lt;code&gt;CommentsController&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;comments_controller.rb&lt;/code&gt;
&lt;/h4&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="vi"&gt;@comment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;comment_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valid?&lt;/span&gt;

      &lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="c1"&gt;# You have to use methods found in ActiveJob::Core::ClassMethods -&amp;gt; &lt;/span&gt;
      &lt;span class="c1"&gt;# https://edgeapi.rubyonrails.org/classes/ActiveJob/Core/ClassMethods.html&lt;/span&gt;

      &lt;span class="c1"&gt;# To enqueue a job to be performed as soon as the queuing system is free, use:&lt;/span&gt;
      &lt;span class="c1"&gt;# .perform_later(record)&lt;/span&gt;

      &lt;span class="vi"&gt;@obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;description: &lt;/span&gt;&lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;user_id: &lt;/span&gt;&lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;project_id: &lt;/span&gt;&lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;project_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;display_name: &lt;/span&gt;&lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;display_name&lt;/span&gt;
      &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;as_json&lt;/span&gt;

      &lt;span class="no"&gt;CommentBroadcastJob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform_later&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="vi"&gt;@comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;render_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="ss"&gt;partial: &lt;/span&gt;&lt;span class="s1"&gt;'comments/comment'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="ss"&gt;locals: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="ss"&gt;comment: &lt;/span&gt;&lt;span class="vi"&gt;@obj&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;project_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is all messy right now, but the data in my views are using a comments hash, so I'll end up refactoring this later. Either &lt;code&gt;render&lt;/code&gt; or &lt;code&gt;render_to_string&lt;/code&gt; works here. The partial will be created with the data you want while using rails helpers in the views: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;comments&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;_comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;erb&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h4&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= comment['display_name'] %&amp;gt;&amp;lt;/h4&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'description'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;% if &lt;/span&gt;&lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'user_id'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="no"&gt;Edit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;
      &amp;lt;p&amp;gt;
        &amp;lt;%= link_to 'delete', 
        { controller: "comments", action: "destroy", id: comment['id'] }, 
        data: { confirm: 'Are you sure?' }, 
        method: :delete %&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This has allowed two users to see comments in real-time. Here is a gif showing the process:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1Pq3ZN3smndxoV3zOOX6i69Bf8R1_om8_" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1Pq3ZN3smndxoV3zOOX6i69Bf8R1_om8_" alt="Live Comments gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I still have to figure out how I can stop displaying the edit/delete buttons for other users.&lt;/p&gt;

&lt;p&gt;I figured it would be great to have this blog have the meat of everything. I spent a good amount of time going through many Wikipedia pages, rails guides, rails repos, blogs, and videos to figure out exactly how to get Action Cable to run. Hope it helps clear some confusion!&lt;/p&gt;

&lt;p&gt;This is the project repo: &lt;a href="https://github.com/GoodGuyGuf/FilmPitch" rel="noopener noreferrer"&gt;&lt;strong&gt;FilmPitch&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions or observations, please comment below. 🤩 &lt;/p&gt;

</description>
      <category>rails</category>
      <category>javascript</category>
      <category>postgres</category>
      <category>redis</category>
    </item>
    <item>
      <title>Building a Website With Gatsby.js</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Tue, 13 Oct 2020 21:39:40 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/building-a-website-with-gatsby-js-2h14</link>
      <guid>https://dev.to/ethanmgustafson/building-a-website-with-gatsby-js-2h14</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Table Of Contents:&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What is Gatsby?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Plugins, Themes, Starters&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Installation, Creation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;File Structure&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pages, images, components directories&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;image.js, header.js&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;gatsby.js files&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Graphql&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This blog is purposed to be a simple overview of creating a website using Gatsby. The &lt;a href="https://www.gatsbyjs.com/"&gt;Gatsby website&lt;/a&gt; will guide you along in creating a website, even if you don't know much about web development. It will teach you topics during the tutorials, but I will walk you through everything all together and will be as direct as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Gatsby?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Gatsby.js&lt;/strong&gt; is an open-source framework that utilizes &lt;a href="https://reactjs.org/"&gt;&lt;strong&gt;React.js&lt;/strong&gt;&lt;/a&gt; to generate static websites. What is a static website? &lt;/p&gt;

&lt;p&gt;A static website doesn't dynamically change. Dynamic websites render different content depending on the data it receives. A static website will be delivered to you as it is stored. Although Gatsby uses Graphql(which is a query language), there is no database. Instead, data retrieving occurs when the app is being built, from local files.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Plugins, Themes, Starters&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There is an abundance of plugins available that add functionality to your application, such as Responsive Images,  an RSS feed, Google Analytics, etc.&lt;/p&gt;

&lt;p&gt;Gatsby Themes are Plugins that come with pre-configured functionality, data sourcing, and/or UI code. There are "Starters", which are website boilerplates that are pre-configured with a direct purpose in mind, like creating a portfolio.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Installation, Creation&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install Gatsby globally with &lt;code&gt;npm install -g gatsby-cli&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;To create a new website, run &lt;code&gt;gatsby new {your-project-name} {link-to-starter}&lt;/code&gt; (&lt;code&gt;{link-to-starter}&lt;/code&gt; can be omitted)&lt;/li&gt;
&lt;li&gt;Run the server with &lt;code&gt;gatsby develop&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you didn't use a starter, then here are some plugins to help get you started. Gatsby image, react helmet, plugin sharp, and the transformer-sharp plugin should already be configured in gatsby-config.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.com/plugins/gatsby-image"&gt;gatsby-image&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.com/plugins/gatsby-background-image/"&gt;gatsby-background-image&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.com/docs/styled-components/"&gt;gatsby-plugin-styled-components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.com/plugins/gatsby-plugin-offline/?=offline"&gt;gatsby-plugin-offline&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of those plugins can be installed after you create the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;File Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you open your newly created project, it used &lt;code&gt;gatsby-starter-default&lt;/code&gt; to generate everything. You'll see a few &lt;code&gt;gatsby.js&lt;/code&gt; files in the root directory, along with the &lt;code&gt;src&lt;/code&gt; directory, which contains three folders:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;components&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;images&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pages&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you decide to rename the files within these folders, you might have to make sure that when you begin importing other files, that the name of what you are importing matches the casing of the filename. If you don't, the terminal will report warnings to you about modules casing inconsistency concerning certain file imports.&lt;/p&gt;

&lt;p&gt;For example, I changed the name of &lt;code&gt;layout.js&lt;/code&gt; to &lt;code&gt;Layout.js&lt;/code&gt;, and I began receiving warnings about modules with different casing names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* It turns out that imports I had in other files like 404.js were: */&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Layout&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="c1"&gt;// When it needed to be &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Layout&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/Layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;// To remove the warnings&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;components&lt;/code&gt;, &lt;code&gt;images&lt;/code&gt;, &lt;code&gt;pages&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The pages folder contains your 'routes'. Each new file created will become a new page in your project, where the name of the file will become the name of the URL route. For example, &lt;code&gt;about.js&lt;/code&gt; would generate the URL route &lt;code&gt;/about&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You'll find four files inside. &lt;code&gt;404.js&lt;/code&gt;, &lt;code&gt;index.js&lt;/code&gt;, &lt;code&gt;page-2.js&lt;/code&gt;, and &lt;code&gt;using-typescript.tsx&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Inside of the &lt;code&gt;index.js&lt;/code&gt; file, you will see this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gatsby&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Layout&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/layout&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;SEO&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/seo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;IndexPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Layout&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SEO&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Home&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Gatsby&lt;/span&gt; &lt;span class="nx"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Now&lt;/span&gt; &lt;span class="nx"&gt;go&lt;/span&gt; &lt;span class="nx"&gt;build&lt;/span&gt; &lt;span class="nx"&gt;something&lt;/span&gt; &lt;span class="nx"&gt;great&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;maxWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`300px`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;marginBottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`1.45rem`&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/page-2/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Go&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&amp;gt; &amp;lt;br /&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/using-typescript/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Go&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Using TypeScript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Layout&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;IndexPage&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening here? The &lt;code&gt;index.js&lt;/code&gt; file is the file gatsby loads upon starting the server. The contents of this file are rendered and sent to the browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Layout&lt;/code&gt;&lt;/strong&gt; is a component in the &lt;code&gt;components&lt;/code&gt; directory. In &lt;code&gt;index.js&lt;/code&gt;, everything inside of &lt;code&gt;Layout&lt;/code&gt; is an argument to the &lt;code&gt;Layout&lt;/code&gt; component. If you are doing data retrieving, &lt;code&gt;layout.js&lt;/code&gt; is where you can query the data with Graphql to be shown in the browser. &lt;/p&gt;

&lt;p&gt;If you look at the &lt;code&gt;return&lt;/code&gt; statement, you'll see this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Header&lt;/span&gt; &lt;span class="nx"&gt;siteTitle&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s2"&gt;`Title`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;
        &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
          &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`0 auto`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;maxWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;960&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`0 1.0875rem 1.45rem`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/main&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;footer&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;
          &lt;span class="na"&gt;marginTop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`2rem`&lt;/span&gt;
        &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="err"&gt;©&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;getFullYear&lt;/span&gt;&lt;span class="p"&gt;()},&lt;/span&gt; &lt;span class="nx"&gt;Built&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;` `&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://www.gatsbyjs.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Gatsby&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/footer&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything is wrapped with React Fragments(&lt;code&gt;&amp;lt;&amp;gt;&amp;lt;/&amp;gt;&lt;/code&gt;), and as you can see the JSX represents the body of the HTML document. There is a Header, main, and footer. The &lt;code&gt;Header&lt;/code&gt; component is receiving the data retrieved from &lt;code&gt;layout.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main&lt;/code&gt; is containing &lt;code&gt;children&lt;/code&gt;, which were passed into &lt;code&gt;Layout&lt;/code&gt; as arguments(from &lt;code&gt;index.js&lt;/code&gt;). Every argument Layout takes in will become a child element of the &lt;code&gt;main&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;Layout&lt;/code&gt;, you will see: &lt;code&gt;&amp;lt;SEO title="Home" /&amp;gt;&lt;/code&gt;. SEO stands for Search Engine Optimization. All of your page contents are available to search engine crawlers because Gatsby uses Server-Side-Rendering.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;SEO&lt;/code&gt; component deals with the metadata in the &lt;code&gt;head&lt;/code&gt; element. It uses Graphql to query metadata to be placed in the &lt;code&gt;head&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the very bottom, you will find this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PropTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isRequired&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is &lt;code&gt;.propTypes&lt;/code&gt; ? In React, &lt;code&gt;propTypes&lt;/code&gt; deals with type checking. Type checking is used to ensure that props contain certain prop types.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;children&lt;/code&gt; prop is being type-checked. &lt;code&gt;PropTypes&lt;/code&gt; define types of data for props. &lt;code&gt;node&lt;/code&gt; is any value that can be rendered on the screen. &lt;code&gt;isRequired&lt;/code&gt; ensures that the type of data the &lt;code&gt;children&lt;/code&gt; prop should be receiving is of the &lt;code&gt;node&lt;/code&gt; type.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;image.js&lt;/code&gt;, &lt;code&gt;header.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;What is &lt;code&gt;gatsby-image&lt;/code&gt;? How does it function?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gatsby-image&lt;/code&gt; works with &lt;code&gt;gatsby-transformer-sharp&lt;/code&gt; and &lt;code&gt;gatsby-plugin-sharp&lt;/code&gt;. &lt;code&gt;gatsby-source-filesystem&lt;/code&gt; connects your local files together so that &lt;code&gt;gatsby-image&lt;/code&gt; can locate them in your Graphql queries. &lt;code&gt;gatsby-image&lt;/code&gt; doesn't require any configuration when used within Gatsby.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gatsby-image&lt;/code&gt; is used in &lt;code&gt;image.js&lt;/code&gt; to handle images. The &lt;a href="https://www.gatsbyjs.com/docs/gatsby-image/"&gt;Gatsby Image API&lt;/a&gt; states:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;gatsby-image&lt;/code&gt; is a React component. It is designed to work with Gatsby's native image processing capabilities powered by Graphql and &lt;code&gt;gatsby-plugin-sharp&lt;/code&gt; to optimize image loading for your site.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gatsby-image&lt;/code&gt; is &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; a drop-in replacement for &lt;code&gt;&amp;lt;img /&amp;gt;&lt;/code&gt;. It’s optimized for responsive fixed width/height images and images that stretch the full-width of a container. There are also other ways to work with images in Gatsby that don’t require GraphQL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;gatsby-image&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loads the optimal image size for each device size and screen resolution&lt;/li&gt;
&lt;li&gt;holds the image in a solid position while your page loads so the elements on the screen don't jump around&lt;/li&gt;
&lt;li&gt;shows a blur effect on images before they are fully loaded&lt;/li&gt;
&lt;li&gt;lazy loads images.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two types of responsive images supported by &lt;code&gt;gatsby-image&lt;/code&gt;, &lt;code&gt;fixed&lt;/code&gt;, and &lt;code&gt;fluid&lt;/code&gt;. &lt;code&gt;fixed&lt;/code&gt; is for images with a fixed &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt;. &lt;code&gt;fluid&lt;/code&gt; is for images that stretch across a fluid container.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;image.js&lt;/code&gt;, you'll find that the return value is either stating the picture wasn't found, or the image with its specified responsive type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useStaticQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="s2"&gt;`
    query {
      placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
        childImageSharp {
          fluid(maxWidth: 300) {
            ...GatsbyImageSharpFluid
          }
        }
      }
    }
  `&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;placeholderImage&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;childImageSharp&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;fluid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Picture&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Img&lt;/span&gt; &lt;span class="nx"&gt;fluid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;placeholderImage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childImageSharp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fluid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the query and the &lt;code&gt;return&lt;/code&gt; specify what type of image it will be. You as a developer will get to choose which kind it is.&lt;/p&gt;

&lt;p&gt;Overall, &lt;code&gt;header.js&lt;/code&gt; just contains what is in the &lt;code&gt;header&lt;/code&gt; element. &lt;code&gt;layout.js&lt;/code&gt; contains the &lt;code&gt;Header&lt;/code&gt; component as well as the rest of the &lt;code&gt;body&lt;/code&gt;. &lt;code&gt;SEO&lt;/code&gt; contains what is in the &lt;code&gt;head&lt;/code&gt;. &lt;code&gt;index.js&lt;/code&gt; loads &lt;code&gt;SEO&lt;/code&gt; and &lt;code&gt;Layout&lt;/code&gt;, the &lt;code&gt;head,&lt;/code&gt; and the &lt;code&gt;body&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;gatsby.js&lt;/code&gt; files
&lt;/h3&gt;

&lt;p&gt;In the root directory of your project, you'll find four &lt;code&gt;gatsby.js&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.com/docs/api-files-gatsby-browser/"&gt;&lt;code&gt;gatsby-browser.js&lt;/code&gt;&lt;/a&gt; is where you can respond to events within the browser and can wrap your site in additional components.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.com/docs/gatsby-config/"&gt;&lt;code&gt;gatsby-config.js&lt;/code&gt;&lt;/a&gt; is where you can set the configurations options for your site. Some things you can configure are &lt;code&gt;siteMetaData&lt;/code&gt;(where you can store common pieces of data across pages for reuse), &lt;code&gt;plugins&lt;/code&gt;, &lt;code&gt;pathPrefix&lt;/code&gt;, &lt;code&gt;Polyfill&lt;/code&gt;(Gatsby uses the ES6 Promise and not all browsers support it, so Gatsby includes Polyfill by default), etc.&lt;/p&gt;

&lt;p&gt;The code in &lt;a href="https://www.gatsbyjs.com/docs/api-files-gatsby-node/"&gt;&lt;code&gt;gatsby-node.js&lt;/code&gt;&lt;/a&gt; is run once in the process of building your site. You can use it to dynamically create pages, add Graphql Nodes, or respond to events during the build lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gatsbyjs.com/docs/api-files-gatsby-ssr/"&gt;&lt;code&gt;gatsby-ssr.js&lt;/code&gt;&lt;/a&gt; correlates with Server-Side-Rendering. SSR is where the server renders a web page, then sends it to the browser, instead of letting the browser render the web page. This file will let you alter the content of static HTML files while they are being rendered by the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Graphql&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://graphql.org/"&gt;&lt;strong&gt;Graphql&lt;/strong&gt;&lt;/a&gt; is a query language developed by Facebook. It doesn't interact with a database, it interacts with APIs. Queries allow you to get all the information you need inside of a single request.&lt;/p&gt;

&lt;p&gt;Gatsby uses Graphql to interact with local files. This allows you to reuse common pieces of data.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;import { useStaticQuery, graphql } from "gatsby";&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;There are two types of queries you can use in Gatsby, static and page queries.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useStaticQuery&lt;/code&gt; is a &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React Hook&lt;/a&gt; that is used to query data with Graphql at build time. React Hooks let you use state and other React features without writing a class.&lt;/p&gt;

&lt;p&gt;React Hooks do not work within classes. You can also build your own hooks. Hooks let you use state outside of a class. React preserves the state between re-renders. More on hooks here: &lt;a href="https://reactjs.org/docs/hooks-overview.html"&gt;Hooks Overview&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useStaticQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="s2"&gt;`
    query SiteTitleQuery {
      site {
        siteMetadata {
          title
        }
      }
    }
  `&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When generated with the default starter, Gatsby configures this variable for you in Layout.js. It assigns the query to the variable &lt;code&gt;data&lt;/code&gt;. This query executes during build time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;graphql&lt;/code&gt; is a Gatsby tag that enables page components to retrieve data from a Graphql query. &lt;code&gt;query&lt;/code&gt; is the operation type. In Graphql, there are &lt;code&gt;query&lt;/code&gt;, &lt;code&gt;mutation&lt;/code&gt;, and &lt;code&gt;subscription&lt;/code&gt; types. &lt;code&gt;SiteTitleQuery&lt;/code&gt; is the name of the query. The name of your query can be omitted, but it is helpful to include when it comes to debugging.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;query&lt;/span&gt; &lt;span class="nx"&gt;SiteTitleQuery&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;site&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;siteMetadata&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;title&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;site&lt;/code&gt; will be the beginning key of the query, it isn't referencing a key in &lt;code&gt;gatsby-config.js&lt;/code&gt;. The data we are asking for from &lt;code&gt;gatsby-config.js&lt;/code&gt; is &lt;code&gt;title&lt;/code&gt; from &lt;code&gt;siteMetadata&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In JavaScript, object properties can be accessed using dot notation. We can access the results of the query with &lt;code&gt;data.site.siteMetadata?.title&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Header&lt;/span&gt; &lt;span class="nx"&gt;siteTitle&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s2"&gt;`Title`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you haven't seen that question mark before, it is the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining"&gt;optional chaining&lt;/a&gt; operator. &lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>graphql</category>
    </item>
    <item>
      <title>Simple Recursion &amp; Challenges(ruby, js, python)</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Tue, 06 Oct 2020 10:21:55 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/simple-recursion-challenges-ruby-js-python-55j7</link>
      <guid>https://dev.to/ethanmgustafson/simple-recursion-challenges-ruby-js-python-55j7</guid>
      <description>&lt;p&gt;What is &lt;a href="https://en.wikipedia.org/wiki/Recursion"&gt;&lt;strong&gt;Recursion&lt;/strong&gt;&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Recursion is where a function calls itself from within the function.&lt;/p&gt;

&lt;p&gt;Example: String Reversal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How is it able to call itself from within the function and keep the return values? It stores each call into the Call Stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Call Stack&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Call_stack"&gt;&lt;strong&gt;&lt;em&gt;Call Stack&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; is a &lt;a href="https://en.wikipedia.org/wiki/Stack_(abstract_data_type)"&gt;&lt;strong&gt;&lt;em&gt;Stack&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; Data Structure that stores an ordered series of items just like a list. When you think of an array, you can add and remove an item at any index. With a stack, the first item added is the last item to be removed.&lt;/p&gt;

&lt;p&gt;When you go past the maximum call stack size, it is called &lt;strong&gt;Stack Overflow&lt;/strong&gt;. If you don't include a condition in a method in order to stop a recursive call, it will loop on endlessly, adding on more calls to the call stack, until memory runs out.&lt;/p&gt;

&lt;p&gt;This is a problem with Recursion. The computer needs to allocate memory for things. You can run out of memory when the call stack size is exceeded. In order to stop this from happening, you need to include a base case.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Base Case&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are two things you have to do in a recursive function. You have to have a recursive call and a base case.&lt;/p&gt;

&lt;p&gt;The base case is a condition set in the function determining when the recursive call should stop. &lt;/p&gt;

&lt;p&gt;There is one problem when it comes to returning values. Remember that the first item added to a stack is the last item removed from the stack. Once the base case condition is met, the stack will remove items, starting with the last item added to the call stack.&lt;/p&gt;

&lt;p&gt;So let's say we don't explicitly return a value we want to receive in JavaScript. It will get to the last recursive call, return the value, then will start popping off the rest of the calls within the call stack. The value you wanted to get will not be there, once you get to the final return.&lt;/p&gt;

&lt;p&gt;You would have to explicitly return the recursive call in order to get the correct value. The return value of all recursive calls would be the final recursive call return value.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Duplicate Letter Challenge&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you are up to it, here are some recursion challenges. The JavaScript Solution details the process I took in solving the challenge, as well as a way to solve it without Recursion.&lt;/p&gt;

&lt;p&gt;Guidelines:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You must use Recursion.&lt;/li&gt;
&lt;li&gt;You must take in a string as an argument to the function.&lt;/li&gt;
&lt;li&gt;The return value must be a Hash(Ruby)/Object(JS)/Dictionary(Python), containing keys of each letter in the string.&lt;/li&gt;
&lt;li&gt;The value to each key will be how many times the key appears in the string.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/GoodGuyGuf/ruby-js-algorithms/blob/main/duplicate_letter_string/DupilcateLetter.js"&gt;&lt;strong&gt;JavaScript Solution&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/GoodGuyGuf/ruby-js-algorithms/blob/main/duplicate_letter_string/duplicate_letter.rb"&gt;&lt;strong&gt;Ruby Solution&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/GoodGuyGuf/ruby-js-algorithms/blob/main/duplicate_letter_string/duplicate_letter.py"&gt;&lt;strong&gt;Python Solution&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ruby</category>
      <category>javascript</category>
      <category>python</category>
    </item>
    <item>
      <title>C - Environment Configuration(VS Code, macOS)</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 30 Sep 2020 00:07:21 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/c-environment-configuration-vs-code-macos-30ga</link>
      <guid>https://dev.to/ethanmgustafson/c-environment-configuration-vs-code-macos-30ga</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Table of Contents:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Program Execution Process&lt;/li&gt;
&lt;li&gt;The Linking Process&lt;/li&gt;
&lt;li&gt;Environment Configuration With VS Code/Clang&lt;/li&gt;
&lt;li&gt;Running Code In The Terminal&lt;/li&gt;
&lt;li&gt;Visual Studio Code&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;In this blog, I will walk through configuring your environment to function with &lt;strong&gt;C&lt;/strong&gt; in &lt;strong&gt;VS Code&lt;/strong&gt; on &lt;strong&gt;macOS&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When you begin configuring your working environment for &lt;strong&gt;C&lt;/strong&gt;, you will learn more about computer architecture, program execution, file types, the IDE, and what the compiler will be doing.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Program Execution Process&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A computer needs to be told what to do. It needs instructions to follow. The &lt;strong&gt;instruction set&lt;/strong&gt; is the bridge between software and hardware. It lives on the CPU. &lt;/p&gt;

&lt;p&gt;The CPU is the brain of the computer. It executes the instructions it receives. The only instructions it can understand are in machine language.&lt;/p&gt;

&lt;p&gt;In order for us to send instructions to the CPU in the C programming language, we need to compile source code. Source code is what is written in a text editor using a programming language.&lt;/p&gt;

&lt;p&gt;During compilation, the source code is converted into object code. Object code is a portion of machine code. Machine code is a program written in machine language. Machine language is binary.&lt;/p&gt;

&lt;p&gt;Resources: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Instruction_set_architecture" rel="noopener noreferrer"&gt;&lt;strong&gt;Instruction Set Architecture&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Linker&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After the source code has been compiled into object code, your program will go through the &lt;em&gt;"linking"&lt;/em&gt; process. &lt;/p&gt;

&lt;p&gt;This is where the compiler will link together object modules and external libraries into your program. It's about gathering all dependencies and linking them together in your code in order to produce an executable file.&lt;/p&gt;

&lt;p&gt;It will also display errors if there was a problem linking with your program. For example, it would report an error if a certain library was missing or couldn't be found.&lt;/p&gt;

&lt;p&gt;The final result of the linking process will be an executable file. In Windows, executable files end with &lt;code&gt;.exe&lt;/code&gt;. Unix won't have that file extension but the program can be executed with the path to your executable.&lt;/p&gt;

&lt;p&gt;Resources: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Linker_(computing)" rel="noopener noreferrer"&gt;&lt;strong&gt;Linker&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Configuring environment for C on macOS with VS Code &amp;amp; Clang&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Terminal&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you would like to run your code without an IDE on macOS, you will only need a Text Editor, Compiler, and a Terminal.&lt;/p&gt;

&lt;p&gt;If you already have &lt;a href="https://apps.apple.com/us/app/xcode/id497799835?mt=12" rel="noopener noreferrer"&gt;XCode&lt;/a&gt; installed, you will have a C compiler. If not, you can install clang like this: &lt;code&gt;xcode-select --install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create and run a simple program:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a simple &lt;code&gt;hello_world.c&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Running &lt;code&gt;gcc hello_world.c&lt;/code&gt; or &lt;code&gt;/usr/bin/clang hello_world.c&lt;/code&gt; will compile the file&lt;/li&gt;
&lt;li&gt;Run the executable with &lt;code&gt;./hello_world&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Visual Studio Code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It is easier to use an Integrated Development Environment (IDE) instead. You will have to install 2 things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/docs/setup/mac" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools" rel="noopener noreferrer"&gt;C/C++ Intellisense extension&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then we will begin configuring the VS Code IDE. We will have to create a few &lt;code&gt;.json&lt;/code&gt; files through VS Code.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Source Code
&lt;/h4&gt;

&lt;p&gt;For this configuration walkthrough, we will be creating a simple 'Hello World' program. Create a new project directory and name it however you wish. Open the directory in VS Code.&lt;/p&gt;

&lt;p&gt;I created another &lt;code&gt;Hello_World&lt;/code&gt; directory inside of the project folder, but you don't need to. Create a &lt;code&gt;Hello_World.c&lt;/code&gt; file inside of the &lt;code&gt;Hello_World&lt;/code&gt;. Place the following code inside of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Before you begin the next sections of writing the &lt;code&gt;json&lt;/code&gt; files, please read the &lt;a href="https://code.visualstudio.com/docs/editor/variables-reference" rel="noopener noreferrer"&gt;&lt;code&gt;tasks.json&lt;/code&gt;/&lt;code&gt;launch.json&lt;/code&gt; Variables Reference&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The Compiler Path (&lt;code&gt;c_cpp_properties.json&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;In order to ensure that the IDE can find the compiler, we have to set up a &lt;code&gt;c_cpp_properties.json&lt;/code&gt; file to link the compiler path.&lt;/p&gt;

&lt;p&gt;To do that, open the &lt;code&gt;Command Palette&lt;/code&gt; either by going to the &lt;code&gt;view&lt;/code&gt; tab at the top or by running &lt;code&gt;Cmd + Shift + P&lt;/code&gt;. Search for &lt;code&gt;c/c++&lt;/code&gt; and find &lt;code&gt;Edit configurations UI&lt;/code&gt;. Mac will most likely have found the compiler path for you, but if not it will be located in &lt;code&gt;/usr/bin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1F89_wXuEezZfv4GFQQtDPuVI7zrxb1eS" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1F89_wXuEezZfv4GFQQtDPuVI7zrxb1eS" alt="Compiler Path"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, scroll down to the IntelliSense mode:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1-N8DXDdsr8VWNI9IX7AmqOcHg24e21AK" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1-N8DXDdsr8VWNI9IX7AmqOcHg24e21AK" alt="IntelliSense"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure you have the right mode selected. It should be &lt;code&gt;clang-x64&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once you have finished settings those, it will create the &lt;code&gt;c_cpp_properties.json&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D13KhHHvBnCJwQJG49ThnqWSPBAK1P2fax" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D13KhHHvBnCJwQJG49ThnqWSPBAK1P2fax" alt="c_cpp_properties.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The Compiler Build Settings (&lt;code&gt;tasks.json&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;Each and every time you begin a new program, you have to configure these build settings in a project's root directory. They are specific to your program.&lt;/p&gt;

&lt;p&gt;Enter the command palette and search for &lt;code&gt;tasks&lt;/code&gt;. Find &lt;code&gt;Configure Default Build Task&lt;/code&gt;. Choose &lt;code&gt;Create tasks.json from template file&lt;/code&gt;, then select &lt;code&gt;Others&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1fkBLrZ_RBK-yPys0WhQTI5-eLPQIrX83" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1fkBLrZ_RBK-yPys0WhQTI5-eLPQIrX83" alt="tasks.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;command&lt;/code&gt; will be &lt;code&gt;"command": "/usr/bin/clang"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;"args"&lt;/code&gt; will have a couple of options: &lt;code&gt;-g&lt;/code&gt;, which is the compilation command for global, &lt;code&gt;-o&lt;/code&gt;, which will generate an object file, after that we name the executable as &lt;code&gt;"${file}"&lt;/code&gt;, which is the currently opened file, then after that, we specify the source files.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;"group"&lt;/code&gt; will specify &lt;code&gt;"kind": "build"&lt;/code&gt; &amp;amp; &lt;code&gt;"isDefault": true&lt;/code&gt;. &lt;code&gt;"kind"&lt;/code&gt; specifies that the task will run when you run &lt;code&gt;cmd + shift + B&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Debugging Settings (&lt;code&gt;launch.json&lt;/code&gt;)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;launch.json&lt;/code&gt; is used to set up the debugging settings. To create the &lt;code&gt;launch.json&lt;/code&gt; file, enter the command palette, and search for &lt;code&gt;launch&lt;/code&gt;. Select &lt;code&gt;"Debug: Open launch.json"&lt;/code&gt;. Select the C++ (GDB/LLDB) environment.&lt;/p&gt;

&lt;p&gt;The only two things we will change are the &lt;code&gt;program&lt;/code&gt; and &lt;code&gt;stopAtEntry&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1QnIDra7VhY319JyoQDrsA7QmkRDhMtpH" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fexport%3Dview%26id%3D1QnIDra7VhY319JyoQDrsA7QmkRDhMtpH" alt="launch.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;program&lt;/code&gt; will specify the executable we want to run.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stopAtEntry&lt;/code&gt; will be set to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Compiling/Executing The Program
&lt;/h4&gt;

&lt;p&gt;To compile your program, run &lt;code&gt;cmd + shift + B&lt;/code&gt;. This will create the executable and the debugging folder called &lt;code&gt;HelloWorld.dSYM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To execute the program, simply run the path to the executable. Since my program executable is inside of another directory of my project directory, I have to run the program as &lt;code&gt;Hello_World/HelloWorld&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/docs/cpp/config-clang-mac" rel="noopener noreferrer"&gt;&lt;strong&gt;VS Code Clang Walkthrough&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Clang" rel="noopener noreferrer"&gt;&lt;strong&gt;Clang Compiler Wiki&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://clang.llvm.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;clang.llvm.org&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/LLVM" rel="noopener noreferrer"&gt;&lt;strong&gt;LLVM Wiki&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://llvm.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;llvm.org&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gcc.gnu.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;gcc.gnu.org&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/GNU_Compiler_Collection" rel="noopener noreferrer"&gt;&lt;strong&gt;GNU Compiler Collection Wiki&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Intro To Learning C</title>
      <dc:creator>Ethan Gustafson</dc:creator>
      <pubDate>Wed, 23 Sep 2020 03:58:38 +0000</pubDate>
      <link>https://dev.to/ethanmgustafson/intro-to-learning-c-20eo</link>
      <guid>https://dev.to/ethanmgustafson/intro-to-learning-c-20eo</guid>
      <description>&lt;p&gt;&lt;strong&gt;Table of Contents:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Understanding Terms&lt;/li&gt;
&lt;li&gt;Interpreters, Compilers, Assemblers&lt;/li&gt;
&lt;li&gt;Programming Language Levels&lt;/li&gt;
&lt;li&gt;Procedural, Object-Oriented, and Functional Programming&lt;/li&gt;
&lt;li&gt;What is a Scripting Language?&lt;/li&gt;
&lt;li&gt;Dynamic Typing&lt;/li&gt;
&lt;li&gt;Garbage Collection&lt;/li&gt;
&lt;li&gt;What is C?&lt;/li&gt;
&lt;li&gt;Why learn the C programming language?&lt;/li&gt;
&lt;li&gt;Conclusion &amp;amp; Next Steps&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Introduction:&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;The first programming languages I messed around with were &lt;strong&gt;C++&lt;/strong&gt; and &lt;strong&gt;Python&lt;/strong&gt; back in early 2018. Back then I was slowly weighing my options as to how I would begin learning to code, so I never fully learned these two languages.&lt;/p&gt;

&lt;p&gt;Fast forward to October 2019 and I'm enrolled in the online Flatiron School Software Engineering program. I was taught &lt;strong&gt;Procedural&lt;/strong&gt; and &lt;strong&gt;Object-Oriented Programming&lt;/strong&gt; with &lt;strong&gt;Ruby&lt;/strong&gt; in the first month. I believe it was a really great way to start learning how to program.&lt;/p&gt;

&lt;p&gt;I will talk a decent amount about both &lt;strong&gt;Ruby&lt;/strong&gt; and &lt;strong&gt;C&lt;/strong&gt;. Although I know programming fundamentals, procedural, OOP &amp;amp; functional programming principles, I don't really know how Ruby works behind the scenes. I don't believe that ultimately makes me a great software engineer.&lt;/p&gt;

&lt;p&gt;So how can I learn more about computer science, computer architecture, program execution, and how high-level programming languages work behind the scenes as well as practice more data structure and algorithm challenges?&lt;/p&gt;

&lt;p&gt;Learning C offers the great opportunity of learning everything above all at once. But before we can learn how to use the C language, we have to confirm what we know and what we should know moving forward about programming and computer science.&lt;/p&gt;

&lt;p&gt;In my previous blogs, I would only begin to write my drafts after I finished learning and programming. This time, I will be writing out the step-by-step process I am taking to &lt;strong&gt;learn C in real-time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I am going to be &lt;strong&gt;&lt;em&gt;direct&lt;/em&gt;&lt;/strong&gt; with terms, concepts, and code as you read onward and will reference where the information comes from. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Understanding Terms&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Ruby_(programming_language)"&gt;&lt;strong&gt;Ruby&lt;/strong&gt;&lt;/a&gt; itself was actually written in the &lt;a href="https://en.wikipedia.org/wiki/C_(programming_language)"&gt;&lt;strong&gt;C programming language&lt;/strong&gt;&lt;/a&gt; by Yukihiro "Matz" Matsumoto in the 1990s. It is an &lt;strong&gt;interpreted high-level general-purpose scripting programming&lt;/strong&gt; language. It is also &lt;a href="https://en.wikipedia.org/wiki/Dynamic_programming_language"&gt;&lt;strong&gt;dynamically typed&lt;/strong&gt;&lt;/a&gt; and uses &lt;a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)"&gt;&lt;strong&gt;garbage collection&lt;/strong&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Did that sound like a lot? &lt;strong&gt;What do those terms &lt;em&gt;really&lt;/em&gt; mean?&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Interpreters, Compilers, Assemblers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Compiler"&gt;&lt;strong&gt;Compiler&lt;/strong&gt;&lt;/a&gt; is a computer program that translates code written in a programming language &lt;em&gt;into another&lt;/em&gt; programming language. There are many different types of compilers. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The compiler for &lt;strong&gt;C&lt;/strong&gt; will be translating &lt;strong&gt;C&lt;/strong&gt; source code &lt;em&gt;into&lt;/em&gt; &lt;strong&gt;&lt;em&gt;object code&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Object_code"&gt;&lt;strong&gt;Object code&lt;/strong&gt;&lt;/a&gt; is the code produced by a compiler and is &lt;strong&gt;unrelated&lt;/strong&gt; to Object-Oriented Programming. This doesn't really describe what object code is, though. It is a portion of &lt;strong&gt;machine code&lt;/strong&gt; that has &lt;strong&gt;&lt;em&gt;not yet&lt;/em&gt;&lt;/strong&gt; been linked into a complete program.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://simple.wikipedia.org/wiki/Machine_code"&gt;&lt;strong&gt;Machine Code&lt;/strong&gt;&lt;/a&gt; is a computer program written in machine language, which is &lt;a href="https://en.wikipedia.org/wiki/Binary_code"&gt;&lt;strong&gt;Binary&lt;/strong&gt;&lt;/a&gt;. Machine Code is the lowest level of software.&lt;/li&gt;
&lt;li&gt;Object Code has to be placed inside of &lt;a href="https://en.wikipedia.org/wiki/Executable"&gt;Executable&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Library_(computing)"&gt;Library&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Object_file"&gt;Object Files&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Interpreter_(computing)"&gt;&lt;strong&gt;Interpreter&lt;/strong&gt;&lt;/a&gt; is a computer program that &lt;em&gt;directly&lt;/em&gt; executes instructions from a programming or scripting language &lt;strong&gt;&lt;em&gt;without&lt;/em&gt;&lt;/strong&gt; having them previously compiled into a machine language program first. There are a number of strategies the interpreter utilizes in order to do this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The terms '&lt;a href="https://en.wikipedia.org/wiki/Interpreted_language"&gt;&lt;strong&gt;interpreted language&lt;/strong&gt;&lt;/a&gt;' and '&lt;a href="https://en.wikipedia.org/wiki/Compiled_language"&gt;&lt;strong&gt;compiled language&lt;/strong&gt;&lt;/a&gt;' effectively mean that the implementation of that language is an interpreter or a compiler, respectively."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, the &lt;strong&gt;Ruby Interpreter&lt;/strong&gt; is &lt;a href="https://en.wikipedia.org/wiki/YARV"&gt;&lt;strong&gt;YARV&lt;/strong&gt;&lt;/a&gt; which is a &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Bytecode"&gt;bytecode&lt;/a&gt; interpreter&lt;/em&gt; that was created to greatly reduce Ruby program execution times.&lt;/p&gt;

&lt;p&gt;Before YARV, the Ruby interpreter was &lt;strong&gt;MRI&lt;/strong&gt;. The MRI interpreter was written in C.&lt;/p&gt;




&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Assembly_language#Assembler"&gt;&lt;strong&gt;Assembler&lt;/strong&gt;&lt;/a&gt; is a computer program that creates Object Code by &lt;a href="https://en.wikipedia.org/wiki/Mnemonic"&gt;&lt;strong&gt;mnemonics&lt;/strong&gt;&lt;/a&gt; and &lt;strong&gt;syntax&lt;/strong&gt; for &lt;em&gt;operations and addressing modes&lt;/em&gt; &lt;strong&gt;into&lt;/strong&gt; their &lt;strong&gt;&lt;em&gt;equivalent numerical value.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Unix"&gt;&lt;strong&gt;Unix&lt;/strong&gt;&lt;/a&gt; is also written in &lt;strong&gt;C&lt;/strong&gt; along with &lt;strong&gt;Assembly&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Programming Language Levels&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;What's all this about language levels? There are &lt;strong&gt;&lt;em&gt;two kinds&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.) &lt;a href="https://en.wikipedia.org/wiki/Low-level_programming_language"&gt;&lt;strong&gt;Low-level&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wikipedia says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A &lt;strong&gt;low-level&lt;/strong&gt; programming language is a programming language that provides little or no abstraction from a computer's instruction set architecture—commands or functions in the language map closely to processor instructions. Generally, this refers to either &lt;strong&gt;machine code&lt;/strong&gt; or &lt;strong&gt;assembly language&lt;/strong&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What this means is that you really are writing everything from scratch. Usually, &lt;strong&gt;low-level&lt;/strong&gt; languages are used to write software &lt;strong&gt;&lt;em&gt;directly&lt;/em&gt;&lt;/strong&gt; for specific hardware, so they aren't portable. This means you &lt;strong&gt;cannot&lt;/strong&gt; use the same software in different environments. For example, &lt;strong&gt;Unics&lt;/strong&gt;(Unix) was first written in Assembly. It would only function on the type of hardware it was designed for. Unix wasn't portable until it was rewritten in &lt;strong&gt;C&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Low-level languages do not require a compiler or interpreter. They can convert to machine code themselves.&lt;/p&gt;

&lt;p&gt;2.) &lt;a href="https://en.wikipedia.org/wiki/High-level_programming_language"&gt;&lt;strong&gt;High-level&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;High-level programming languages are designed so that you can focus on the problem at hand without worrying about lower-level processes. They make the code look as human-readable as possible while abstracting away work you would have to do with machine language.&lt;/p&gt;

&lt;p&gt;Let's say I created a custom &lt;code&gt;Person&lt;/code&gt; class that initializes objects with two attributes: &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;skill&lt;/code&gt;. This is how you can create an 'instance of' a &lt;code&gt;Class&lt;/code&gt; in &lt;strong&gt;Ruby&lt;/strong&gt;, using the &lt;code&gt;new&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="n"&gt;ethan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Ethan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;skill: &lt;/span&gt;&lt;span class="s2"&gt;"Ruby"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new object containing my data. This is very easy to understand. I told it what to do using the Ruby syntax and it did it. &lt;/p&gt;

&lt;p&gt;But how was that object stored into memory? Where was it stored? What enabled it to be converted into a language the computer could read? Where was it sent in order to be executed? What happens to this data when a program is done using it?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://cs.lmu.edu/~ray/notes/pltypes/"&gt;Categories of Programming Languages&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Procedural, Object-Oriented, and Functional Programming&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Programming_paradigm"&gt;&lt;strong&gt;Programming Paradigm&lt;/strong&gt;&lt;/a&gt; is a way to classify languages based on their features. A programming language can have many paradigms.&lt;/p&gt;

&lt;p&gt;Wikipedia's description says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"&lt;a href="https://en.wikipedia.org/wiki/Procedural_programming"&gt;&lt;strong&gt;Procedural Programming&lt;/strong&gt;&lt;/a&gt; is a programming paradigm, derived from &lt;a href="https://en.wikipedia.org/wiki/Structured_programming"&gt;&lt;strong&gt;structured programming&lt;/strong&gt;&lt;/a&gt;, based on the concept of the &lt;a href="https://en.wikipedia.org/wiki/Subroutine"&gt;&lt;strong&gt;procedure call&lt;/strong&gt;&lt;/a&gt;. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution, including by other procedures or itself."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically, procedural programming involves executing a series of steps in order to complete a task.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;&lt;strong&gt;Object-Oriented Programming&lt;/strong&gt;&lt;/a&gt; is a programming paradigm based on the concept of &lt;strong&gt;objects&lt;/strong&gt;. An object can have an &lt;strong&gt;identity&lt;/strong&gt;, &lt;strong&gt;attributes&lt;/strong&gt;, and &lt;strong&gt;behaviors&lt;/strong&gt;. OOP Principles involve &lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;&lt;strong&gt;S.O.L.I.D&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/GRASP_(object-oriented_design)"&gt;&lt;strong&gt;G.R.A.S.P&lt;/strong&gt;&lt;/a&gt; principles.&lt;/p&gt;

&lt;p&gt;Ruby is an &lt;strong&gt;object-oriented&lt;/strong&gt;, &lt;strong&gt;procedural&lt;/strong&gt; and &lt;strong&gt;functional&lt;/strong&gt; programming language.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional Programming&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"&lt;a href="https://en.wikipedia.org/wiki/Functional_programming"&gt;&lt;strong&gt;Functional Programming&lt;/strong&gt;&lt;/a&gt; is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements that change the state of the program."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A lot of functional programming is taught in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://en.wikipedia.org/wiki/Dynamic_programming_language"&gt;&lt;strong&gt;Dynamic Typing&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Ruby is dynamically typed. "A dynamically typed language is a class of high-level programming languages, which at runtime execute common programming behaviors that static programming languages perform during compilation."&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)"&gt;&lt;strong&gt;Garbage Collection&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Garbage collection is about memory management. The collector will remove memory from objects that are no longer used by a program. You don't have to manually garbage collect in Ruby. That is done for you behind the scenes.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is a Scripting Language?&lt;/strong&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Scripting_language"&gt;scripting&lt;/a&gt; or script language is a programming language for a special run-time environment that automates the execution of tasks;[1] the tasks could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted, rather than compiled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is C?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Wikipedia says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;C&lt;/strong&gt; is a high-level &lt;a href="https://en.wikipedia.org/wiki/Imperative_programming"&gt;imperative&lt;/a&gt; procedural programming language. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Imperative programming&lt;/strong&gt; is a paradigm where a developer will explicitly describe the actions a program will take.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Declarative programming&lt;/strong&gt; is a paradigm where a developer will describe what a program will accomplish, rather than describing &lt;em&gt;how&lt;/em&gt; it will accomplish it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;C is high-level compared to Assembly. C is considered low-level compared to Ruby. This is why when you search the internet what level C is on, it will tell you at times that it is low-level or high-level.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why learn the C programming language?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Most operating systems are written in C languages. This gives you a better understanding when you approach learning what your operating system can do.&lt;/li&gt;
&lt;li&gt;Many programming languages are written in C. Learning it will help you become a better developer in other languages.&lt;/li&gt;
&lt;li&gt;Since C isn't object-oriented, you can fully focus on procedural programming.&lt;/li&gt;
&lt;li&gt;It will help you learn computer architecture.&lt;/li&gt;
&lt;li&gt;You will understand how high-level languages operate and how programs execute. &lt;/li&gt;
&lt;li&gt;You can run C in Ruby and you can run Ruby in C. The &lt;a href="https://silverhammermba.github.io/emberb/c/"&gt;&lt;strong&gt;Ruby C API&lt;/strong&gt;&lt;/a&gt; details how you can &lt;a href="https://silverhammermba.github.io/emberb/extend/"&gt;&lt;strong&gt;run C in Ruby&lt;/strong&gt;&lt;/a&gt; and how you can &lt;a href="https://silverhammermba.github.io/emberb/embed/"&gt;&lt;strong&gt;run Ruby in C&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;C's execution time is very fast.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;That was a lot! What I learned at Flatiron is starting to make more sense. Now with that out of the way I feel more than ready to dive into learning the language.&lt;/p&gt;

&lt;p&gt;The next blog in this series will detail &lt;strong&gt;how to configure your C environment with VS Code on macOS&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>c</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
