<?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: critwitt</title>
    <description>The latest articles on DEV Community by critwitt (@critwitt).</description>
    <link>https://dev.to/critwitt</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%2F913652%2Fee27cd8c-802d-427c-94e6-67fde5b69254.png</url>
      <title>DEV Community: critwitt</title>
      <link>https://dev.to/critwitt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/critwitt"/>
    <language>en</language>
    <item>
      <title>Handling Errors in Ruby</title>
      <dc:creator>critwitt</dc:creator>
      <pubDate>Mon, 24 Oct 2022 01:25:24 +0000</pubDate>
      <link>https://dev.to/critwitt/handling-errors-in-ruby-5f6k</link>
      <guid>https://dev.to/critwitt/handling-errors-in-ruby-5f6k</guid>
      <description>&lt;p&gt;When working with Active Record within Ruby, utilizing CRUD operations can yield a multitude of errors. Some common errors are &lt;code&gt;ActiveRecord::RecordNotFound&lt;/code&gt;, &lt;code&gt;ActiveRecord::RecordInvalid&lt;/code&gt;, and &lt;code&gt;ActiveRecord::InvalidForeignKey&lt;/code&gt; among others. In this blog, we're going to go over some ways in which you can handle these errors in order to give the user more information about the errors they receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Utilizing &lt;code&gt;rescue_from&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A recommended way for error handling is utilizing Ruby's built in &lt;code&gt;rescue_from&lt;/code&gt; instance public method. Able to be placed right in your ApplicationController or in any specific class controller, &lt;code&gt;rescue_from&lt;/code&gt; in combination with a trailing &lt;code&gt;:with&lt;/code&gt; option allows for you to create a private method to handle errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationController &amp;lt; ActionController::Base
  rescue_from User::RecordNotFound, with: :not_found

  private

  def not_found
    render json: {error: "Record not found"}
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will render an error message to the user indicating that the record they were searching for does not exist. This can take care of some errors with &lt;code&gt;#show&lt;/code&gt; and &lt;code&gt;#destroy&lt;/code&gt; methods. But what if we're creating a new instance but our user does not pass our data validations? Wouldn't it be nice to tell our user what validations were not passed? Let's explore how we can handle that in our next section&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Validations Errors
&lt;/h2&gt;

&lt;p&gt;There's an important decision you have to make when creating data, and that's whether you want to use &lt;code&gt;create&lt;/code&gt; or &lt;code&gt;create!&lt;/code&gt;. Comparing the two, &lt;code&gt;create&lt;/code&gt; returns the object created by the user, while &lt;code&gt;create!&lt;/code&gt; returns a &lt;code&gt;RecordInvalid&lt;/code&gt; error.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;create&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class InstanceController &amp;lt; ApplicationController

  def create
    instance = InstanceClass.create(instance_params)
    if instance.valid?
      render json: instance, status: :created
    else
      render json: {errors: instance.errors }, status: :unprocessable_entity
      end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;create&lt;/code&gt; allows for a user to access the errors key to display all the errors associated with the object.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;create!&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class InstanceController &amp;lt; ApplicationController

  rescue_from ActiveRecord::RecordInvalid, with: :invalid

  def create
    instance = Instance.create!(instance_params)
    render json: instance, status: :created
  end

  private

  def invalid errors
    render json: {errors: errors}, status: :unprocessable_entity
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that &lt;code&gt;create!&lt;/code&gt; will raise a &lt;code&gt;RecordInvalid&lt;/code&gt; error, and that will be rendered for each invalid option. So that takes care of &lt;code&gt;#create&lt;/code&gt; errors in terms of validation.&lt;/p&gt;

&lt;p&gt;Using this guide, play around with Active Record and Postman and see if you can generate and debug your own errors. Happy Coding!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Fixing Errno::EIO: Input/Output Error when Running Sinatra for Ruby</title>
      <dc:creator>critwitt</dc:creator>
      <pubDate>Tue, 04 Oct 2022 05:11:00 +0000</pubDate>
      <link>https://dev.to/critwitt/fixing-errnoeio-inputoutput-error-when-running-sinatra-for-ruby-1cnh</link>
      <guid>https://dev.to/critwitt/fixing-errnoeio-inputoutput-error-when-running-sinatra-for-ruby-1cnh</guid>
      <description>&lt;p&gt;I came across and issue while working on my backend server using Sinatra with Ruby. When I would try to run my server locally on my machine, I would get an error on Sinatra's end saying Errno::EIO, and notifying me that there was an input/output error which was preventing me from fetching data from that endpoint. That error might occur because you're trying to access a port which is already in use. There's a lot of fascinating Computer Science that goes into ports but chances are you're just looking to fix this issue.&lt;/p&gt;

&lt;p&gt;In your terminal, you want to kill the port that's currently in use. For this example, I will use port 9292, but you should use whatever port you're trying to access.&lt;/p&gt;

&lt;p&gt;In your terminal, type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo kill -9 `sudo lsof -t -i:9292`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to use backticks, not apostrophes or quotes. If you're interested in exactly what you're typing, I'll direct you &lt;a href="https://explainshell.com/explain?%20cmd=sudo+kill+-9+%60sudo+lsof+-t+-i%3A9292%60"&gt;ExplainShell&lt;/a&gt;. If you're uninterested in the specifics just know this this is killing the open terminal.&lt;/p&gt;

&lt;p&gt;Now when you try to run your server again, Sinatra will work perfectly. Happy Coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Hooks: Using useState</title>
      <dc:creator>critwitt</dc:creator>
      <pubDate>Mon, 12 Sep 2022 02:55:54 +0000</pubDate>
      <link>https://dev.to/critwitt/react-hooks-using-usestate-57ci</link>
      <guid>https://dev.to/critwitt/react-hooks-using-usestate-57ci</guid>
      <description>&lt;p&gt;If you're a beginner with React, this blog post is aimed at familiarizing yourself with the benefits of using &lt;code&gt;useState&lt;/code&gt; to re-render component to your React App.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Hook?
&lt;/h2&gt;

&lt;p&gt;A Hook is a way of allowing a user to use stateful logic from a component without reorganizing your code or restructuring your hierarchy. Essentially, hooks allow for easier collaboration for people working on different components of a project. One of the most useful Hooks that's provided by React is &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does &lt;code&gt;useState&lt;/code&gt; do?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; allows a user to easily create a state variable and update it. First a user must declare the state variable and function which will update the state variable. Then a user will call &lt;code&gt;useState&lt;/code&gt; and set the initial state. (A side note about the initial state is that it can be any data type! More on the logistics of that below) See below for an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function App () {
  const [dog, setDog] = useState('');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see the &lt;code&gt;useState&lt;/code&gt; hook declares the state variable dogs and assigns it to an empty string. Now we can use a function which utilizes &lt;code&gt;setDog&lt;/code&gt; to update the state of &lt;code&gt;dog&lt;/code&gt;. The beauty of updating state in React is that this will trigger your app to re-render! Let's take a look at how we can utilize a function to set the value of dogs and re-render your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function App () {
  const [dog, setDog] = useState('');

  function addDogs () {
    setDogs('German Shepherd');
  }

  return (
    &amp;lt;p onClick={addDogs}&amp;gt;Click me to see my favorite dog: {dog} &amp;lt;/p&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a very simple example of how clicking this &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag will set the &lt;code&gt;dog&lt;/code&gt; state to &lt;code&gt;German Shepherd&lt;/code&gt; every single time. State variables can be updated in more complex ways according to the needs of your app. State variables can even be passed on to children components as props. This allows a user to update state in a child component which will trigger a re-render in the parent component. &lt;/p&gt;

&lt;h2&gt;
  
  
  A Note About Arrays and Objects
&lt;/h2&gt;

&lt;p&gt;In our example above, updating the variable &lt;code&gt;dog&lt;/code&gt; was very simple as clicking our &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag would only update it to a single value. When updating an array or object, it's important to not erase our previous data when we only want to update the last element in an array or a single key in an object. For this reason it's important to use the spread operator to maintain that previous state data. Take a look at the example below from w3schools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";
import ReactDOM from "react-dom/client";

function Car() {
  const [car, setCar] = useState({
    brand: "Ford",
    model: "Mustang",
    year: "1964",
    color: "red"
  });

  const updateColor = () =&amp;gt; {
    setCar(previousState =&amp;gt; {
      return { ...previousState, color: "blue" }
    });
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h1&amp;gt;My {car.brand}&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;
        It is a {car.color} {car.model} from {car.year}.
      &amp;lt;/p&amp;gt;
      &amp;lt;button
        type="button"
        onClick={updateColor}
      &amp;gt;Blue&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(&amp;lt;Car /&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see how the spread operator preserves the previous state so that the car color is updated but the car model and car year is unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is a very user friendly way of hooking into react state and updating the state to trigger a re-render. There are many different applications that &lt;code&gt;useState&lt;/code&gt; can utilize so get out there are start experimenting. Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>usestate</category>
    </item>
    <item>
      <title>Array Iteration for Beginners: What is forEach() and map()?</title>
      <dc:creator>critwitt</dc:creator>
      <pubDate>Tue, 23 Aug 2022 02:17:36 +0000</pubDate>
      <link>https://dev.to/critwitt/array-iteration-for-beginners-what-is-foreach-and-map-9lp</link>
      <guid>https://dev.to/critwitt/array-iteration-for-beginners-what-is-foreach-and-map-9lp</guid>
      <description>&lt;h2&gt;
  
  
  What is "iteration"?
&lt;/h2&gt;

&lt;p&gt;Iteration is the process of going through each element of an array and doing &lt;em&gt;something&lt;/em&gt;. There are a lot of reasons you might want to do &lt;em&gt;something&lt;/em&gt; to each element in an array. Luckily, JavaScript has a lot of ways to &lt;em&gt;do things&lt;/em&gt;! We're going to explore the two JavaScript iteration methods &lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; and figure out what they are, and when to use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to know before starting
&lt;/h2&gt;

&lt;p&gt;There are some considerations to make before iterating over an array. The first thing you want to ask yourself is what am I trying to accomplish through iteration? What exactly would I like to be returned to me after I have each element &lt;em&gt;do something&lt;/em&gt;? Let's see how &lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; can operate similarly but have different return values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;forEach()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;forEach()&lt;/code&gt; is very aptly named because it will take an array and perform some function &lt;em&gt;for each&lt;/em&gt; value in the array. If we want &lt;code&gt;forEach()&lt;/code&gt; to to perform some sort of &lt;em&gt;function&lt;/em&gt;, then it will have take a function as a parameter. This is called a &lt;strong&gt;callback function&lt;/strong&gt; and it will be used in both &lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt;. A callback function is the &lt;em&gt;something&lt;/em&gt; that is happening &lt;em&gt;for each&lt;/em&gt; element in the array! Take a look at the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const people = ["Mac", "Dennis", "Charlie", "Dee", "Frank"];
people.forEach(person =&amp;gt; console.log(`${person} is a my favorite!`);

// CONSOLE:
// Mac is my favorite!
// Dennis is my favorite!
// Charlie is my favorite!
// Dee is my favorite!
// Frank is my favorite!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's important to note about the return of this &lt;code&gt;forEach()&lt;/code&gt;? Well let's examine what's happening. For each person in the people array, a callback function returns a log of the person to the console. So the callback function is what's returning a value. So what does the &lt;code&gt;forEach()&lt;/code&gt; iteration method return? Well...nothing! It doesn't return a value it just calls upon a function to return a value &lt;em&gt;for each&lt;/em&gt; element in the array. We can check this by trying to log the &lt;code&gt;forEach()&lt;/code&gt; array to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const doubleNumbers = numbers.forEach(number =&amp;gt; number*2);
console.log(doubleNumbers);

// CONSOLE:
// undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's apparent that using &lt;code&gt;forEach()&lt;/code&gt; does not return a value of doubleNumbers to the console. What if we want those values returned to us so we can manipulate them further? Perhaps we can make use of &lt;code&gt;map()&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;map()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When you create a map of something, you recreate something on a different scale. Theoretically, a map of the United States should just be a smaller United States. That's essentially what we're doing with our &lt;code&gt;map()&lt;/code&gt; function! We're just changing &lt;em&gt;something&lt;/em&gt; about our array, but, as opposed to &lt;code&gt;forEach()&lt;/code&gt;, &lt;code&gt;map()&lt;/code&gt; returns a new array of the same size. Take a look at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const doubleNumbers = numbers.map(number =&amp;gt; number*2);
console.log(doubleNumbers);

// CONSOLE:
// [2, 4, 6, 8, 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that &lt;code&gt;map()&lt;/code&gt; takes a &lt;strong&gt;callback function&lt;/strong&gt; just like &lt;code&gt;forEach()&lt;/code&gt;. However, unlike &lt;code&gt;forEach()&lt;/code&gt; we see that an array &lt;code&gt;doubleNumbers&lt;/code&gt; is logged. This array is the returned value of each element after the callback function has been performed on that element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, so what?
&lt;/h2&gt;

&lt;p&gt;Well it's important to know if you want an array returned or if you just want some action performed to each element. There are instances where a new array is unnecessary because the array only exists for some action to be performed upon its elements. Alternatively, it can be very useful to return a new, updated array to use further in your code. It all depends on your uses. Explore the uses of &lt;code&gt;forEach()&lt;/code&gt; and &lt;code&gt;map()&lt;/code&gt; and see where they excel and where their limitations might lie. Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
