<?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 Matthew Masiello on Medium]]></title>
        <description><![CDATA[Stories by Matthew Masiello on Medium]]></description>
        <link>https://medium.com/@sotek222?source=rss-4958cd1420ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kfCMoIOVmZu2Gceh2andcg.jpeg</url>
            <title>Stories by Matthew Masiello on Medium</title>
            <link>https://medium.com/@sotek222?source=rss-4958cd1420ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 07:54:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sotek222/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[Journey to the center of the (virtual) DOM]]></title>
            <link>https://medium.com/@sotek222/journey-to-the-center-of-the-virtual-dom-7121cd908b3?source=rss-4958cd1420ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/7121cd908b3</guid>
            <category><![CDATA[dom]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vdom]]></category>
            <dc:creator><![CDATA[Matthew Masiello]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 12:45:42 GMT</pubDate>
            <atom:updated>2019-04-04T12:45:42.405Z</atom:updated>
            <content:encoded><![CDATA[<h3>In the beginning there was the DOM and it was good…</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*UkBik4Db4hk81QuguOT8Iw.jpeg" /><figcaption>The DOM is often described as a tree with branches and nodes</figcaption></figure><p>The React framework utilizes a rather interesting and vital concept at its core. This concept is the virtual DOM…</p><p>…But what is the Virtual DOM?</p><p><strong>The React documentation defines it thusly</strong>:</p><p><em>“The virtual DOM (VDOM) is a programming concept where an ideal, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM by a library such as ReactDOM.</em>”</p><p>Okay cool but why would we want to virtually represent the DOM and interact with this facsimile, rather than the DOM itself?</p><p>I think it is important to first define and understand the regular DOM before we look at its abstraction.</p><h3><strong>THE DOM:</strong></h3><p>When a user agent or web browser receives a document/file that is structured in either XML or HTML it serves up an abstraction of that document that displays in the viewport. This is what is known as the Document Object Model or DOM.</p><p>The DOM takes all of the HTML elements and represents them in a hierarchical structure known as a DOM tree or Node Tree. These nodes can be selected/created/updated by using a scripting language. That scripting language is Javascript…</p><h4>You fought in Browser wars?!?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*s5Md2p-kwGHNSUnww1TBxw.gif" /><figcaption>They sure don’t make em like they used to.</figcaption></figure><p>With Javascript, developers are able to manipulate the DOM and create a highly dynamic and fast web site. The reason for Javascript becoming the language of the web is tied into the browser wars of the 90s. To quickly summarize, from 1995 to 2001 the Netscape Navigator and its opponent Internet Explorer created different ways for their iterations of JS (Javascript and JScript) to interact with websites. This is how the DOM came to be. This was all done in an effort to subvert customers to their browser. Ultimately Internet Explorer won out…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*F5Fs1cVppWaPQFtT444LGg.jpeg" /></figure><p>Eventually it came to the point where browsers were all designed to interpret Javascript and every update from then to now has further cemented this fact. The DOM became the most prominent API for interacting with the HTML.</p><h3>THE VDOM:</h3><p>Alright so now that we understand that the DOM is a representation or abstraction of the HTML file, what then does the follow React documentation snippet mean to us?</p><p><em>“…an ideal, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM…”</em></p><p>Well the virtual part means that it is a representation of the DOM…</p><p>So… the VDOM is an abstraction off of an abstraction…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*wqh4outJSzzDweP9ssc35w.png" /></figure><p>Nice… but why is that useful?</p><p>The VDOM can be thought of as React’s carbon copy of the DOM and it gives React the ability to quickly and efficiently manipulate elements and make sure that what is rendered appears in the actual DOM.</p><p>For the developer this is extremely useful as previous to this in order to manipulate an element you would have to first select or create it and then begin the manipulation.</p><p>This is fine when there are only a few elements on the DOM tree but many modern websites are made up of hundreds of nodes that would need to be selected and manipulated by hand.</p><p>In addition to that problem another problem is that many times the DOM will get updated needlessly.</p><p>React takes care of all of this through its directiveness and allows for all of the selecting, creating and updating to be taken care of under the hood. React will also only update the VDOM when an actual change is registered.</p><p><strong>React utilizes two other important concepts, React Elements, and React Components:</strong></p><p><strong>Reacts elements</strong> are objects represented in memory that can be directly manipulated quickly and then rendered onto the VDOM, which is synced with the actual DOM. These elements are the equivalent of the traditional HTML elements like &lt;p&gt;, &lt;h1&gt;, etc. These elements are immutable and inexpensive to make which gives React its high speed and dynamism. Unfortunately they are many useful for creating elements and that’s about it. They are not aware of state so they are used as good fundamental building block.</p><p><strong>React Components</strong> are JS classes that extend from the React component module. This module allows components to carry state. State gives the component the ability to change what&#39;s happening on the DOM and allows the program to be aware of those changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*OMwUSmVixo0F8gJfSh2ONA.jpeg" /></figure><p>The difficulty with components is that since they are actually just JS classes they have no actual direct interaction with the VDOM. React handles this by taking the component and its JSX and changing it into a React Element which is then placed onto the VDOM… which is then synced with the actual DOM.</p><p>This process may seem unnecessarily cumbersome and complicated but in actuality thanks to React’s diffing algorithm it is faster than traditional DOM manipulation.</p><p>So in short the VDOM is React’s “version” of the real DOM that it manipulates when necessary and renders to the actual DOM.</p><p>So to wrap things up…</p><p>React is a very powerful and interesting tool for frontend web development. This tool relies on a very important pattern known as the Virtual DOM. This is a representation of a representation of the HTML file sent by a server over the internet. Its origins are found during the portentous browser wars of the 90s. It is important to not underestimate or undervalue this tool as it is actually how/why the core of React works so quickly.</p><h4>Sources:</h4><ul><li><a href="https://reactjs.org/docs/faq-internals.html">Virtual DOM and Internals - React</a></li><li><a href="https://reactkungfu.com/2015/10/the-difference-between-virtual-dom-and-dom/">The difference between Virtual DOM and DOM</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction">Introduction to the DOM - Web APIs | MDN</a></li><li><a href="https://www.codecademy.com/articles/react-virtual-dom">React: The Virtual DOM | Codecademy</a></li><li><a href="https://en.wikipedia.org/wiki/Document_Object_Model"></a></li><li><a href="https://en.wikipedia.org/wiki/Browser_wars#First_Browser_War:_1995-2001">Browser wars - Wikipedia</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7121cd908b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Seed your Database with fakes using Faker]]></title>
            <link>https://medium.com/@sotek222/seed-your-database-with-fakes-using-faker-5ba5dccda44f?source=rss-4958cd1420ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ba5dccda44f</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[seed-data]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[gem]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Matthew Masiello]]></dc:creator>
            <pubDate>Thu, 07 Mar 2019 12:10:12 GMT</pubDate>
            <atom:updated>2019-03-07T12:10:12.231Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first began using Ruby on Rails I realized that compared to Rack, and Sinatra there was some serious streamlining and automation. I found that Rails could cut the amount of time spent writing boilerplate code into almost zero.</p><p>With this in mind, one of the attributes that I like about being a programmer is that <strong>it&#39;s good to be lazy</strong>.</p><p>With Rails, I could be lazy and still get a lot of groundwork covered. This gave me more time to spend on solving more complicated problems related to my app.</p><p>But… even with all this streamlining there are still some areas of building out the back-end that take up more time than it should.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6W-lX9mu1co4GpTeBfIvbA.jpeg" /></figure><h4>Seeding a Database</h4><p>One area that is particularly tedious is creating seed data that will be useful and can actually represent what might actually exist in a fully deployed project.</p><p>On the one hand I want to save time and on the other I want enough data that is reflective of the general randomness that users can bring to an app.</p><p>Luckily the awesome members of the ruby community can always be relied on to swoop in and save the day… this time it’s with the Faker gem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*h0T_OHMS8MaRhYDkVw3y5Q.jpeg" /><figcaption>Two of my “random” users…</figcaption></figure><p>As ridiculous as this may sound one aspect of creating seed data that is so tedious is thinking up random names and/or other information related to my models.</p><p>With the Faker gem this is all taken care of for me.</p><p>Faker is a gem that you can use to create “fake” data. You can then use that data when you write your seed file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VMH8qjDdR8k_cpQHK5uWWA.png" /></figure><p>okay the bottom one is clearly easier to just copy and paste …but what does it return?</p><p>Here&#39;s an example of just three new users:</p><pre>Name: Elfreda Volkman, Job: Central Manager, quote: Imagine you are writing an email. You are in front of the computer. You are operating the computer, clicking a mouse and typing on a keyboard, but the message will be sent to a human over the internet. So you are working before the computer, but with a human behind the computer.</pre><pre>Name: Ethan Gulgowski, Job: IT Liaison, quote: People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that&#39;s what I&#39;ve tried to do.</pre><pre>Name: Beverley Harris III, Job: Regional Officer, quote: Everyone has an individual background. Someone may come from Python, someone else may come from Perl, and they may be surprised by different aspects of the language. Then they come up to me and say, &#39;I was surprised by this feature of the language, so therefore Ruby violates the principle of least surprise.&#39; Wait. Wait. The principle of least surprise is not for you only.</pre><p>Thats a lot of data… its a lot easier letting the gem do it for me.</p><p>But as I said earlier I’m lazy, so if all I had to do was copy and paste it as many times as I needed data, than I could just loop it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yxR_Vc7Kmjt97Hv-iZyoPA.png" /><figcaption>Laziness at its finest!</figcaption></figure><p>That&#39;s a lot of seed data all in three lines!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*QqMKtmsMBV04Nr1uECXrIg.jpeg" /><figcaption>So many unique users</figcaption></figure><h4>The anatomy of a Faker</h4><p>So what does it take to be a Faker? …well it’s actually quite simple:</p><p>First, you have to install the gem itself</p><pre>gem install faker</pre><p>Second, you just require it in your seed.rb file</p><pre>require &#39;faker&#39;</pre><p>Third, when you create a new instance to be saved in your database, rather than passing in actually hand-typed data, you use the Faker syntax like so:</p><pre>Dog.create(name: Faker::Creature::Dog.name, breed: Faker::Creature::Dog.name, age: Faker::Creature::Dog.age)</pre><p>So as you can see you use the Faker module, within the namespace “Creature” and then within the namespace of “Dog”, and finally append it with the desired trait, like: name, age, breed, etc.</p><p>Now obviously there is a limit to what can be done with Faker. You can’t have a random made up trait like:</p><pre>Faker::Creature::Dog.favorite_video_game</pre><pre>==&gt; `handle_exception&#39;: translation missing</pre><p>This may make the gem seem less useful, but in truth there are a lot of different namespaces to use.</p><p>Looking at the docs there are <strong>165 different namespaces</strong> all with their own attributes.</p><p>From things like seinfeld characters (with traits like name, quote, company) to lists of fake phone numbers.</p><p>So hopefully the gem will have something you need.</p><p>If it doesn’t have what you need you can always contribute…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*UI9_AfpuQg-U2sM8VkCP_A.gif" /></figure><p>Sources:</p><ul><li><a href="https://github.com/stympy/faker">stympy/faker</a></li><li><a href="http://www.xyzpub.com/en/ruby-on-rails/3.2/seed_rb.html">4.5. Populating the Database with seeds.rb</a></li><li><a href="https://edgeguides.rubyonrails.org/active_record_migrations.html">Active Record Migrations - Ruby on Rails Guides</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ba5dccda44f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sewing programs with the Ruby Thread Class]]></title>
            <link>https://medium.com/@sotek222/sewing-programs-with-the-ruby-thread-class-7de27ca40e0d?source=rss-4958cd1420ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/7de27ca40e0d</guid>
            <category><![CDATA[flatiron-school]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[blog]]></category>
            <dc:creator><![CDATA[Matthew Masiello]]></dc:creator>
            <pubDate>Fri, 15 Feb 2019 02:55:36 GMT</pubDate>
            <atom:updated>2019-02-15T02:55:36.829Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*M7cz0YLevCl9h0qfFZwigg.jpeg" /></figure><p>Imagine yourself as a burgeoning programmer. One of your goals might be to build a terminal game using Ruby. Not a very robust or complicated game, but something a little more humble and retro. Something like a text-adventure would do nicely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*QpZUQ28A1aIkOfWgOHdzcw.gif" /></figure><p>As you begin to create the game you use all sorts of cool programming tricks like a while loop to keep the game running, classes to hold player information, control flow, gets.chomp, etc, etc. At this point your game is looking pretty nifty, but then you think, wait… I need some epic music to go along with the story! Well, you could always use afplay to run music but then what if I want to play music without stopping and have it loop throughout the game?</p><p>This is the point in my own attempt at making a game where I became very confused and frustrated. I tried several gems, an mp3 class; whatever I tried never ended up working. It felt like a hopeless endeavour…</p><p>Enter concurrent programming with the Ruby Thread Class……</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*qg8BiaxHxcIe_y816F5X5g.gif" /><figcaption>What it looks like to use threads in Ruby</figcaption></figure><p>According to the Ruby docs a program that requires multiple “threads” of execution is a good candidate for the Thread class.</p><p>Hmm… maybe this would work for having music that plays in the background. The Thread class is Ruby’s way of implementing concurrent programming.</p><p><strong>So what is concurrent programming anyway?</strong></p><p>A concurrent program is one in which several computations execute in a non-sequential or overlapping manner. Ruby in its natural form is not a concurrent programming language. It is sequential which means each computation must finish before the next one can start.</p><p>So imagine without the thread class how would this example execute?:</p><pre>loop do<br>  fork{exec &quot;afplay&quot;, &quot;lib/music/song_one.mp3&quot;}<br>  puts &quot;Playing Song...&quot;<br>  sleep 168 # song length<br>end</pre><pre>puts &quot;Hello World!&quot;</pre><pre>==&gt; &quot;Playing Song...&quot;<br>    (168 seconds later)<br>    &quot;Playing Song...&quot;<br>    (168 seconds later)<br>    &quot;PLaying Song...&quot;<br>    etc...</pre><p>Well that&#39;s not what we want at all…</p><p>The program will never reach the next puts. This is due to Ruby kindly executing exactly what is written in a sequential way. So in this case we are stuck in an infinite loop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*SCqlK5To2xDcv178lAgwow.gif" /><figcaption>Rev up those loops…</figcaption></figure><p>Now lets try the same thing using the thread class while we ask the user for some input?:</p><pre>require &#39;thread&#39;</pre><pre>Thread.new do<br>  loop do<br>    fork{exec &quot;afplay&quot;, &quot;lib/music/song_one.mp3&quot;}<br>    sleep 168<br>  end<br>end</pre><pre>puts &quot;Whats up?&quot;<br>response = gets.chomp</pre><pre>while response != &quot;not much&quot; do <br>  puts &quot;Whats up?&quot;<br>  response = gets.chomp<br>end</pre><p>So in this case the terminal will execute the afplay command first, then it will sleep for the length of the song, then it will loop.</p><p>Because of the thread class Ruby knows to move on to the next method while it concurrently is running the other executions. As long as my answer is never “not much” my program will live, and my music will play on loop for as long as the program is running!</p><h3>So how do we set up our threads?</h3><p>First and foremost ruby does not include the thread class as part of its core library, so you must require it before you can initialize a new thread instance.</p><pre>require &#39;thread&#39;</pre><p>With Ruby ready, it’s as easy as initializing a new thread instance:</p><pre>def tell_me_time<br>  Thread.new do<br>    loop do<br>      puts Time.now<br>      sleep 300 # 5 minutes  <br>    end<br>  end<br>end</pre><p>Once it has been initialized, you can simply call the method “tell me the time” and every five minutes Ruby will puts the current time.</p><h3>More Fun With Threads!</h3><p>So Threads can allow you to run multiple concurrent executions at once. Seems pretty powerful. So what are some other useful tricks we can do with threads…</p><p><strong>#join</strong></p><p>This method allows a thread to take over as the main thread even if the main program has ceased executing.</p><pre>require &#39;thread&#39;</pre><pre>puts_world = Thread.new { puts &quot;world!&quot; }</pre><pre>puts &quot;hello&quot;<br>puts_world.join</pre><pre>=&gt; &quot;hello&quot;<br>   &quot;world!&quot;</pre><p>So what would happen if we did not use #join?</p><pre>require &#39;thread&#39;</pre><pre>puts_world = Thread.new { puts &quot;world!&quot; }</pre><pre>puts &quot;hello&quot;<br>puts_world</pre><pre>=&gt; &quot;hello&quot;</pre><p>This occurs because without #join the thread will be killed as soon as the main program finishes its final execution.</p><p>Another cool feature of the join method is that it can pass executions along a chain:</p><pre>require &#39;thread&#39;</pre><pre>puts_world = Thread.new { puts &quot;world!&quot; }</pre><pre>puts_woohoo = Thread.new { puts &quot;woohoo!&quot;}</pre><pre>puts &quot;hello&quot;</pre><pre>puts_world.join<br>puts_woohoo.join</pre><pre>=&gt; &quot;hello&quot;<br>   &quot;world!&quot;<br>   &quot;woohoo!&quot;</pre><p>Using #join can make for some powerful dynamic programs.</p><p><strong>#stop</strong></p><p>This method is useful for stopping/pausing the execution of a thread. It puts the thread to sleep.</p><p>But how do we wake it up?</p><p><strong>#wakeup</strong></p><p>This method is pretty self evident. It is able to wake up a thread that was previously put to sleep.</p><p>If other functions are running as a priority, it gets scheduled for wakeup which means it may not run immediately on it being called.</p><p><strong>#kill</strong></p><p>This method kills the called thread. Ruby will then begin to run another thread or end the process if it is the final thread.</p><h3>Following the thread back</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*gMbablOHh3rgpIBi7KhpcQ.gif" /></figure><p>Ruby is a great language. It is fun to write, looks beautiful, and is nice to beginners. It offers many tricks and useful tools, like the thread class. The best part of learning how to program is discovering all these new tools and figuring out ways to use them in fun and interesting ways.</p><p>Sources:</p><ul><li><a href="https://ruby-doc.org/core-2.2.0/Thread.html">Class: Thread (Ruby 2.2.0)</a></li><li><a href="https://www.tutorialspoint.com/ruby/ruby_multithreading.htm">Ruby Multithreading</a></li><li><a href="https://en.wikipedia.org/wiki/Concurrent_computing">Concurrent computing - Wikipedia</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7de27ca40e0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>