<?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 Khanittha Krajangjaem on Medium]]></title>
        <description><![CDATA[Stories by Khanittha Krajangjaem on Medium]]></description>
        <link>https://medium.com/@kkkraj?source=rss-636df410fb02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7QklA_zA0DjOGB2jKyzGdQ.jpeg</url>
            <title>Stories by Khanittha Krajangjaem on Medium</title>
            <link>https://medium.com/@kkkraj?source=rss-636df410fb02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:21:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kkkraj/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[Understanding the basic of Loops  —  in C]]></title>
            <link>https://medium.com/@kkkraj/understanding-the-basic-of-loops-in-c-50e3a597a391?source=rss-636df410fb02------2</link>
            <guid isPermaLink="false">https://medium.com/p/50e3a597a391</guid>
            <category><![CDATA[loop]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[Khanittha Krajangjaem]]></dc:creator>
            <pubDate>Sun, 27 Sep 2020 05:03:53 GMT</pubDate>
            <atom:updated>2020-09-30T22:32:47.221Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding the basic of Loops</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7hqrp3-RdDMy1pNS" /><figcaption>Photo by <a href="https://unsplash.com/@splashabout?utm_source=medium&amp;utm_medium=referral">Nareeta Martin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Loops are basic and important structures that exist in almost all programming languages. Loops allow your program to execute lines of code repeatedly until a specific condition is met. Without loops, we’d have to do a lot of copy and pasting the same line of code over and over.</p><p>I must say I have worked with all kinds of loops in all three of the programming languages that I have learned including C, Ruby, and JavaScript. Even though I feel like I get the theory and how they operate I still find myself hesitating over which loop to use in which situation sometimes.</p><blockquote>Today, I am going to talk about loops in C which is pretty similar to loops in JavaScript.</blockquote><blockquote>There are three major types of loops in C, and as in all languages, they exist for different purposes</blockquote><p><strong>While Loop</strong> — or <em>Infinite Loop<br></em><strong>HOW IT WORKS:</strong> While the condition evaluates to true, the lines of code between the two curly braces will execute from top to bottom forever otherwise the condition evaluates to false or break out of it.<br><strong>WHEN TO USE:</strong> When you want the loop to run repeatedly for an <em>unknown </em>number of times or never run at all.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d9551608369c0a6db9158dc3fe006fc/href">https://medium.com/media/8d9551608369c0a6db9158dc3fe006fc/href</a></iframe><p><strong>Do While Loop <br>HOW IT WORKS: </strong>Is similar to <em>While Loop </em>except for it makes sure the code between the two curly braces run at least once (regardless of the condition). Then continue to check the condition, if the condition expression evaluates to true then the above while loop will execute.<br><strong>WHEN TO USE:</strong> When you have the same purpose of using <em>While Loop</em> but want to make sure your code executes at least once.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d95cf704ea5b266b2d10ed3bf832cba/href">https://medium.com/media/5d95cf704ea5b266b2d10ed3bf832cba/href</a></iframe><p><strong>For Loop<br>HOW IT WORKS: </strong>Is pretty straightforward. It repeats the lines of code between the two curly braces for a specific number of times. Given a <em>counter </em>and a <em>condition</em>,<em> </em>while the condition expression evaluates to true the code will execute. <br><strong>WHEN TO USE:</strong> When you want the loop to run for a specific number of times (e.g. 10 times or x times) and for sure has an endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/508fbf96180abd23758671742eb3ac73/href">https://medium.com/media/508fbf96180abd23758671742eb3ac73/href</a></iframe><blockquote>Something To Keep In Mind — In most cases, you can interchange all three of the above loops, but best to use each loop in the right context. And you can also combine them which make nested loops.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50e3a597a391" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTML, CSS, JavaScript — A Guide to Building a Simple Application using Data from an API]]></title>
            <link>https://medium.com/swlh/html-css-javascript-a-guide-to-building-a-simple-application-using-data-from-an-api-3d432073dbd0?source=rss-636df410fb02------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d432073dbd0</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dom-manipulation]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[like-button]]></category>
            <dc:creator><![CDATA[Khanittha Krajangjaem]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 07:53:36 GMT</pubDate>
            <atom:updated>2022-02-22T23:43:53.591Z</atom:updated>
            <content:encoded><![CDATA[<h3>HTML, CSS, JavaScript — A Guide to Building a Simple Application with Reaction Buttons</h3><p>In my previous blog post, I explained <a href="https://medium.com/@khanitthakk/simple-steps-to-building-your-own-rails-api-from-scratch-169702455943">Simple Steps to Building Your Own Rails API from Scratch</a>. In this blog post to continue with what we’ve built, we are going to pull the data from that API and use it to build something even more awesome-rific!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*mUIH5rLlyvopxOPpZeL-sg.gif" /></figure><p>If you ever find yourself struggling with where to start — begin with the big picture, write it out, sketch it out, whiteboard it out the whole idea you have in mind. It’s always a good idea to translate the abstractions in your head to something you can visually look at, and you can refer to it later down the line. Coming from a Creative field, writing out a <em>design brief</em>, and drawing out a <em>concept board</em> let you determine the concept and scope of your work. With programming, writing out <em>Models</em>, <em>User Stories</em>, <em>Application Features</em>, etc at the beginning of planning your project can help navigate you and keep you on the right path throughout your project. It’s good to follow the patterns, it also helps with the workflow.</p><p>In this example, to start building this application, two important factors we need to know beforehand — what to build? and where to get the data from?</p><ul><li><strong>what to build </strong>— A simple application with image cards one for each main character from a famous Netflix series “Stranger Things”. In which users will be able to see each character’s name and image. And instead of a like-only button, users will be able to give each character “love” 🥰, “eye-roll” 🙄, “mad” 😡, and “shivering” 🥶 reactions.</li><li><strong>where to get the data</strong> — Our stranger-things-api of course.</li></ul><h3>HTML, CSS, JavaScript: 3 Phases — Let’s build the Front-End Application!</h3><h4>Create a New Directory and Base Files</h4><ul><li><strong>Create a new directory </strong>in the terminal<strong> </strong>with<strong> </strong>mkdir command, and be sure tocd into that directory.</li></ul><pre>~ // ♥ &gt; mkdir stranger-things-liker</pre><ul><li><strong>Create a new file</strong> after cd into the working directory create new files in the terminal with<strong> </strong>touch command.</li></ul><pre>~ // ♥ &gt; touch index.html<br>~ // ♥ &gt; touch style.css<br>~ // ♥ &gt; touch index.js</pre><h4>1. First Thing — HTML</h4><p>HTML file is a good place to start. Because you don’t want to build an app blindfolded not knowing what it looks like, what’s already there, and what’s not. If you do not know what to put on the page yet start out with an HTML <a href="https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default">boilerplate</a>, and include some texts in the body element. You may want to check out if your HTML file is successfully set up by typing open index.html in your terminal. That should open up index.html on the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*cuBEu21PiFQoItzMK6tQOg.png" /><figcaption>example of HTML Boilerplate</figcaption></figure><p>Once your HTML file works properly start adding some more elements to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction">DOM</a>. In this example, I created a couple of container elements (div) that wrap around certain contents. There’s also a container I made in advance for the data I am going to pull from the API and render it to the page later on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-_SuhxC06BPlSeMCzAPXzQ.png" /></figure><h4>2. Implement the Functionality — index.js</h4><p>Unless your &lt;script&gt; tag is placed at the bottom of your HTML file, listening to&quot;DOMContentLoaded&quot; is always a good starting point in the JS file. If you ask me, it’s the flag that determines where your code begin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*MW48hV-HBqt7wvN3DdWa5Q.png" /></figure><blockquote><em>Always use </em><em>console.log() to check if your Javascript code is working. Be sure to use Chrome, and open up </em><a href="https://developers.google.com/web/tools/chrome-devtools"><em>Chrome DevTools</em></a><em> to inspect your code.</em></blockquote><h4>A Journey of JavaScript Functions</h4><ul><li><strong>Writing a fetch function —</strong> To <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch">fetch</a> data from the API = To make a GET Request. Simply pass in the resource url, because the default method of <em>fetch</em> is GET so you won’t have to specify which <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods">request method</a> you’re trying to achieve. <br><em>Fetch</em> will return a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#:~:text=The%20Promise%20object%20represents%20the,to%20read%20Using%20promises%20first."><em>promise</em></a> which is not the format we can use right out of the box. You’ll have to use <em>json() method</em> to extract the <em>JSON</em> body from the response, then from there we can start using the data.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*EvuYA-tyTKj9cZx-dARiOA.png" /></figure><ul><li><strong>Render the data</strong> — After data has been fetched and converted into JSON format we can begin to utilize it. Because what we get is always going to be a nested structure we’ll have to use JavaScript to iterate over it. To extract each <em>character</em> from <em>characters</em> we can use forEach() method. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach">forEach()</a> iterates over each element in an array, and executes a callback function once for each element. From there we can access properties of the characters such as names, and images, and use them in the app.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*pKBWrte08LY-cY_hMDdkYw.png" /><figcaption>always use &lt;console.log&gt; to make sure your code is working</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uVgPXxvY-er7hBnTnFMJPA.png" /><figcaption>open up DevTools Console, this is the result of &lt;console.log(character)&gt; and &lt;character&gt; data</figcaption></figure><ul><li><strong>DOM Manipulation</strong> — Time to fulfill the page with the data we just rendered. With <em>DOM</em> Manipulation we can grab elements from the <em>DOM</em>, add elements to the <em>DOM</em>, etc. This is the most exciting part.</li><li><strong>Grab Elements:</strong> There are multiple ways to grab or get certain elements from the DOM.</li></ul><blockquote><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById"><em>document.getElementById()</em></a><em> To grab a specific element by its own unique id.<br></em><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName"><em>document.getElementsByTagName()</em></a><em> To grab elements with the same tag name.<br></em><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName"><em>document.getElementsByClassName()</em></a><em> To grab elements by class name.<br></em><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll"><em>document.querySelectorAll()</em></a><em> To grab multiple elements that share the same attribute value.</em></blockquote><p>In this example, I go with <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector">document.querySelector()</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*rLtfYrIk6oCXcYwzl8bLLA.png" /><figcaption>select element that has the class name “card-container” and set it to a variable cardsContainer</figcaption></figure><blockquote>In order to grab something from the DOM, you’ll often need to refer back to your HTML file or DevTools Elements to see the elements’ attributes because those are what you’ll need to pass into the <em>()</em> in order to grab that specific element or elements from the DOM.</blockquote><ul><li><strong>Create New Elements: </strong>After retrieving an element that already exists in the DOM, in order to add data to the DOM we’ll need to create an additional element to hold a specific piece of data. We can do this with document.createElement().</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*X6WiaOCbWdGR5y0Qh4Nbyw.png" /><figcaption>create a container — image card</figcaption></figure><ul><li><strong>Set Attributes: </strong>After creating an element, you may want to give it some attributes such as an <em>id</em> or add it to a certain group of elements by a <em>class</em>. We can use element.setAttribute(name, value).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*pGKjqKC4yCy8HduLT9xClw.png" /><figcaption>give each image card an id using character id</figcaption></figure><blockquote><em>Using character “object” we extracted from characters, we can access the object properties by the object keys. We get the id by </em><em>character.id. This will set each image card of each character with each character’s id.</em></blockquote><ul><li><strong>Add Image: </strong>Now that the card is created, let’s add an image to it. Start by creating a new image element, give it an attribute, and connect it with the <em>image card element</em> which will become a parent element of this element.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*JViQT9FdGdwPLBzRG8B6Ew.png" /></figure><blockquote><em>Using </em><em>.append or </em><em>.appendChild to add elements to parent elements.</em></blockquote><ul><li><strong>Add Contents with innerText: </strong>Now we have an image card with an image inside, let’s add character’s name to it. <br>Again, follow the patterns<em> — </em>1<em>.</em>Start with creating an element, 2.give it an attribute, and 3.add it to the parent.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*PUkGkd4B61bJ0droafzSig.png" /></figure><p>Adding reaction buttons is quite similar to adding character’s name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*ZK5br5AWzOIlLRYLk0QKfQ.png" /></figure><ul><li><strong>addEventListener: </strong>Because users should be able to click on the reaction buttons, we’ll need an <em>EventListener</em> to listen to the “click” <a href="https://developer.mozilla.org/en-US/docs/Web/Events">event</a>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*xqrGqhqUELRerGS0XQT1Qg.png" /></figure><p>Testing out by console.log() a message, and click on that button to see if the message displays in the console. Users should be able to see if the particular button has been clicked, we can do something with the styling, to identify the change. Notice the button has been initialized with a class attribute with the value “off” by taking advantage of this attribute value, we can keep track of the button’s status, and toggle it on and off.</p><p>With a simple JavaScript Conditional Statement we can do:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*gP7Ej5yiv9mtWukbH7u0Iw.png" /></figure><p>And now the character can be loved and unloved. Super Cool!</p><h4>3. Wrap it up with Styling</h4><p>We created the page and have added some features to it. With CSS we can make it more attractive. Since there are multiple image cards in a single display, we can use grid to make it look clean and organized. You can look up <a href="https://www.w3schools.com/css/css_selectors.asp">CSS Selectors</a> and begin with selecting the most outer div that wraps around all of the cards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*lxVShSckiQq87VpkcEimFw.png" /></figure><p>The page would look strange without a header, let’s add a header with the series logo, set the size, and how it displays on the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*Lg5h45ee7hOpf_a4_yhywQ.png" /></figure><p>Adding a background color, according to the Stranger Things color theme, it is all about black and red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*vVOQ51sNKb8nb0W6aMG2TA.png" /><figcaption>set margin to 0% to get rid of the white space at the border of the page</figcaption></figure><p>Images should stick inside of image cards with a certain size, and all of them should have the same format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*YRd0bB2DaqSWlPk7Wa-laA.png" /><figcaption>use class selector to set up or modify multiple elements with the same format</figcaption></figure><p>You can refresh the page and see the differences. The page now looks organized and sorted.</p><blockquote>“ Coming from a Design background, the CSS part of the whole project is always satisfying ”</blockquote><h3>SUM IT UP</h3><blockquote>Feel free to use my code as a reference. Please read it through and understand the role of each function, what it does, and how it’s communicating with the DOM.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05699d1db12098c191587373109ed6a8/href">https://medium.com/media/05699d1db12098c191587373109ed6a8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f5c6f1914cc82d75de8802cc0a09fb0/href">https://medium.com/media/9f5c6f1914cc82d75de8802cc0a09fb0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9e390d7d730a3fb1b843cccc9ced020/href">https://medium.com/media/f9e390d7d730a3fb1b843cccc9ced020/href</a></iframe><h3>The Take-Aways</h3><ul><li>Use &lt;div&gt;’s for containers of certain contents.</li><li>Use class attribute to group multiple elements together.</li><li>Use id attribute to set a unique #id for a particular element — this will make it easy when working with that element via JavaScript or CSS.</li><li>Instead of having to write the same element.setAttribute(name, value) multiple times. Write a function like setAttributes() function, the function lets you set multiple attributes of the same element at once. This helps making your code short and DRY — <em>index.js line 10–14</em></li><li>Nesting multiple <em>children elements</em> to the same <em>parent</em> at once by parent.append(child1, child2, child3, ...) — <em>index.js line 59–61</em></li><li>Without adding the newly created elements to the parent node, those elements won’t bee displayed on the page.</li><li>Group lines of code that are doing a similar job to keep the code organized and easy to read.</li><li>If something is being used multiple times throughout your code, declare it in a Global Scope in order to reuse it in multiple functions — <em>index.js line 1</em> (cardsContainer declared in Global scope), <em>line 5</em> (cardsContainer is assigned after DOM content has been loaded).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*fsk6pqY7mnhN7f61c8XGHQ.gif" /><figcaption>“Telling Mr. Demogorgon how we feel about him”</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d432073dbd0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/html-css-javascript-a-guide-to-building-a-simple-application-using-data-from-an-api-3d432073dbd0">HTML, CSS, JavaScript — A Guide to Building a Simple Application using Data from an API</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Steps to Building Your Own Rails API from Scratch]]></title>
            <link>https://medium.com/swlh/simple-steps-to-building-your-own-rails-api-from-scratch-169702455943?source=rss-636df410fb02------2</link>
            <guid isPermaLink="false">https://medium.com/p/169702455943</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[rails-api]]></category>
            <dc:creator><![CDATA[Khanittha Krajangjaem]]></dc:creator>
            <pubDate>Sat, 20 Jun 2020 05:37:59 GMT</pubDate>
            <atom:updated>2022-02-22T22:41:10.783Z</atom:updated>
            <content:encoded><![CDATA[<p>Searching for <a href="https://www.freecodecamp.org/news/what-is-an-api-in-english-please-b880a3214a82/">Public API</a>’s that satisfy your needs can get frustrating sometimes. I was that person who spent hours searching for an API that includes data that I already have in mind and what I expect it to look like. Then this idea occurs.</p><blockquote>If you know the how-to — why not use that knowledge and build it yourself? Why spend hours searching when you have that power to build one for your own?</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3ohzgN8AQh5S8BjZq8%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3ohzgN8AQh5S8BjZq8%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3ohzgN8AQh5S8BjZq8%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="217" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f5a27f3219137c128d620eb9d3e557/href">https://medium.com/media/94f5a27f3219137c128d620eb9d3e557/href</a></iframe><p>Let’s babble no more. Because today, my friends, we are gonna be making our own Rails API from scratch — Now, let’s dive into it!</p><h4>step 1: Creating a new <a href="https://guides.rubyonrails.org/api_app.html">Rails API-Only Application</a></h4><p>First, make sure tocd into any folder you want this new application to exist or you can create a new directory by using amkdir command. When ready run:</p><pre>~ // ♥ &gt; rails new<em> stranger-things-api </em>--api</pre><p>By including an--api flag, Rails knows to limit and remove any other default features that are not necessary in an API application.</p><h4>step 2: Implementing Model(s) and Migration(s)</h4><p>Once the new application is created, cd into that application directory. It’s time to open up your code editor and from there start out with:</p><pre>~ // ♥ &gt; rails g resource <em>character name img_url</em></pre><p><em>“Resource Generator” </em>is a great option if you are creating an <em>API</em>, it creates quite a bit of code for you to start with including the important stuff like: <em>Model</em>, <em>Migration</em>, <em>Controller</em>, <em>Resources files</em>, etc.</p><p><strong>Model(s) </strong>— In this example, to keep it simple there’s only one <em>Model</em> to work with, but depending on what you are building, there can be more but make sure to include the <a href="https://guides.rubyonrails.org/association_basics.html"><em>associations</em></a> between them. (Character Model is automatically created when using Resource Generator — by including character parameter)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35fd922167b53b6cf5ab4b1141e150a5/href">https://medium.com/media/35fd922167b53b6cf5ab4b1141e150a5/href</a></iframe><p><strong>Migration(s) </strong>— No wonder <em>“Resource Generator”</em> also creates a <em>Migration</em> file for you by including name and img_url<em> </em>parameters, and with the default type of <em>string </em>this is the <em>Migration</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd2a796ebde34f6f76626619149a0d89/href">https://medium.com/media/fd2a796ebde34f6f76626619149a0d89/href</a></iframe><p><strong>Seeds</strong> — Now that we have <em>Model</em> and <em>Migration</em> set up, because this is an API, this step of seeding the database is the highlight of the show. Everything you put in this file will be the data you’ll grab from the API later on so make sure you include the data you want to see.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99bcb8768a19d5aa7ff567587d753312/href">https://medium.com/media/99bcb8768a19d5aa7ff567587d753312/href</a></iframe><blockquote>Migration is ready, Seeds are ready, run:</blockquote><pre>~ // ♥ &gt; rails db:migrate<br>~ // ♥ &gt; rails db:seed</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/686302cf0589202a88104eefb9eebfec/href">https://medium.com/media/686302cf0589202a88104eefb9eebfec/href</a></iframe><h4>step 3: Controller + Some Modifications</h4><p><strong>Controller — </strong>file has been created with class CharactersController that inherits from ApplicationController, all of these come from using <em>“Resource Generator”. </em>Since I want to be able to just see the characters data and can look up a specific character, I’d only need<em> index </em>and <em>show</em> actions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f094accd89c0074bbba081c458102791/href">https://medium.com/media/f094accd89c0074bbba081c458102791/href</a></iframe><p>By including only: and slice() when generating I minimize what will get rendered and what information will be included in the API. This might not be necessary and you can just simply do:</p><ul><li>render json: characters for index</li><li>render json: character for show</li></ul><p>but the data will include <em>created_a</em>t and <em>updated_at</em> keys which you might not use so there you have options.</p><blockquote><em>If your version of API has more complex model structure, consider looking up </em><a href="https://learn.co/lessons/using-active-model-serializer"><em>Serializer</em></a><em> along with </em><a href="https://github.com/Netflix/fast_jsonapi"><em>Fast JSON API</em></a><em>.</em></blockquote><p><strong>Modifying Some Code</strong></p><p><strong>Modification #1: CORS </strong>— When generating a new application using <em>“Resource Generator”</em>, Gemfile gets created with <a href="https://github.com/cyu/rack-cors#:~:text=Rack%3A%3ACors%20provides%20support,using%20workarounds%20such%20as%20JSONP.">gem &#39;rack-cors&#39;</a> commented out, make sure to uncomment and run bundle install in order to implement <a href="https://humanwhocodes.com/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/">CORS</a>.</p><p>By default, the browser doesn&#39;t allow <a href="https://stackoverflow.com/questions/2130239/what-exactly-is-ajax-request-is-it-different-from-servlet-request#:~:text=An%20Ajax%20call%20is%20an,etc)%20or%20an%20Ajax%20request.">AJAX requests</a> other than the current site you’re visiting, this is a <a href="https://en.wikipedia.org/wiki/Same-origin_policy">web security</a>. By using CORS (Cross-Origin Resource Sharing) you’ll be able to specify who or what can access your endpoint on the server side. Part of your Gemfile and cors.rb should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17f349ddd3d74f24c8e0335309e2d7a9/href">https://medium.com/media/17f349ddd3d74f24c8e0335309e2d7a9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/263fbe6411f11353d12b1b08945db90f/href">https://medium.com/media/263fbe6411f11353d12b1b08945db90f/href</a></iframe><p><strong>Modification #2: Resources </strong>— Using <em>Resource Generator</em> is so convenient, it gives us a full resources call in the routes.rb file. But in this example we are not going to use <a href="https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default">all of them</a>. Since it only needs index action and show action, we can go ahead and modify routes.rb a little.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/957c0a9434e9862c5fe88ada0e1acfce/href">https://medium.com/media/957c0a9434e9862c5fe88ada0e1acfce/href</a></iframe><blockquote>Now that everything is set up properly, be prepared for something spectacular.</blockquote><p>Without further ado, run rails server in your terminal or test it out with a cross-platform application like <a href="https://insomnia.rest/">Insomnia</a>. The API data should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JyBjf9ulOQ-ULGZ4IeJwOA.png" /><figcaption>only includes data specified in CharactersController</figcaption></figure><blockquote>And with that green light and a “200 OK” <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status">HTTP Response Status code</a> <em>we have successfully created the </em>“Back-End”. A<em>nd we are ready to move on to the next adventure … the </em>“Front-End”<em>. From where we stand we can now retrieve the data and building a web app using JavaScript, HTML, and CSS.</em></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl378u5q6LN2kmwdgc%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl378u5q6LN2kmwdgc%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl378u5q6LN2kmwdgc%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="257" frameborder="0" scrolling="no"><a href="https://medium.com/media/a89155a8ad0b9b1416bdebff203c2c90/href">https://medium.com/media/a89155a8ad0b9b1416bdebff203c2c90/href</a></iframe><p><a href="https://medium.com/swlh/html-css-javascript-a-guide-to-building-a-simple-application-using-data-from-an-api-3d432073dbd0">HTML, CSS, JavaScript — A Guide to Building a Simple Application using Data from an API</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=169702455943" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/simple-steps-to-building-your-own-rails-api-from-scratch-169702455943">Simple Steps to Building Your Own Rails API from Scratch</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Computer’s Memory Underneath the Hood]]></title>
            <link>https://medium.com/@kkkraj/computers-memory-underneath-the-hood-6043f0606520?source=rss-636df410fb02------2</link>
            <guid isPermaLink="false">https://medium.com/p/6043f0606520</guid>
            <category><![CDATA[computer-memories]]></category>
            <category><![CDATA[string]]></category>
            <category><![CDATA[char]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Khanittha Krajangjaem]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 11:39:39 GMT</pubDate>
            <atom:updated>2020-06-08T20:41:14.530Z</atom:updated>
            <content:encoded><![CDATA[<p>As we program, we are typing and seeing things on the monitor, there’s something we are also associating with that works behind the scene, it’s called computer’s memory, where things are stored and every byte has its own identifier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NVNXE0pBExJv49lWe6q17A.jpeg" /><figcaption>computer memory — prefix with “0x” followed by the <a href="https://simple.wikipedia.org/wiki/Hexadecimal">hexadecimal</a></figcaption></figure><p>Who would know, when we create something like <strong>int</strong> n <strong>=</strong> 20; in C. What computer does underneath the hood is storing the value inside of the memory. While below is just an example and how the value can be stored anywhere within the memory, and variable n turns out to be just an address of the value 20 in hexadecimal format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oAYsej3p_bfutDLG_9OWyw.jpeg" /><figcaption>Integer takes up 4 bytes in memory</figcaption></figure><p>in C we can see the address by using an &amp; operator:</p><pre>{<br>    <strong>int</strong> n <strong>=</strong> 20;<br>    printf(&quot;%p\n&quot;, <strong>&amp;</strong>n);<br>}</pre><p><strong>let’s break it down a little — </strong>printf(&quot;%p\n&quot;, &amp;n); this line of code tells the computer to find the address of n wherever it exists in memory<strong>:</strong></p><p>%p is a special format code in C which will give us the address of that specific value and &amp;n represents the address of n (&amp;<strong> — </strong><em>the AddressOf operator</em>).</p><p>The above code returns something like 0x2fe07adb which is an address that tells you where the value is inside of the computer’s memory, in another word, that address is a “<em>pointer”</em> (%p) to that value.</p><h4>The other example which makes this more interesting is STRING:</h4><p>If we want to create something like <strong>string</strong> s <strong>=</strong> &quot;STRING&quot;; this is what the computer’s memory will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NeGkP0l2Fn3ttuzAhR753w.jpeg" /><figcaption>each character takes up 1 byte in memory, indicates the end of a string by a terminating character “\0”.</figcaption></figure><p>Apparently s from <strong>string</strong> <strong>s</strong> is just a pointer that points to “STRING” or the first character of a string “STRING”.</p><blockquote><strong>In C <em>string</em> is just an array of characters which means there is no such thing as string in C — In fact, string in C is just a pointer to the sequence of characters as described above.</strong></blockquote><p>So instead of using <strong>string</strong> s <strong>=</strong> &quot;STRING&quot;;</p><blockquote>we use <strong>char</strong> *s <strong>=</strong> &quot;STRING&quot;;</blockquote><p>char * represents a pointer to a character:</p><pre>{<br>    <strong>char</strong> <strong>*</strong>s <strong>=</strong> &quot;STRING&quot;;<br>    printf(&quot;<strong>%s</strong>\n&quot;, s);<br>}</pre><p>: will print out STRING</p><pre>{<br>    <strong>...</strong><br>    printf(&quot;<strong>%p</strong>\n&quot;, s);<br>}</pre><p>: will output a pointer or an address (of the first character in the string) e.g. 0x123</p><pre>{<br>    <strong>...</strong><br>    printf(&quot;<strong>%p</strong>\n&quot;, &amp;s[0]);<br>    printf(&quot;<strong>%p</strong>\n&quot;, &amp;s[1]);<br>    printf(&quot;<strong>%p</strong>\n&quot;, &amp;s[2]);<br>    printf(&quot;<strong>%p</strong>\n&quot;, &amp;s[3]);<br>    printf(&quot;<strong>%p</strong>\n&quot;, &amp;s[4]);<br>}</pre><p>: will output the addresses of each character of the string, starting from the first character s[0] e.g. 0x123 0x124 0x125 0x126 0x127…</p><pre>{<br>    <strong>...</strong><br>    printf(&quot;<strong>%c</strong>\n&quot;, *s);<br>}</pre><p>%c is a placeholder for a <em>character</em></p><p>: will prints out S</p><pre>printf(&quot;%c\n&quot;, *(s+1));</pre><p>: prints out T</p><pre>printf(&quot;%c\n&quot;, *(s+2));</pre><p>: prints out R</p><p>And so on …</p><h4>Let’s take a look at something quite similar to C in JavaScript for a second:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*s3MXhCR7GUMsAJvRrKddQw.png" /><figcaption>in JavaScript console</figcaption></figure><p>While we are not getting addresses as return values. <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob">Blob.size</a> returns the size, in bytes, of the data contained in the Blob object.</p><p>The similarity is when the variable got declared and assigned value to, JavaScript underneath the hood creates a unique identifier or an address for the variable, and store that value next to it. — Apparently string “hello” contains <em>5 bytes</em> in memory which would be the same with a sequence of characters in C.</p><p>While this might not be something important to learn, it’s really interesting how things work in the lower level, sometimes having things working like magic makes us a little bit blind, you never know how many things are working beyond (behind) what we can see.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6043f0606520" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My First Command Line Application]]></title>
            <link>https://medium.com/@kkkraj/my-first-command-line-application-172820292501?source=rss-636df410fb02------2</link>
            <guid isPermaLink="false">https://medium.com/p/172820292501</guid>
            <category><![CDATA[command-line-interface]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[crud]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Khanittha Krajangjaem]]></dc:creator>
            <pubDate>Tue, 26 May 2020 23:37:13 GMT</pubDate>
            <atom:updated>2020-06-08T20:48:11.847Z</atom:updated>
            <content:encoded><![CDATA[<p>After two-plus weeks of heavily dreaming in code, it’s time for more code and more code in dreams… Module 1 final project!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F10UeedrT5MIfPG%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F10UeedrT5MIfPG%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F10UeedrT5MIfPG%2Fgiphy.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="317" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fdd469ebc0755fbea496ca0e12f3ddd/href">https://medium.com/media/0fdd469ebc0755fbea496ca0e12f3ddd/href</a></iframe><p>After a rough couple of weeks with Ruby, the idea of “what am I going to make?” for a Mod1 final project kicks in as easy as the morning sun. Since we are parents who happen to have children around the same age, my project partner and I have decided to make a <em>Command Line App</em> for preschoolers to have fun learning math, we call it — “<a href="https://github.com/KhanitthaKraj/ruby-project-guidelines-sfo01-seng-ft-042020.git">Little Math Lab 🧸</a>”.</p><blockquote><strong>PROJECT GOAL:</strong> The requirement is to create a <strong>Command Line CRUD App </strong>using the skills we’ve learned in Mod1 such as Ruby Object Orientation, ActiveRecord Associations, and SQL.</blockquote><h4>START BUILDING WITH BASIC STEPS</h4><ol><li>Make data <strong>Models</strong> that are associated with each other by a <a href="https://guides.rubyonrails.org/association_basics.html">Many to Many Relationship</a>, each model inherits from ActiveRecord::Base. In this case — Play is a <em>join model</em>.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5958d1b990bee886cdc371696eef4b1/href">https://medium.com/media/e5958d1b990bee886cdc371696eef4b1/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba4f531ca7f9fb586a2908bd990275a5/href">https://medium.com/media/ba4f531ca7f9fb586a2908bd990275a5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/395c252969b0330d6597a58af2a10ef7/href">https://medium.com/media/395c252969b0330d6597a58af2a10ef7/href</a></iframe><p>2. Create <a href="https://guides.rubyonrails.org/active_record_migrations.html"><strong>Migrations</strong></a>, and run rake db:migrate, this is an example of what my <a href="https://guides.rubyonrails.org/active_record_migrations.html#what-are-schema-files-for-questionmark">schema</a> look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/590664948c7d62a90cb1e5919e57c739/href">https://medium.com/media/590664948c7d62a90cb1e5919e57c739/href</a></iframe><p>3. Create a Seed file to seed your database, then runrake db:seed — I did not use the <a href="https://github.com/faker-ruby/faker">Faker Gem</a> but you should check it out!</p><h3>Continue with the Execution</h3><p>Now that my Models, Relationships, and Database are set up, for a good abstraction I create another model called cli.rb for creating and managing a Command Line Interface(CLI). And run any method(s) from the CLI model in bin/run.rb and because it is a run file, you’ll have to run ruby bin/run.rb to run your application. For example my run.rb looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94d5500eee1a67718e95f5cba0147aae/href">https://medium.com/media/94d5500eee1a67718e95f5cba0147aae/href</a></iframe><h3>THE APPLICATION</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*WXX5UofS0qixKRR5L31f5A.gif" /><figcaption><strong>WELCOME PAGE: </strong>with some greeting messages</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*FdhXSKMyIdORaBvmDDcTQw.gif" /><figcaption><strong>LOG-IN or SIGN-UP</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*19bCuA2pS5EsviEuUxSHvg.gif" /><figcaption><strong>LOG-IN: </strong>If user has played before, simply provide the username to log-in.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*z0z2rbvALFEEH8YUHsN2FQ.gif" /><figcaption><strong>SIGN-UP: </strong>If this is the first time visiting the app then register with simple steps.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*RwimogzmsxWaBfVQWaIgoA.gif" /><figcaption><strong>MAIN MENU: MY ACCOUNT:</strong> User has three options: View / Update / Delete Account.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*DuFlIFJC1ZXlHmhncWraBw.gif" /><figcaption><strong>MAIN MENU: PLAY GAME: </strong>There are five questions per play. Users will be given two chances to provide the correct answer. After the second attempt, if the answer is still incorrect, the app will generate the next question. Scores will be outputted once the game is finished. User has options to go back to the main menu and start over or quit the game.</figcaption></figure><p>Thanks to <a href="https://github.com/piotrmurach/tty-prompt">TTY::Prompt</a> and <a href="https://github.com/fazibear/colorize">colorize</a> gems, my app is filled with so many fun features — <em>Below is an example of how I utilize these gems.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e45f49b431829ec03c881433c9440b6b/href">https://medium.com/media/e45f49b431829ec03c881433c9440b6b/href</a></iframe><blockquote>lastly, thank you — <em>Radniel Padron</em> — my project partner for working hard together!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=172820292501" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>