<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shaqqour on Medium]]></title>
        <description><![CDATA[Stories by Shaqqour on Medium]]></description>
        <link>https://medium.com/@shaqqour?source=rss-62f9f6293668------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KwpedNkUXgBWV8SV.jpg</url>
            <title>Stories by Shaqqour on Medium</title>
            <link>https://medium.com/@shaqqour?source=rss-62f9f6293668------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:40:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shaqqour/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Create a Backend Rails Application as an API]]></title>
            <link>https://shaqqour.medium.com/how-to-create-a-backend-rails-application-as-an-api-16d7d4661ad8?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/16d7d4661ad8</guid>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[ruby-on-rails-tutorial]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[rendering]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Fri, 05 Mar 2021 04:59:06 GMT</pubDate>
            <atom:updated>2021-03-05T04:59:06.925Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*JKUH0yexzVaiQjwIi_y5ew.png" /></figure><p>In one of my previous <a href="https://shaqqour.medium.com/scaffold-generator-in-rails-23a89bb4d690">blog posts</a>, I explained how to create a full Rails application using the Scaffold generator. However, in many cases, you will need Rails to serve as your backend application that renders APIs to the frontend application. In this blog post, I will show you how to create a backend Rails application as an API.</p><h4>Create a New Rails Application</h4><p>As always we will start by creating a new Rails application using rails newcommand. Let’s run the following command in the terminal:</p><pre>$ rails new rails-application</pre><p>Running this command will create a full Rails application that would also serve as a frontend application. We can continue this way, or we could choose a better way that would create what we need exactly which is only a Rails backend API application — without frontend components. Let’s choose the better way.</p><p>First, we will go back and delete what we just did using this command:</p><pre>$ rm -r rails-application</pre><p>Let’s rerun the rails new command, but now by specifying that it is an API application. Run the following command in your terminal:</p><pre>$ rails new rails-api-application --api</pre><p>Notice here that we added the --apiflag which tells Rails to create an API only application.<br>Note: Don’t get confused with the application name rails-api-applicationwhich is only the name of our project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/165/1*_5lmn2ka84zEizq6wTfSog.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*oc-y9aE0637_J317v05WFQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*VaWbW6B5Vipg2anp_A9_MQ.png" /><figcaption>Full rails application vs API only application</figcaption></figure><p>Notice the extra files and folders that the plain rails newcommand will create. While when we added the apiflag it creates only the backend structure for a rails application that renders APIs to the frontend.</p><p>Let’s create a model, perhaps movies, and try rendering movies API to the frontend.</p><h4>Create a Resource</h4><p>Let’s use the resource generator to create our model, migration, controller, and add a route to this resource. Run the following command in your terminal:</p><pre>$ rails g resource Movie title:string director:string year:string</pre><p>And then migrate the database changes to create the movie table by running the migrate command:</p><pre> $ rails db:migrate</pre><h4>Add Some Data</h4><p>Inside your db/seeds.rb, create some data so you can test and render some movies APIs. You can create your own, or copy-paste these three movies:</p><pre>movies = Movie.create([{ title: &#39;Star Wars&#39;, director: &quot;George Lucas&quot;, year: &quot;2008&quot; }, { title: &#39;Lord of the Rings&#39;, director: &quot;Peter Jackson&quot;, year: &quot;2001&quot; }, { title: &#39;E.T.&#39;, director: &quot;Steven Spielberg&quot;, year: &quot;1982&quot; } ])</pre><p>Run rails db:seed to seed your database table with these three movies.</p><h4>Rendering Movies as APIs</h4><p>Now that we have our movies, let’s try to render these movies as an API and check it out in the browser. We will be working on the controller now. Open app/controllers/movies_controller.rband let’s add an index action that would render all the movies.</p><pre>// app/controllers/<strong>movies_controller.rb</strong></pre><pre>class MoviesController &lt; ApplicationController</pre><pre>def index<br>        movies = Movie.all<br>        render json: movies<br>    end</pre><pre>end</pre><p>Here, we are getting all the movies we have in the movies table, and then render a JSON object that represents these movies. If you open your browser and go to <a href="http://localhost:3000/movies">http://localhost:3000/movies</a>, you should see something similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*_hvtpJ67izmVN6QTrhPq_w.png" /><figcaption>Movies API</figcaption></figure><p>That was how you create backend rails API application that renders a JSON object to be used later on in the frontend application. If you have anything you would like to add, please commit below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16d7d4661ad8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use React Router in Your React Application]]></title>
            <link>https://shaqqour.medium.com/how-to-use-react-router-in-your-react-application-693a03dc23bd?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/693a03dc23bd</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[front-end-developer]]></category>
            <category><![CDATA[react-router-dom]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sun, 28 Feb 2021 22:00:10 GMT</pubDate>
            <atom:updated>2021-02-28T22:00:10.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cjyDNtzQnWFbH_ko" /><figcaption>Photo by <a href="https://unsplash.com/@seb?utm_source=medium&amp;utm_medium=referral">Sébastien Jermer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>React Router is a library or a package to help you navigate between your React application components. As mentioned in <em>React Router’s Documentation,</em> “React Router is a collection of <strong>navigational components</strong> that compose declaratively with your application. Whether you want to have <strong>bookmarkable URLs</strong> for your web app or a composable way to navigate in <strong>React Native</strong>, React Router works wherever React is rendering — so take your pick!”</p><p>In this blog post, I will show you how to use React Router with a code along for a song library. Let’s get started:</p><h4>Create a New React Project</h4><p>This is an easy step. All you have to do is to run the convenient create-react-appcommand.</p><pre>$ npx create-react-app react-routes</pre><h4>Install the React-Router Package</h4><p>Now that we created the project, let’s install the react-router package by running the following command:</p><pre>$ npm install react-router-dom</pre><h4>Add Some Data</h4><p>After creating the project, inside ./srcdirectory, create a new file data.js to store some data that we will be using in this project.</p><pre>//   ./src/data.js</pre><pre>let songs = [<br>    {<br>        title: &#39;No Woman, No Cry&#39;,<br>        genres: [&#39;Romance&#39;, &#39;Fantasy&#39;],<br>    },<br>    {<br>        title: &#39;Is This Love&#39;,<br>        genres: [&#39;Romance&#39;, &#39;Comedy&#39;]<br>    },<br>    {<br>        title: &#39;Ya Agmal 3oyoon&#39;,<br>        genres: [&#39;Romance&#39;, &#39;Fantasy&#39;]<br>    }<br>]</pre><pre>let artists = [<br>    {<br>        name: &#39;Bob Marley&#39;,<br>        songs: [&#39;No Woman, No Cry&#39;, &#39;Is This Love&#39;]<br>    },<br>    {<br>        name: &#39;Amr Diab&#39;,<br>        songs: [&#39;Ya Agmal 3oyoon&#39;, &#39;Burg Al 7oot&#39;]<br>    }<br>]</pre><pre>let writers = [<br>    {<br>        name: &#39;Vincent Ford&#39;,<br>        songs: [&#39;No Woman, No Cry&#39;, &#39;Crazy Baldhead&#39;]<br>    },<br>    {<br>        name: &#39;Bob Marley&#39;,<br>        movies: [&#39;In This Love&#39;]<br>    },<br>    {<br>        name: &#39;Turky Al Sheekh&#39;,<br>        movies: [&#39;Ya Agmal 3oyoon&#39;, &#39;A7la 7aga&#39;]<br>    }<br>]</pre><pre>module.exports = {<br>    songs,<br>    artists,<br>    writers<br>}</pre><h4>Create the Components</h4><p>Let’s create some components that we will be navigating through in this application. Inside ./srccreate a new directory and name it components. We will be adding Home.js, Artists.js, Writers.js, andSongs.js</p><p>Here are samples of how these components would look like. You can customize it to whatever you like.</p><pre>// ./src/components/<strong>Home.js</strong></pre><pre>import React from &#39;react&#39;;</pre><pre>const Home = () =&gt; {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Home Page&lt;/h1&gt;<br>    &lt;/div&gt;<br>  );<br>};</pre><pre>export default Home;</pre><pre>// ./src/components/<strong>Artists.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { artists } from &#39;../data&#39;;</pre><pre>const Artists = () =&gt; {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Artists Page&lt;/h1&gt;<br>      {artists.map((artist, index) =&gt; (<br>          &lt;div key={index}&gt;<br>              &lt;h3&gt;Name: {artist.name}&lt;/h3&gt;<br>              &lt;p&gt;Songs:&lt;/p&gt;<br>              &lt;ul&gt;<br>                  {artist.songs.map((song, index) =&gt; (<br>                      &lt;li key={index}&gt;{song}&lt;/li&gt;<br>                  ))}<br>              &lt;/ul&gt;<br>          &lt;/div&gt;<br>      ))}<br>    &lt;/div&gt;<br>  );<br>};</pre><pre>export default Artists;</pre><pre>// ./src/components/<strong>Writers.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { writers } from &#39;../data&#39;;</pre><pre>const Writers = () =&gt; {<br>    return (<br>        &lt;div&gt;<br>            &lt;h1&gt;Writers Page&lt;/h1&gt;<br>            {writers.map((writers, index) =&gt; (<br>                &lt;div key={index}&gt;<br>                    &lt;h3&gt;Name: {writers.name}&lt;/h3&gt;<br>                    &lt;p&gt;Songs:&lt;/p&gt;<br>                    &lt;ul&gt;<br>                        {writers.songs.map((song, index) =&gt; (<br>                            &lt;li key={index}&gt;{song}&lt;/li&gt;<br>                        ))}<br>                    &lt;/ul&gt;<br>                &lt;/div&gt;<br>            ))}<br>        &lt;/div&gt;<br>    );<br>}</pre><pre>export default Writers</pre><pre>// ./src/components/<strong>Songs.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { songs } from &#39;../data&#39;;</pre><pre>const Songs = () =&gt; {<br>    return (<br>        &lt;div&gt;<br>            &lt;h1&gt;Songs Page&lt;/h1&gt;<br>            {songs.map((song, index) =&gt; (<br>                &lt;div key={index}&gt;<br>                    &lt;h3&gt;Name: {song.title}&lt;/h3&gt;<br>                    &lt;p&gt;Genres:&lt;/p&gt;<br>                    &lt;ul&gt;<br>                        {song.genres.map((genre, index) =&gt; (<br>                            &lt;li key={index}&gt;{genre}&lt;/li&gt;<br>                        ))}<br>                    &lt;/ul&gt;<br>                &lt;/div&gt;<br>            ))}<br>        &lt;/div&gt;<br>    );<br>};</pre><pre>export default Songs;</pre><h4>Add a Navigation Bar</h4><p>Now that we have all our components, let’s add a navigation bar to all of them. We will be using theNavLinkpackage that comes with react-router-dom. Inside the components directory, create a new file and name it NavBar.js.</p><pre>// ./src/components/<strong>NavBar.js</strong></pre><pre>import React from &#39;react&#39;;<br>import { NavLink } from &#39;react-router-dom&#39;;</pre><pre>const NavBar = () =&gt; {<br>    return (<br>        &lt;div className=&quot;navbar&quot;&gt;<br>            &lt;NavLink to=&quot;/&quot;&gt;Home&lt;/NavLink&gt;<br>            &lt;NavLink to=&quot;/songs&quot;&gt;Songs&lt;/NavLink&gt;<br>            &lt;NavLink to=&quot;/writes&quot;&gt;Writers&lt;/NavLink&gt;<br>            &lt;NavLink to=&quot;/artists&quot;&gt;Artists&lt;/NavLink&gt;<br>        &lt;/div&gt;<br>    );<br>};</pre><pre>export default NavBar;</pre><p>These are the navigation links to each of the components that we just created. We will be importing this NavBarcomponent inside App.js.</p><h4>Define the Routes in App.js</h4><p>Even though we have the NavBar, it is not working because we didn’t define the routes. We need to tell our application when the user clicks on a link, to go to the corresponding route. Here is where the react-router-dom comes in handy to define all these routes. Make sure to import BrowserRouter and Route from react-router-dom</p><p>Let’s fix App.jsto include those routes:</p><pre>// ./src/<strong>App.js</strong></pre><pre>import {BrowserRouter as Router, Route} from &#39;react-router-dom&#39;;<br>import NavBar from &#39;./components/NavBar&#39;;<br>import Home from &#39;./components/Home&#39;;<br>import Songs from &#39;./components/Songs&#39;;<br>import Artists from &#39;./components/Artists&#39;;<br>import Writers from &#39;./components/Writers&#39;;</pre><pre>function App() {<br>  return (<br>    &lt;div&gt;<br>        &lt;Router&gt;<br>          &lt;NavBar /&gt;<br>          &lt;Route exact path=&quot;/&quot; component={Home} /&gt;<br>          &lt;Route exact path=&quot;/artists&quot; component={Artists} /&gt;<br>          &lt;Route exact path=&quot;/writers&quot; component={Writers} /&gt;<br>          &lt;Route exact path=&quot;/songs&quot; component={Songs} /&gt;<br>        &lt;/Router&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><pre>export default App;</pre><p>You see that we used a Router tag and included our NavBar inside it. After that, we included all our routes by adding the path and the component that it should navigate to. One thing to pay attention to here is the exactflag. If you include the exactflag, it will match the exact path to present the component, however, if you don’t use theexact flag here, it will render the component even if at least part of the path is matched.</p><p>Here is a screenshot of how the routes change according to what component is presenting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*uwOuEJdEQ2LaLOzurTkNrw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*I5AY7amthIoVK_F7TfPVBQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*BcZG0nj_jk7v9NHRho2DYw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*ymmVHGoozPqqxTb_b9Y-QQ.png" /><figcaption>Screenshots of application routes</figcaption></figure><p>This was how to use React-Router to navigate through your React application components. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=693a03dc23bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scaffold Generator in Rails]]></title>
            <link>https://shaqqour.medium.com/scaffold-generator-in-rails-23a89bb4d690?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/23a89bb4d690</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[scaffolding]]></category>
            <category><![CDATA[scaffold]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sun, 21 Feb 2021 20:58:51 GMT</pubDate>
            <atom:updated>2021-02-21T20:58:51.796Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zmyMps84Q4xze9Bc" /><figcaption>Photo by <a href="https://unsplash.com/@goshua13?utm_source=medium&amp;utm_medium=referral">Joshua Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you are new to Rails, you might come across <strong>Scaffold</strong> generator. So what is scaffold generator in Rails? In this blog post, I will show you a code along with an example of how to use it.</p><h4>First, What is Scaffold?</h4><p><strong>“</strong>Scaffolding, also called scaffold or staging is a temporary structure used to support a work crew and materials to aid in the construction, maintenance and repair of buildings, bridges and all other man-made structures.” — <em>Wikipedia</em></p><p>Now that you know what scaffold is in general, you can have an idea of what scaffold is in Rails. A scaffold in Rails is a generator that can be used in the command line to automatically generate some files to create the basic structure of a complete Rails application. These files include a Model, a Controller, Views for each controller action, a Migration for the database, a Route, and some test unit files.</p><h4>Let&#39;s start our code along!</h4><p>First, let’s try out this generator to create a movie project. In your terminal, run first $ rails new movies to create the project then run $ rails generate scaffold moviesto create the basic structure. Here is the result of running the scaffold generator:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5RY_P_-H8ZwXb4mUXi5k3Q.png" /><figcaption>A list of all the files that were created when running the scaffold generator</figcaption></figure><p>That command created the following for us:</p><ul><li>A movie model</li><li>A movie controller</li><li>A new route resources :movies that was added to config/routes.rb.</li><li>Views inside app/views.movies</li><li>Some testing files</li></ul><p>Let’s undo this by using the destroy command, $ rails destroy scaffold movies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ajIRtaZ5jHLF6x2yDrw9Jg.png" /><figcaption>A list of all the files that were deleted when running the scaffold destroy</figcaption></figure><p>This deleted everything that was created when you ran the scaffold generator command.</p><p>Let’s re-run the scaffold generator command, but now let’s add some fields:</p><p>$ rails generate scaffold movies title:string director:string</p><p>Now remember one thing, this created a migration file for you for the database, however, you still need to run $ rails db:migrate to migrate the database changes. If you don’t see any errors, then you are ready to run your application on your browser.</p><h4>Running the application on your browser</h4><p>Now that we’ve created our application, let’s try to run it on the browser. To do so, you need to run the rails server. In your terminal, run the following command:</p><p>$ rails server</p><p>You can check out the application by directing to: <a href="http://localhost:3000/movies">http://localhost:3000/movies</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*5xotMoH98RTEuKtEq5XMXQ.png" /><figcaption>Movies application</figcaption></figure><p>If you click on the ‘New Movie’ link, you can create a new movie like showing below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*tUU1k-jV2i3b_pX69CmQJQ.png" /><figcaption>Create a new movie</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*h-BFyTlStIoFeHgTAdrrfA.png" /><figcaption>The movie that was just created</figcaption></figure><p>Play around with the application and try to add more movies and delete some movies and edit them as well.</p><p>This was an explanation of how to use the Scaffold generator in rails. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23a89bb4d690" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Create Click Me Button with a Counter in React Native]]></title>
            <link>https://shaqqour.medium.com/how-to-create-click-me-button-with-a-counter-in-react-native-32bcf1d68a5e?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/32bcf1d68a5e</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[state]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 14:04:17 GMT</pubDate>
            <atom:updated>2021-02-12T14:04:17.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0ULKPalL10yhBL4x" /><figcaption>Photo by <a href="https://unsplash.com/@bernardhermant?utm_source=medium&amp;utm_medium=referral">Bernard Hermant</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In one of my <a href="https://shaqqour.medium.com/how-to-run-your-first-hello-world-app-on-your-iphone-e1d9b3548ab4">previous blogs</a>, I explained how to create your first react-native project. In this blog post, we will create a project that updates the state when typing in a TextInputor by clicking a button.</p><h4>Create a New Project</h4><p>We know from the <a href="https://shaqqour.medium.com/how-to-run-your-first-hello-world-app-on-your-iphone-e1d9b3548ab4">previous react-native blog post</a> the way to create a new project using Expo client to run your app on a web browser or you can even run it on your device. In your terminal run the following command and then when prompt choose blank :</p><pre>$ expo init counter</pre><p>After you create a new react native project, if you get an error that says “Something went wrong installing JavaScript dependencies,” run npm install and that should fix the error.</p><p>To see the live changes, make sure to get the expo client on your device and follow these <a href="https://shaqqour.medium.com/how-to-run-your-first-hello-world-app-on-your-iphone-e1d9b3548ab4">instructions</a> and then run expo start in your terminal.</p><h4>Create a Greeting Component</h4><p>Create a new file in the same directory and call it Greeting.js. This will have the greeting message. It can look something like this</p><pre>import React, { useState } from &#39;react&#39;;<br>import { Text, TextInput, View } from &#39;react-native&#39;;</pre><pre>const Greeting = () =&gt; {<br>    const [name, setName] = useState(&quot;Sarah&quot;);<br>    <br>    return (<br>        &lt;View&gt;<br>           &lt;TextInput placeholder=&quot;Enter your name...&quot;<br>                      onChangeText={text =&gt; setName(text)}<br>            /&gt;<br>           &lt;Text&gt;Hello {name}!&lt;/Text&gt;<br>        &lt;/View&gt;<br>   );<br>}<br>export default Greeting;</pre><p>Let me explain a little here. In the beginning, we are importing React and the useState component to be able to use React Hooks instead of using props. Later, we are defining the Greeting function where we initiate the state, const [name, setName] = useState(&quot;Sarah&quot;);. Then we are returning a TextInput and a greeting statement. In the TextInput, we are updating the state variable name to whatever the user is typing inside the TextInput.</p><p>Now, we will create the counter and a reset button:</p><h4>Create a Counter Component with a Reset Button</h4><p>Create another file in the same directory and call it Counter.js. This will have a button and a counter state variable to keep track of how many times you pressed this button. Also, it will have a reset button to reset the counter state variable.</p><pre>import React, { useState } from &#39;react&#39;;<br>import { View, Text, Button } from &#39;react-native&#39;;</pre><pre>const Counter = () =&gt; {<br>    const [count, setCount] = useState(0);</pre><pre>   return (<br>        &lt;View&gt;<br>           &lt;Button<br>             onPress={() =&gt; {setCount(count + 1)}} title=&quot;Click Me&quot;<br>           /&gt;<br>           &lt;Button<br>             onPress={() =&gt; {setCount(0)}} title=&quot;Reset&quot;<br>           /&gt;<br>           &lt;Text&gt;You clicked me {count} times&lt;/Text&gt;<br>       &lt;/View&gt;<br>   );<br>}<br>export default Counter;</pre><p>The same as the Greeting.js component, we are importing React and useState that handles the changing in the state variable, counter. Then we are defining the Counter function which will have two buttons, one to increase the counter value by one and another to reset the counter. Also, we have the Texttag to display the counter value to make sure it listens to the changes when we click on Click Me and Reset button.</p><p>This was how you Create a Click Me button that would update the state. Make sure to include these two components, Greeting.js and Counter.js, in your App.js by importing both of them and add the following two tags inside the View tag in App.js :</p><pre>import React from &#39;react&#39;;<br>import { Text, TextInput, View } from &#39;react-native&#39;;<br>import Counter from &#39;./Counter&#39;;<br>import Greeting from &#39;./Greeting&#39;;</pre><pre>export default function App() {<br>     <br>     return (<br>        &lt;View&gt;<br>           <strong>&lt;Greeting /&gt;<br>           &lt;Counter /&gt;<br>        </strong>&lt;/View&gt;<br>     );<br>}</pre><p>Hope that helped you in creating a react-native app with a counter state variable that can be updated by clicking a button. Also a greeting message with a name that can be changed according to the TextInput value. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32bcf1d68a5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Resource Generator in Rails]]></title>
            <link>https://shaqqour.medium.com/resource-generator-in-rails-2316799e1c7?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/2316799e1c7</guid>
            <category><![CDATA[rails-generators]]></category>
            <category><![CDATA[resources]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[generator]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sat, 06 Feb 2021 11:49:36 GMT</pubDate>
            <atom:updated>2021-02-06T11:49:36.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jPpk78KinFarVk1T" /><figcaption>Photo by <a href="https://unsplash.com/@goshua13?utm_source=medium&amp;utm_medium=referral">Joshua Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In one of my <a href="https://shaqqour.medium.com/model-generator-in-rails-in-details-34644ac00e44">previous blogs</a>, I wrote a blog post on how to use the Model generator in Rails. In this blog post, I will explain how to use the Resource generator.</p><p>As we said before, generators in Rails is a built-in script that produces many application elements. The Model generator produces a model (class) inside the app/models directory, a migration file inside thedb/migrate directory, in addition to some unit testing files. However, the Resource generator produces more than just these elements. Let’s talk about this in detail.</p><h4>Resource Generator Command</h4><p>You can write the command to create your resource specifying the attributes or you can leave them blank and specify that later in the migration. Let’s use an example to explain this:</p><p>$ rails generate resource Movie</p><p>This what happens when you run the previous command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*Qonef5jPSKQUVotBIifmIg.png" /><figcaption>List of the files the command created</figcaption></figure><p>That created for us: a migration, a model, a controller, a route, a helper, and some test unit files. We will go over the migration, model, controller, and route.</p><h4>The Migration</h4><p>The migration will be created for you, but it will be empty because you didn’t specify any attributes. Open the migration file, db/migrate/create_movies.rb <br>It should look something like this:</p><pre>class CreateMovies &lt; ActiveRecord::Migration[6.0]<br>    def change<br>        create_table :movies do |t|<br>           <br>           t.timestamps<br>        end<br>    end<br>end</pre><p>Here inside the change method, we can add each of the table attributes and specify what data types they will be. Let’s just add two attributes, the title and the director:</p><pre>class CreateMovies &lt; ActiveRecord::Migration[6.0]<br>    def change<br>        create_table :movies do |t|<br>           <strong>t.string :title<br>           t.string :director</strong></pre><pre>           t.timestamps<br>        end<br>    end<br>end</pre><p>If you want to specify these attributes beforehand, you just need to add them to the resource generating like this:</p><pre>$ rails generate resource Movie <strong>title:string director:string</strong></pre><h4>The Model</h4><p>The model will not have anything in it. It will an empty class that inherits from the ApplicationRecord class which inherits the ActiveRecord::Base. Later, in your project, you might customize this class by adding some methods to it.</p><pre>class Movie &lt; ApplicationRecord</pre><pre>    # Methods</pre><pre>end</pre><h4>The Controller</h4><p>Like the model, the controller will also be an empty class that inherits from theApplicationControllerclass which inherits from the ActionController::Base. Later, you can add some actions such as index or showto this controller.</p><pre>class MoviesController &lt; ApplicationController</pre><pre>    # Actions</pre><pre>end</pre><h4>The Route</h4><p>The routes file, config/routes.rb, is basically a way to handle the HTTP requests and match them up with the controller actions. The resource generator adds all the basic routes inside the routes.rb.</p><pre>Rails.application.routes.draw do<br><strong>     resources :movies<br></strong>     # For details on the DSL available within this file, see<br>     <a href="https://guides.rubyonrails.org/routing.html">https://guides.rubyonrails.org/routing.html</a><br>end</pre><p>This was how to use the Resource Generator in Rails. If you have anything you would like to add, please comment below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2316799e1c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Code Along That Explains an Easy Way to Scrape Data in Ruby]]></title>
            <link>https://shaqqour.medium.com/code-along-that-explains-an-easy-way-to-scrape-data-in-ruby-6c533e3021fc?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c533e3021fc</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[css-selectors]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[nokogiri]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sun, 31 Jan 2021 19:48:35 GMT</pubDate>
            <atom:updated>2021-01-31T19:48:35.329Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2ELtNdZ2AkfvCz_R" /><figcaption>Photo by <a href="https://unsplash.com/@danedeaner?utm_source=medium&amp;utm_medium=referral">Dane Deaner</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>What is Scraping in Ruby?</h4><p>Scraping in Ruby is a way to extract data from web pages so you can use it in your program. We usually scrape data when the website doesn’t provide raw data, APIs for instance, for the information we need. So we just pull the web page to our program and extract some raw data from it.<br>For example, this <a href="https://editorial.rottentomatoes.com/guide/200-essential-movies-to-watch-now/">web page</a> presents the best 300 movies to watch according to Rotten Tomatoes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*DN_WFeQx3XpubMRb5FW36Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*zfPxYU9M2MR0gcpCO_RGRg.png" /><figcaption>Rotten Tomatoes 300 Essential Movies to Watch</figcaption></figure><p>You see the list of the movies and some information about each one. Our mission here is to read this information and store it in our program. Let’s dive deep into this.</p><h4>Introducing Nokogiri</h4><p>The word “Nokogiri” is a Japanese saw, a tool that is used in woodworking. It is also a Ruby gem that is used for data scraping ;-). To install this gem on your machine use the following command in your terminal:</p><pre>gem install nokogiri</pre><p>Make sure to include your Gemfile in your project directory and run bundle install . Here is an example of how the Gemfile would look like:</p><pre>source &quot;https://rubygems.org&quot;</pre><pre>gem &#39;byebug&#39;<br>gem &#39;nokogiri&#39;</pre><p>Let’s start our code-along:</p><h4>Create a new project:</h4><p>In your machine, create a new project directory, and let’s call it movie-scraper, and inside that directory create another directory and call it lib. finally, create the scraper file. You can run the following commands:</p><pre>mkdir movie-scraper<br>cd movie-scraper<br>mkdir lib<br>cd lib<br>touch scraper.rb</pre><h4>Require nokogiri and open-Uri</h4><p>Inside your new file add the following two lines:</p><pre>require &#39;nokogiri&#39;<br>require &#39;open-uri&#39;</pre><p>The open-uri gem is already included in your library, so you don&#39;t need to install it. This gem will pull the whole webpage and store it inside a variable in our program.</p><h4>Create the class</h4><p>Also, we will need a new class in the same file, scraper.rb , to include all the scraping code:</p><pre>require &#39;nokogiri&#39;<br>require &#39;open-uri&#39;</pre><pre>class MovieScraper</pre><pre>end</pre><h4>Send HTTP request to the movies page</h4><p>We need to send a request to the web page that has all the data we need. Here we will use open-uri and nokogirigems to do so:</p><pre>require &#39;nokogiri&#39;<br>require &#39;open-uri&#39;</pre><pre>class MovieScraper<br>   <strong>url = &quot;https://editorial.rottentomatoes.com/guide/essential-<br>          movies-to-watch-now/&quot;<br>   html = open(url)<br>   doc = Nokogiri::HTML(html)</strong><br>end</pre><p>Now that we have all we need inside the doc variable, which is an array of all the elements in the web page, let&#39;s browse through it.</p><h4>Use the CSS selectors</h4><p>We will need to use our skills in selecting an element in the doc variable. Here is a <a href="https://help.crossbrowsertesting.com/selenium-testing/tutorials/css-selectors-to-xpath/">link</a> that will help you understand how to select an element inside a web page. On our web page, we need to select the element that has all the movies. By inspecting the page, you can see that all the movies are inside this element div .row.countdown-item . Let’s store it by itself inside an array that will contain all the inner elements. This makes it easier for us to select from it.</p><pre>require &#39;nokogiri&#39;<br>require &#39;open-uri&#39;</pre><pre>class MovieScraper<br>   url = &quot;https://editorial.rottentomatoes.com/guide/essential-<br>          movies-to-watch-now/&quot;<br>   html = open(url)<br>   doc = Nokogiri::HTML(html)<br><strong>   movies = doc.css(&quot;div .row.countdown-item&quot;)<br></strong>end</pre><h4>Getting each movie name</h4><p>Now that we have all the movies stored in one array, we can iterate over it and get the name of the movie by using this selector, div h2 a .</p><pre>require &#39;nokogiri&#39;<br>require &#39;open-uri&#39;</pre><pre>class MovieScraper<br>   url = &quot;https://editorial.rottentomatoes.com/guide/essential-<br>          movies-to-watch-now/&quot;<br>   html = open(url)<br>   doc = Nokogiri::HTML(html)<br><strong>   </strong>movies = doc.css(&quot;div .row.countdown-item&quot;)</pre><pre><strong>   movies_names = []<br>   movies.each_with_index do |movie, idx|<br>     movies_names &lt;&lt; movie.css(&quot;div h2 a&quot;).text.strip<br>   end<br></strong>end</pre><p>You see that we are storing all the movies name in the movies_names array. Using the same techniques, you can get all other movies’ information and store it in your program in any format you like. The most common one is using a hash.</p><p>That was how you scrape data from a webpage on the internet using Nokogiri. Hope that helped you in your first scraping application in Ruby. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c533e3021fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the Basics of  Macros in Ruby Programming]]></title>
            <link>https://shaqqour.medium.com/understanding-the-basics-of-macros-in-ruby-programming-13929a366075?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/13929a366075</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby-programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[macro]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sat, 23 Jan 2021 12:26:44 GMT</pubDate>
            <atom:updated>2021-01-23T12:26:44.418Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding the Basics of Macros in Ruby Programming</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kKsoxMp3RTKe3ycm" /><figcaption>Photo by <a href="https://unsplash.com/@rosssneddon?utm_source=medium&amp;utm_medium=referral">Ross Sneddon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A big part of Ruby programming is based on making the programmer life’s easier and better. Macros in Ruby were created so programmers don’t write the same things over and over again. So what is a macro in Ruby?!</p><p>A macro in Ruby is a piece of code that is responsible for generating other code which could be a new instance method. In my <a href="https://shaqqour.medium.com/how-to-use-getters-and-setters-in-ruby-edb24f722970">previous blog post</a>, I explained how to write Setter and Getter methods in Ruby. In this blog post, I will show you how you can use some macros that will generate getter or setter methods for you.</p><h4>Macro for getter method</h4><p>Let’s use the same Student class example from the last blog post:</p><pre>class Student<br>  def initialize(name)<br>    @name = name<br>  end<br><br>  #getter method<br>  def name<br>    @name<br>  end<br>end</pre><p>Ruby provides a way to include this getter method instead of always having to repeat the same thing. Here is how to do so:</p><pre>class Student<br>  <strong>attr_reader :name </strong>#this replaces the getter method above<br>  <br>  def initialize(name)<br>    @name = name<br>  end<br>end</pre><p>You even can include many instance variables in the same line. Let’s say the student class has another instance variable, for example age.</p><pre>class Student<br>  <strong>attr_reader :name, :age </strong>#this replaces the getter method above<br>  <br>  def initialize(name, age)<br>    @name = name<br>    @age = age<br>  end<br>end</pre><h4>Macro for setter method</h4><pre>class Student<br>  <strong>attr_writer :name, :age </strong>#this replaces the setter method bellow</pre><pre>  def initialize(name, age)<br>    @name = name<br>    @age = age<br>  end<br><br>  #No need for this anymore<br>  #def name=(name)<br>  #  @name = name<br>  #end<br>end</pre><p>attr_reader is a macro for getter methods and attr_writer is a macro for setter methods. In some cases, you will need getter and setter methods for the same instance variable. That’s why there is another macro to do both at the same time. attr_accessor is a macro for generating a getter and a setter method.</p><h4>Macro for both</h4><pre>class Student<br>  <strong>attr_accessor :name, :age</strong></pre><pre>  def initialize(name, age)<br>    @name = name<br>    @age = age<br>  end<br>end</pre><p>There is much more than setter and getter macros in Ruby. For example, when declaring a “has many” relationship in Rails application, you would use has_many macro. You can read more about it <a href="https://guides.rubyonrails.org/association_basics.html">here</a>.</p><p>Hope that helps you understand what macros are in Ruby programming. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13929a366075" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Getters and Setters in Ruby]]></title>
            <link>https://shaqqour.medium.com/how-to-use-getters-and-setters-in-ruby-edb24f722970?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/edb24f722970</guid>
            <category><![CDATA[instance-variables]]></category>
            <category><![CDATA[class]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[setter]]></category>
            <category><![CDATA[getter]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Fri, 15 Jan 2021 15:01:08 GMT</pubDate>
            <atom:updated>2021-01-15T15:01:08.322Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jrsTVtkqrJLTZE5x" /><figcaption>Photo by <a href="https://unsplash.com/@casparrubin?utm_source=medium&amp;utm_medium=referral">Caspar Camille Rubin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Getter and Setter methods in Ruby are gateways to access class instance variables. In this blog post I will explain how to create a class with getter and setter methods.</p><p>Let’s explain what a getter and a setter method in Ruby are!</p><p>A<strong> Getter method </strong>is the only way where you can retrieve the value of an instance variable from outside its class. A <strong>Setter method </strong>is the only way where you can assign a value to an instance variable from outside its class.</p><p>Now that we have an idea on what each one is, let’s explain them using an example.</p><p>Here is a <strong>Student</strong> class:</p><pre>class Student<br>  def initialize(name)<br>    @name = name<br>  end<br>end<br><br>student = Student.new(&#39;Sarah&#39;)<br>p student.name #=&gt; <em>undefined method `name&#39; (NoMethodError)</em></pre><p>We defined a <strong>Student</strong> class and then later on we created a student giving her the name “Sarah”, however when we tried to access the student name, it gave us a <em>NoMethodError</em> because it is trying to look for a method called name but we never defined this method.</p><p>As we said before, we need to define a getter method to be able to retrieve the student name:</p><pre>class Student<br>  def initialize(name)<br>    @name = name<br>  end<br><br>  #getter method<br>  def name<br>    @name<br>  end<br>end<br><br>student = Student.new(&#39;Sarah&#39;)<br>p student.name #=&gt; &quot;Sarah&quot;</pre><p>Here we were able to retrieve the name because we defined a <strong>getter </strong>method for the name instance variable.</p><p>Now, let’s try to change the name of the student:</p><pre>class Student<br>  def initialize(name)<br>    @name = name<br>  end<br><br>  #getter method<br>  def name<br>    @name<br>  end<br>end<br><br>student = Student.new(&#39;Sarah&#39;)<br>p student.name #=&gt; &quot;Sarah&quot;<br>student.name = &#39;Pumpkin&#39; #=&gt; <em>undefined method `name=&#39;(NoMethodError)</em></pre><p>Here when we tried to assign another name to the student, we got a <em>NoMethodError </em>because we never defined a <strong>setter</strong> method for the name instance variable.</p><p>Here is how we define a setter method:</p><pre>class Student<br>  def initialize(name)<br>    @name = name<br>  end<br><br>  #getter method<br>  def name<br>    @name<br>  end<br><br>  #setter method<br>  def name=(name)<br>    @name = name<br>  end<br>end<br><br>student = Student.new(&#39;Sarah&#39;)<br>p student.name #=&gt; &quot;Sarah&quot;<br>student.name = &#39;Pumpkin&#39;<br>p student.name #=&gt; &quot;Pumpkin&quot;</pre><p>Notice how we defined the setter method adding = to the end of its name to indicate it is a setter method. When we do student.name = &#39;Pumpkin&#39; this will automatically call the setter method sending whatever is after the equal sign = to the method as a parameter.</p><p>Hope that gave you a clear explanation on Getters and Setters in Ruby. If you would like to add anything, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=edb24f722970" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Closures in JavaScript]]></title>
            <link>https://shaqqour.medium.com/understanding-closures-in-javascript-77b538771927?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/77b538771927</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[sinners]]></category>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[outer]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Sat, 09 Jan 2021 18:24:17 GMT</pubDate>
            <atom:updated>2021-01-09T18:24:17.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iFoi7gN7Nrz9Ylms" /><figcaption>Photo by <a href="https://unsplash.com/@frankiefoto?utm_source=medium&amp;utm_medium=referral">frank mckenna</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A closure is a concept in JavaScript where one function has access to variables that belong to another function. The main thing about these two functions is that one lives inside the other where the inner one has access to the outer one’s variables. A closure is created whenever a function is created.</p><p>When a closure is created, it will have three scope chains: its own scope, the outer function scope, and the global scope.</p><ul><li>It will have access to its own scope where any variable is defined within its body.</li><li>It will have access to the outer function variables.</li><li>It will have access to any global variable.</li></ul><p>Let’s just explain this by using a code example of a simple closure that you can run and practice using this <a href="https://jsfiddle.net/">link</a>.</p><pre>function outer() {<br>    var exterior = 100;<br>    <br>    function inner() {<br>        var interior = 200;<br>        console.log(exterior + interior);<br>    }<br>    <br>    return inner;<br>}</pre><p>You see that there is an outer function and an inner function here.</p><p>The outerfunction has the variable exterior and notice that at the end it returns a reference to theinner function.<br>The inner function has its own variable interior and it also has access to the outer function variable exterior .</p><p>The variable exterior is accessible within the outer function scope, and the variable interior is accessible within the inner function scope. Let’s call the the outer function and store its result in another variable.</p><pre><strong>var result = outer();</strong></pre><p>Before discussing what result stores, let’s see what happens when you invoke the outer function:</p><ul><li>The variable exterior is created and stores the value 100 in it.</li><li>The inner function is declared without being invoked.</li><li>A reference of the inner function is returned.</li></ul><p>Since a reference to the inner function is returned, that is what will be stored in the result variable.</p><p>Something you need to pay attention to is once the outer function is done with execution, all its local variables (in our example, exterior) will no longer exist. Ok, now, let’s try to invoke the inner function which we have a reference to it, result by adding parenthesis to it like this:</p><pre><strong>result();</strong></pre><p>Let’s go step by step on what will happen here:</p><ul><li>The variable interioris created and stores the value 200 in it.</li><li>There is a console.log(exterior + interior); let’s dive into details for both of these variables, exterior and interior .</li></ul><p>The interior variable is defined within the inner function scope, so we will have no problem trying to access this variable here, however, the exterior in the outer function, as we said, no longer exists because the execution of the outer is done. Now when we invoke the inner function, theoretically, we will have an error trying to access a variable that no longer exists.</p><p>Here comes the idea of closure, which is how JavaScript handles this situation. Once the inner is declared, JavaScript will preserve variables in the outer function to be available in the inner function and whenever the inner function is called, those outervariables will be saved and ready to use in the inner function. So when we invoke the inner function, result(), the console.log(exterior + interior); will print 300 to the console.</p><p>Hope that helps you understand closures in JavaScript. If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77b538771927" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a Migration in Rails Manually]]></title>
            <link>https://shaqqour.medium.com/create-a-migration-in-rails-manually-32917a1dfe1f?source=rss-62f9f6293668------2</link>
            <guid isPermaLink="false">https://medium.com/p/32917a1dfe1f</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[migration]]></category>
            <dc:creator><![CDATA[Shaqqour]]></dc:creator>
            <pubDate>Fri, 01 Jan 2021 15:26:10 GMT</pubDate>
            <atom:updated>2021-01-01T15:26:10.708Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IVIXqNLMYqJGc6Q1" /><figcaption>Photo by <a href="https://unsplash.com/@_aloksharma?utm_source=medium&amp;utm_medium=referral">Alok Sharma</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Migration in Rails is responsible for creating and editing database tables. To migrate the database changes in rails, run $ rails db:migrate<em>.<br></em>Rails will only read the new migration files that have never been migrated before. So once you run a migration file, you can’t go back and change it and run it again. You will have to create a new migration to edit your database.</p><p>In this blog post, I will show you how to manually create a migration file in Rails; whether it is a whole new table or fixing an existing table.</p><h4>Create a table</h4><p>You will have to create a new file in your db/migrate folder and you can choose a name such as 01_create_employees.rb. In this file, you will define a method within a class that inherits from Active Record. This method will create the employees&#39; table. Here is a code snippet on how this file will look like:</p><pre>class CreateEmployees &lt; ActiveRecord::Migration[5.1]<br>    def change<br>        create_table :employees do |t|<br>             t.string :first_name<br>             t.string :last_name<br>        end<br>    end<br>end</pre><p>We defined the <strong><em>change</em></strong> method indicating inside that these are the changes I want to do to my database which is creating a new table called <strong><em>employees</em></strong>. We are using the ActiveRecord method <strong><em>create_table </em></strong>to do so by passing a block that contains the attributes for this table and the data type for each one.</p><p>For these changes to take effect, as I said before, you will have to run this migration by running the following command:$ rails db:migrate</p><h4>Add a Column to a table</h4><p>Here you will create a migration to add a column, such as a salary attribute, to the employees’ table. Let’s create a file in the same directory, db/migrate, and name it 02_add_salary_to_employees.rb. Similar to creating a table, you will define a method within a class that inherits from Active Record. This method will add the salary column to the employees’ table. Here is a code snippet on how this file will look like:</p><pre>class AddSalaryToEmployees &lt; ActiveRecord::Migration[5.1]<br>    def change<br>       add_column :employees, :salary, :integer<br>    end<br>end</pre><p>As before, we defined the <strong><em>change</em></strong> method indicating inside that these are the changes I want to do to my employees’ table which is adding the salary attribute to it. We are using another ActiveRecord method <strong><em>add_column </em></strong>to do so by passing a block that contains the table you want to add the attribute to, the attribute you want to add, and the data type for this attribute. Run $rails db:migrate command for these changes to take effect.</p><p>There are many situations that you will need to create a new migration for, such as fixing a data type for a column. Here is a <a href="https://guides.rubyonrails.org/active_record_migrations.html">link</a> you can check out for some extra ActiveRecord migration methods you can use to update your database tables.</p><p>If you have anything you would like to add, please comment below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32917a1dfe1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>