<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
  <channel>
    <title>megbits</title>
    <link>http://localhost/</link>
    <atom:link href="http://localhost/feed.xml" rel="self" type="application/rss+xml"/>
    <description>A tiny blog with posts about programming and QA testing. All posts are written by a human.</description>
    <lastBuildDate>Sun, 30 Aug 2026 21:41:55 GMT</lastBuildDate>
    <language>en</language>
    <generator>Lume 3.3.1</generator>
    <item>
      <title>The Monty Hall problem in Ruby</title>
      <link>http://localhost/posts/monty-hall-problem-ruby/</link>
      <guid isPermaLink="false">http://localhost/posts/monty-hall-problem-ruby/</guid>
      <content:encoded>
        <![CDATA[<p>The <a href="https://en.wikipedia.org/wiki/Monty_Hall_problem">Monty Hall Problem</a> is a probability brain teaser based on the game show &quot;Let's Make a Deal&quot;, where Monty Hall is the host.</p>
<!--more-->
<p>The basic premise is the host, Monty Hall, presents 3 closed doors to a contestant, one of which hides a new car, and the other 2 doors contain goats. The goal for the contestant is to choose the door hiding the new car.</p>
<p>To start the game, the contestant chooses 1 of the 3 doors. But before they can see what's inside, the host opens one of the remaining doors where this is a goat.  The host then offers the contenstant the option to switch their door selection to the other remaining door or to stick with their first choice.</p>
<p>The brain teaser: Is it better for the contestant to stay, switch, or does it not matter?</p>
<p>To find out, I wrote a Ruby script, and added in a bit of Lord of the Rings by changing goats to orcs and setting the prize as farthings.</p>
<h2 id="ruby-script-with-lord-of-the-rings-theme" tabindex="-1"><a href="http://localhost/posts/monty-hall-problem-ruby/#ruby-script-with-lord-of-the-rings-theme" class="header-anchor">Ruby script with Lord of the Rings theme</a></h2>
<pre><code class="language-ruby">def green_dragon

  stay_wins = 0
  switch_wins = 0
  show_door = nil
  switch_door = nil

  tries = 10000

  tries.times do
    initial_guess = rand(3)
    door_items = [&quot;500 farthings&quot;, &quot;1 orc&quot;, &quot;1 orc&quot;].shuffle!
    door_items.each_with_index do |item, door_index|
      if door_index != initial_guess and item != &quot;500 farthings&quot;
        show_door = door_index
      end
    end
    
    switch_door = ((0..2).to_a - [initial_guess, show_door])[0]

    if door_items[initial_guess] == &quot;500 farthings&quot;
      stay_wins += 1
    end

    if door_items[switch_door] == &quot;500 farthings&quot;   
      switch_wins += 1
    end

  end

  stay_wins = (stay_wins.to_f / tries.to_f) * 100
  switch_wins = (switch_wins.to_f / tries.to_f) * 100
  results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}
 
end
</code></pre>
<h2 id="code-breakdown" tabindex="-1"><a href="http://localhost/posts/monty-hall-problem-ruby/#code-breakdown" class="header-anchor">Code breakdown</a></h2>
<p><code>def green_dragon</code></p>
<p>I define a method and give it a name - in this case <code>green_dragon</code></p>
<p><code>stay_wins = 0</code> and <code>switch_wins = 0</code></p>
<p>To get a count of wins won by staying and a count of wins won by switching, I add the two variables stay_wins and switch_wins, and set their starting count to 0.</p>
<pre><code class="language-ruby">show_door = nil
switch_door = nil
</code></pre>
<p><code>show_door</code></p>
<p>Represents one of the doors containing an orc that the hosts shows the contestant after their initial selection.</p>
<p><code>switch_door</code></p>
<p>Represents the door the hosts offers the contestant to switch to instead of staying with their initial door selection.</p>
<p><code>= nil</code></p>
<p>While each door will hold a single index value (0,1,2) during each iteration, they have no value at the start, and begin with <code>nil</code> as their initial value.</p>
<p><code>tries = 10000</code></p>
<p>This variable represents the number of times the do / end statement will run. 10000 will give us a good enough sample to draw a conclusion, but can be any integer (50000, 1230000).</p>
<p><code>tries.times</code></p>
<p><code>times</code> is a public ruby method for iterating over a block a certain number of times - in this case, 1000 times as defined by the tries variable.</p>
<p><code>initial_guess = rand(3)</code></p>
<p><code>initial_guess</code> is the guest's first door selection. <code>rand</code> sets the index value within initial_guess to random through each iteration of the loop. (3) gives us three index values for the three doors (0, 1, 2)</p>
<p><code>door_items = [&quot;500 farthings, &quot;1 orc&quot;, &quot;1 orc&quot;].shuffle!</code></p>
<p>The door_items array [&quot;500 farthings&quot;, &quot;1 orc&quot;, &quot;1 orc&quot;] gives us the items behind the doors.</p>
<p><code>shuffle</code> is a method that shuffles items in an array through each iteration.</p>
<p><code>!</code> (often referred to as bang) modifies the object it's called on.  It is ruby convention to use <code>!</code> at the end of a method when something will be modified in your code.</p>
<p><code>door_items.each_with_index do |item, door_index|</code></p>
<p>This will iterate through each index of the door items.</p>
<p>The variables in the pipe <code>|item, door_index|</code> are placeholders. <code>item</code> represents the item string (such as &quot;1 orc&quot;), and <code>door_index</code> represents the index number of the item (0, 1, or 2). Naming it <code>door_index</code> makes it clear the value corresponds to a door position.</p>
<pre><code class="language-ruby"> if door_index != initial_guess and item != &quot;500 farthings&quot;
  show_door = door_index
 end
</code></pre>
<p>This is an if conditional statement.  It states if the index does not equal the initial_guess and if the item does not equal the desired prize (&quot;farthings&quot;), then that index belongs to the show_door.
The 1st end ends the conditional statement, and the second end ends the each do loop.</p>
<p><code>switch_door = ((0..2).to_a - [initial_guess, show_door])[0]</code></p>
<p>This statement finds the switch_door.  (0..2).to_a takes the potential index value of the three doors (0,1,2) and puts them in an array [0,1,2].  - [intial_guess, show_door] subtracts the indices belonging to initial_guess and show_door, leaving you with the index value of the switch_door in an array.  Now we need the index value out of array form, and that's where [0] comes in.  This grabs the value of the 0 index position of an array.</p>
<pre><code class="language-ruby"> if door_items[initial_guess] == &quot;500 farthings&quot;
   stay_wins += 1
 end
 if door_items[switch_door] == &quot;500 farthings&quot;   
   switch_wins += 1
 end
</code></pre>
<p>The first conditional statement finds out if the initial_guess item is farthings, and if so add 1 win to stay_wins.  The second conditional find out if the switch_door contains the farthings and if so add 1 win to switch_wins.  The 1st two ends conclude the conditional statements, and the last end take us out of the loop.</p>
<pre><code class="language-ruby">  stay_wins = (stay_wins.to_f / tries.to_f) * 100
  switch_wins = (switch_wins.to_f / tries.to_f) * 100
  results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}
</code></pre>
<p><code>stay_wins</code> and <code>switch_wins</code> now contain how many times a contestant sticking with their initial selection won over how many times they played, then converts the answer to a percentage.</p>
<p>To calculate the variables, they need to be in float form .to_f</p>
<p><code>results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}</code></p>
<p>We need to put <code>stay_wins</code> and <code>switch_wins</code> into a hash, otherwise the value of the variables will come up as nil.  A hash is an associative array of key, value pairs.  While an array has default index integer values (0,1,2,3...) to reference an item within the array, hashes allow you to use any object type.</p>
<p>For example if I wrote this hash: animals = {&quot;monkeys&quot; =&gt; &quot;primates&quot;, &quot;sharks&quot; =&gt; &quot;fish&quot;}.  I could type: <code>puts animals[&quot;monkeys&quot;]</code> and I would get the string &quot;primates&quot; as the result.</p>
<h2 id="the-conclusion" tabindex="-1"><a href="http://localhost/posts/monty-hall-problem-ruby/#the-conclusion" class="header-anchor">The conclusion</a></h2>
<p>It is best for the contestant to choose switch over stay.  Consistently the results showed choosing to stay meant 1/3 odds you will win, while switching gives you 2/3 odds.</p>
]]>
      </content:encoded>
      <category>ruby</category>
      <category>probability</category>
      <pubDate>Sun, 24 Mar 2024 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Generate a zip from the terminal</title>
      <link>http://localhost/posts/generate-zip-command/</link>
      <guid isPermaLink="false">http://localhost/posts/generate-zip-command/</guid>
      <content:encoded>
        <![CDATA[<p>Here's a quick command for generating a zip from the terminal.</p>
<!--more-->
<p>Run in the directory you'd like to zip:</p>
<pre><code>zip -r zip.zip ./ -x *.git* -x *.zip*
</code></pre>
<p>Zip is a command line utility and you can find the <a href="https://linux.die.net/man/1/zip">man page here</a>.</p>
<p>You can also view the man page in your terminal with the command:</p>
<p><code>man zip</code></p>
]]>
      </content:encoded>
      <category>terminal</category>
      <category>zip</category>
      <pubDate>Wed, 03 May 2023 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Replace a webpage with only the text that interests you</title>
      <link>http://localhost/posts/replace-web-page-with-only-text-that-interests-you/</link>
      <guid isPermaLink="false">http://localhost/posts/replace-web-page-with-only-text-that-interests-you/</guid>
      <content:encoded>
        <![CDATA[<p>Sometimes information you're looking for on a website is not (1) available at a quick glance (ie. no parsing through non-essential elements), and (2) easy to extract for other purposes, such as doing a comparison between different projects or tracking changes to a project.</p>
<p>One solution for this is to use browser developer tools and a little javascript.</p>
<!--more-->
<h2 id="javascript-example" tabindex="-1"><a href="http://localhost/posts/replace-web-page-with-only-text-that-interests-you/#javascript-example" class="header-anchor">Javascript example</a></h2>
<p>As a quick example, let's say you'd like to only see a numbered text list of <a href="https://airbyte.com/connectors">what connectors available for Airbyte</a>.</p>
<p>You can open your browser's developer tools and run the following javascript in the console.</p>
<p>Note: If you're unsure how to use your browser's developer tools, you may find <a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools">Mozilla's article here</a> helpful to review.</p>
<pre><code class="language-javascript">// Get all the elements containing the names of the integrations and stores them in a variable.
let title = document.documentElement.getElementsByClassName('card-title');

// Create an empty array called 'titlesArray' to store the text of each 'card-title' element
var titlesArray = [];

// Loop through each 'card-title' element
for (const element of title) {
  // Add the text of the current 'card-title' element to the 'titlesArray' array
  titlesArray.push(element.innerText)
}

// Sort the 'titlesArray' array alphabetically
let titlesArraySort = titlesArray.sort();

// Create a new ordered list element using the createElement method of the Document Object Model (DOM)
let list = document.createElement('ol');

// Loop through each element in the 'titlesArraySort' array
for (let i=0; i&lt;titlesArraySort.length; i++){ 
  // Create a new list item element using the createElement method of the DOM
  let item = document.createElement('li'); 
  // Set the text of the list item to the current element in the 'titlesArraySort' array
  item.innerText = titlesArraySort[i]; 
  // Append the new list item to the ordered list element created earlier
  list.appendChild(item); 
} 

// Define a function called 'removeAllChildNodes' that will remove all child nodes from a given parent element
function removeAllChildNodes(parent) { 
  while (parent.firstChild) { 
    parent.removeChild(parent.firstChild); 
  } 
} 

// Get a reference to the &lt;body&gt; element using the querySelector method of the DOM
let bodyElement = document.querySelector('body'); 

// Remove all child nodes from the &lt;body&gt; element using the removeAllChildNodes function
removeAllChildNodes(bodyElement);

// Append the new ordered list element to the &lt;body&gt; element using the appendChild method of the DOM
bodyElement.appendChild(list);

// Add margin and padding for better readability

listElement.style.marginLeft = &quot;50px&quot;
list.style.padding = &quot;50px&quot;

</code></pre>
<h2 id="images-of-the-webpage-before-%26-after-javascript" tabindex="-1"><a href="http://localhost/posts/replace-web-page-with-only-text-that-interests-you/#images-of-the-webpage-before-%26-after-javascript" class="header-anchor">Images of the webpage before &amp; after javascript</a></h2>
<h3 id="before" tabindex="-1"><a href="http://localhost/posts/replace-web-page-with-only-text-that-interests-you/#before" class="header-anchor">Before</a></h3>
<p><img src="https://cdn.pagepixels.com/4e8ace79-fdd2-4cfa-b1e4-38613e7efbf8/9739e85c5103ad-1682306986977.jpg" alt="airbyte's connectors page before adding javascript"></p>
<h3 id="after" tabindex="-1"><a href="http://localhost/posts/replace-web-page-with-only-text-that-interests-you/#after" class="header-anchor">After</a></h3>
<p><img src="https://cdn.pagepixels.com/b1b945e6-97f5-4738-a471-678e19ff527f/ca2a0f8a78bbe8-1682315461803.jpg" alt="Airbyte's integration page after applying the above javascript"></p>
]]>
      </content:encoded>
      <category>javascript</category>
      <pubDate>Fri, 04 Mar 2022 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Local to remote server changes via scp</title>
      <link>http://localhost/posts/local-to-remote-server-changes-via-scp/</link>
      <guid isPermaLink="false">http://localhost/posts/local-to-remote-server-changes-via-scp/</guid>
      <content:encoded>
        <![CDATA[<p>Just some scp commands to remember for later.</p>
<!--more-->
<p>To push changes from a local file to a remote server, use scp in the local terminal:</p>
<p><code>$scp index.html username@ip-address:/var/www/desired-directory</code></p>
<p>To push changes from the entire local directory use scp -r ./ in the local terminal:</p>
<p><code>scp -r ./ username@ip-address:/var/www/desired-directory</code></p>
]]>
      </content:encoded>
      <category>scp</category>
      <pubDate>Tue, 06 Feb 2018 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>How to change your Wordpress username</title>
      <link>http://localhost/posts/how-to-change-your-wordpress-username/</link>
      <guid isPermaLink="false">http://localhost/posts/how-to-change-your-wordpress-username/</guid>
      <content:encoded>
        <![CDATA[<p>Unfortunately, WordPress doesn’t allow you to edit your username(even if you are the admin).  You can’t change the username directly, but thankfully there is a work-around.</p>
<!--more-->
<ol>
<li>Create a new user and give them administrator privileges.</li>
<li>Logout of WordPress and log back in with the new user’s credentials.</li>
<li>Delete the old administrator</li>
<li>If the old administrator created any posts, WordPress will prompt you to either attribute the posts to the new user or delete the posts entirely.   If there are no posts associated with the user, WordPress will ask you to confirm deletion.</li>
<li>Now you have the admin username you want.</li>
</ol>
]]>
      </content:encoded>
      <category>wordpress</category>
      <pubDate>Thu, 04 Jan 2018 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Git aliases for efficiency or for fun</title>
      <link>http://localhost/posts/git-aliases/</link>
      <guid isPermaLink="false">http://localhost/posts/git-aliases/</guid>
      <content:encoded>
        <![CDATA[<p>Git alias is a helpful feature that reduces time spent typing commands you use over and over again.</p>
<!--more-->
<p><strong>Example</strong></p>
<p><code>$ git commit</code></p>
<p>Git commit is like save in a word document.  It is a super helpful command that records what you’ve done to the repository, and it’s best practice to do it often.</p>
<p>But you have to type git commit over and over again and what if you want to save a little time.  Here is where aliases comes in handy.  As a basic example, you can type “git ct” as opposed to “git commit” and have it function the same way.</p>
<p><code>$ git config --global alias.ct 'commit -v'</code></p>
<p>Alternatively you can also achieve the same thing by adding this to your git config file:</p>
<pre><code class="language-plaintext">[alias]
       ct = commit
</code></pre>
<p>You can also choose to have a little fun with git alias rather than focus on efficiency.</p>
<p>For example, if you want to Harry Potterify git status you can do:</p>
<p><code>$ git config --global alias.accio 'status -v'</code></p>
<p>Or if you think monkeys are the greatest, to which, I agree, you can do:</p>
<p><code>git config --global alias.monkeysrock 'status -v'</code></p>
]]>
      </content:encoded>
      <category>git</category>
      <pubDate>Mon, 03 Apr 2017 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Foreign Keys in Ruby on Rails</title>
      <link>http://localhost/posts/foreign-keys-in-rails/</link>
      <guid isPermaLink="false">http://localhost/posts/foreign-keys-in-rails/</guid>
      <content:encoded>
        <![CDATA[<p>A foreign key is an important tool in the world of databases.  What it is, is alluded to in the name: a key is how you gain access to something, while foreign means from another place.</p>
<p>To understand how foreign keys are used in Rails, we need to know a little about models, databases, and migrations too.  To better illustrate the use of foreign keys and these other concepts, we’ll board the USS Enterprise.</p>
<!--more-->
<h2 id="uss-enterprise-example" tabindex="-1"><a href="http://localhost/posts/foreign-keys-in-rails/#uss-enterprise-example" class="header-anchor">USS Enterprise example</a></h2>
<p>You are an ensign within the engineering department of the USS Enterprise.  You have a list of tasks displayed on your PADD (personal access display device) that you need to complete.</p>
<p>Your first task today is to complete maintenance of the warp drive, and it was ordered by Chief Engineer Geordi La Forge.  When Geordi first added this task it saves to a database (like an excel spreadsheet).  Your PADD retrieves information from that database in order to display the tasks Geordi added.</p>
<p>The database is organized into tables, which is like having an excel spreadsheet broken up into categories.  For example, the list of tasks you need to complete are in the tasks table.  This organization is key to speedy data access, because rather than the computer searching through mountains of data (medical records, officer profiles, mission notes, etc, etc) to find one item (a task) it can just look through the tasks table.</p>
<p>But with this separation, the tables need a way to tap into each other in some instances.  For example, the task <strong>maintenance of warp drive</strong> in the tasks table needs to be associated with an officer(you the ensign) in the officers table.</p>
<p>In one file here’s how you associate the officers table with the tasks table:</p>
<pre><code class="language-ruby">class Officer &lt; ActiveRecord::Base
    has_many :tasks
end
</code></pre>
<p>And in another file here’s how you can associate tasks with the officers table:</p>
<pre><code class="language-ruby">class Task &lt; ActiveRecord::Base
    belongs_to :officers
end
</code></pre>
<p>Breaking it Down:</p>
<pre><code class="language-ruby">class Officer &lt; ActiveRecord::Base
class Task &lt; ActiveRecord::Base
</code></pre>
<p>These lines of code create what is called a model class in Ruby.  In this example, we are creating the Officer model to chat with the officers table and the Task model to chat with the tasks table.</p>
<p>Models are like the guardians of the database.  They tell their data what it’s allowed to do, decides which data is valid for their table, who their data can chat with and in what way, and how the data can be manipulated.</p>
<pre><code class="language-ruby">has_many :tasks
belongs_to :officers
</code></pre>
<p>These lines of code describe the association(relationship) between the two tables.  It states a single officer can have many tasks, and a task belongs to a single officer.</p>
<p>Now that the Officer model and the Task model are associated with each other, we need attributes.  An attribute describes the type of data we are entering into the table.  For example, for the Officer table we need the attributes id, officer’s name, and their rank.  For the Task table we need id, officer’s id, and task.  The id is a way for Rails to locate the data needed, and is automatically generated when you create the model.</p>
<p>Luckily, with Ruby on Rails when you create a model (like the Officer and Task models) you automatically create migration files containing the attributes. Migrations are a less time-consuming and easier way to alter your database schema (structure of the database) than having to write SQL(database language) by hand.</p>
<p>This is how to create the Officer and Task models mentioned above within the terminal:</p>
<pre><code class="language-bash">rails generate model Officer name:text rank:text
rails generate model Task task:text officer_id:integer
</code></pre>
<p>And here is the code that’s generated in the task and officer migration files:</p>
<pre><code class="language-ruby">class CreateTasks &lt; ActiveRecord::Migration
  def change
    create_table :tasks do |t|
      t.integer :id
      t.integer :officer_id
      t.text :task    
    end
  end
end

class CreateTasks &lt; ActiveRecord::Migration
  def change
    create_table :tasks do |t|
      t.integer :id
      t.integer :officer_id
      t.text :task    
    end
  end
end

</code></pre>
]]>
      </content:encoded>
      <category>ruby</category>
      <category>rails</category>
      <category>databases</category>
      <pubDate>Sat, 01 Apr 2017 00:00:00 GMT</pubDate>
    </item>
  </channel>
</rss>