{"version":"https://jsonfeed.org/version/1.1","title":"megbits","home_page_url":"http://localhost/","feed_url":"http://localhost/feed.json","description":"A tiny blog with posts about programming and QA testing. All posts are written by a human.","language":"en","items":[{"id":"http://localhost/posts/monty-hall-problem-ruby/","url":"http://localhost/posts/monty-hall-problem-ruby/","title":"The Monty Hall problem in Ruby","language":"en","content_html":"<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>\n<!--more-->\n<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>\n<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>\n<p>The brain teaser: Is it better for the contestant to stay, switch, or does it not matter?</p>\n<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>\n<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>\n<pre><code class=\"language-ruby\">def green_dragon\n\n  stay_wins = 0\n  switch_wins = 0\n  show_door = nil\n  switch_door = nil\n\n  tries = 10000\n\n  tries.times do\n    initial_guess = rand(3)\n    door_items = [&quot;500 farthings&quot;, &quot;1 orc&quot;, &quot;1 orc&quot;].shuffle!\n    door_items.each_with_index do |item, door_index|\n      if door_index != initial_guess and item != &quot;500 farthings&quot;\n        show_door = door_index\n      end\n    end\n    \n    switch_door = ((0..2).to_a - [initial_guess, show_door])[0]\n\n    if door_items[initial_guess] == &quot;500 farthings&quot;\n      stay_wins += 1\n    end\n\n    if door_items[switch_door] == &quot;500 farthings&quot;   \n      switch_wins += 1\n    end\n\n  end\n\n  stay_wins = (stay_wins.to_f / tries.to_f) * 100\n  switch_wins = (switch_wins.to_f / tries.to_f) * 100\n  results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}\n \nend\n</code></pre>\n<h2 id=\"code-breakdown\" tabindex=\"-1\"><a href=\"http://localhost/posts/monty-hall-problem-ruby/#code-breakdown\" class=\"header-anchor\">Code breakdown</a></h2>\n<p><code>def green_dragon</code></p>\n<p>I define a method and give it a name - in this case <code>green_dragon</code></p>\n<p><code>stay_wins = 0</code> and <code>switch_wins = 0</code></p>\n<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>\n<pre><code class=\"language-ruby\">show_door = nil\nswitch_door = nil\n</code></pre>\n<p><code>show_door</code></p>\n<p>Represents one of the doors containing an orc that the hosts shows the contestant after their initial selection.</p>\n<p><code>switch_door</code></p>\n<p>Represents the door the hosts offers the contestant to switch to instead of staying with their initial door selection.</p>\n<p><code>= nil</code></p>\n<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>\n<p><code>tries = 10000</code></p>\n<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>\n<p><code>tries.times</code></p>\n<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>\n<p><code>initial_guess = rand(3)</code></p>\n<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>\n<p><code>door_items = [&quot;500 farthings, &quot;1 orc&quot;, &quot;1 orc&quot;].shuffle!</code></p>\n<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>\n<p><code>shuffle</code> is a method that shuffles items in an array through each iteration.</p>\n<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>\n<p><code>door_items.each_with_index do |item, door_index|</code></p>\n<p>This will iterate through each index of the door items.</p>\n<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>\n<pre><code class=\"language-ruby\"> if door_index != initial_guess and item != &quot;500 farthings&quot;\n  show_door = door_index\n end\n</code></pre>\n<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.\nThe 1st end ends the conditional statement, and the second end ends the each do loop.</p>\n<p><code>switch_door = ((0..2).to_a - [initial_guess, show_door])[0]</code></p>\n<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>\n<pre><code class=\"language-ruby\"> if door_items[initial_guess] == &quot;500 farthings&quot;\n   stay_wins += 1\n end\n if door_items[switch_door] == &quot;500 farthings&quot;   \n   switch_wins += 1\n end\n</code></pre>\n<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>\n<pre><code class=\"language-ruby\">  stay_wins = (stay_wins.to_f / tries.to_f) * 100\n  switch_wins = (switch_wins.to_f / tries.to_f) * 100\n  results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}\n</code></pre>\n<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>\n<p>To calculate the variables, they need to be in float form .to_f</p>\n<p><code>results = {&quot;Stay Wins&quot; =&gt; stay_wins, &quot;Switch Wins&quot; =&gt; switch_wins}</code></p>\n<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>\n<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>\n<h2 id=\"the-conclusion\" tabindex=\"-1\"><a href=\"http://localhost/posts/monty-hall-problem-ruby/#the-conclusion\" class=\"header-anchor\">The conclusion</a></h2>\n<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>\n","tags":["ruby","probability"],"date_published":"2024-03-24T00:00:00.000Z"},{"id":"http://localhost/posts/generate-zip-command/","url":"http://localhost/posts/generate-zip-command/","title":"Generate a zip from the terminal","language":"en","content_html":"<p>Here's a quick command for generating a zip from the terminal.</p>\n<!--more-->\n<p>Run in the directory you'd like to zip:</p>\n<pre><code>zip -r zip.zip ./ -x *.git* -x *.zip*\n</code></pre>\n<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>\n<p>You can also view the man page in your terminal with the command:</p>\n<p><code>man zip</code></p>\n","tags":["terminal","zip"],"date_published":"2023-05-03T00:00:00.000Z"},{"id":"http://localhost/posts/replace-web-page-with-only-text-that-interests-you/","url":"http://localhost/posts/replace-web-page-with-only-text-that-interests-you/","title":"Replace a webpage with only the text that interests you","language":"en","content_html":"<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>\n<p>One solution for this is to use browser developer tools and a little javascript.</p>\n<!--more-->\n<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>\n<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>\n<p>You can open your browser's developer tools and run the following javascript in the console.</p>\n<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>\n<pre><code class=\"language-javascript\">// Get all the elements containing the names of the integrations and stores them in a variable.\nlet title = document.documentElement.getElementsByClassName('card-title');\n\n// Create an empty array called 'titlesArray' to store the text of each 'card-title' element\nvar titlesArray = [];\n\n// Loop through each 'card-title' element\nfor (const element of title) {\n  // Add the text of the current 'card-title' element to the 'titlesArray' array\n  titlesArray.push(element.innerText)\n}\n\n// Sort the 'titlesArray' array alphabetically\nlet titlesArraySort = titlesArray.sort();\n\n// Create a new ordered list element using the createElement method of the Document Object Model (DOM)\nlet list = document.createElement('ol');\n\n// Loop through each element in the 'titlesArraySort' array\nfor (let i=0; i&lt;titlesArraySort.length; i++){ \n  // Create a new list item element using the createElement method of the DOM\n  let item = document.createElement('li'); \n  // Set the text of the list item to the current element in the 'titlesArraySort' array\n  item.innerText = titlesArraySort[i]; \n  // Append the new list item to the ordered list element created earlier\n  list.appendChild(item); \n} \n\n// Define a function called 'removeAllChildNodes' that will remove all child nodes from a given parent element\nfunction removeAllChildNodes(parent) { \n  while (parent.firstChild) { \n    parent.removeChild(parent.firstChild); \n  } \n} \n\n// Get a reference to the &lt;body&gt; element using the querySelector method of the DOM\nlet bodyElement = document.querySelector('body'); \n\n// Remove all child nodes from the &lt;body&gt; element using the removeAllChildNodes function\nremoveAllChildNodes(bodyElement);\n\n// Append the new ordered list element to the &lt;body&gt; element using the appendChild method of the DOM\nbodyElement.appendChild(list);\n\n// Add margin and padding for better readability\n\nlistElement.style.marginLeft = &quot;50px&quot;\nlist.style.padding = &quot;50px&quot;\n\n</code></pre>\n<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>\n<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>\n<p><img src=\"https://cdn.pagepixels.com/4e8ace79-fdd2-4cfa-b1e4-38613e7efbf8/9739e85c5103ad-1682306986977.jpg\" alt=\"airbyte's connectors page before adding javascript\"></p>\n<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>\n<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>\n","tags":["javascript"],"date_published":"2022-03-04T00:00:00.000Z"},{"id":"http://localhost/posts/local-to-remote-server-changes-via-scp/","url":"http://localhost/posts/local-to-remote-server-changes-via-scp/","title":"Local to remote server changes via scp","language":"en","content_html":"<p>Just some scp commands to remember for later.</p>\n<!--more-->\n<p>To push changes from a local file to a remote server, use scp in the local terminal:</p>\n<p><code>$scp index.html username@ip-address:/var/www/desired-directory</code></p>\n<p>To push changes from the entire local directory use scp -r ./ in the local terminal:</p>\n<p><code>scp -r ./ username@ip-address:/var/www/desired-directory</code></p>\n","tags":["scp"],"date_published":"2018-02-06T00:00:00.000Z"},{"id":"http://localhost/posts/how-to-change-your-wordpress-username/","url":"http://localhost/posts/how-to-change-your-wordpress-username/","title":"How to change your Wordpress username","language":"en","content_html":"<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>\n<!--more-->\n<ol>\n<li>Create a new user and give them administrator privileges.</li>\n<li>Logout of WordPress and log back in with the new user’s credentials.</li>\n<li>Delete the old administrator</li>\n<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>\n<li>Now you have the admin username you want.</li>\n</ol>\n","tags":["wordpress"],"date_published":"2018-01-04T00:00:00.000Z"},{"id":"http://localhost/posts/git-aliases/","url":"http://localhost/posts/git-aliases/","title":"Git aliases for efficiency or for fun","language":"en","content_html":"<p>Git alias is a helpful feature that reduces time spent typing commands you use over and over again.</p>\n<!--more-->\n<p><strong>Example</strong></p>\n<p><code>$ git commit</code></p>\n<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>\n<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>\n<p><code>$ git config --global alias.ct 'commit -v'</code></p>\n<p>Alternatively you can also achieve the same thing by adding this to your git config file:</p>\n<pre><code class=\"language-plaintext\">[alias]\n       ct = commit\n</code></pre>\n<p>You can also choose to have a little fun with git alias rather than focus on efficiency.</p>\n<p>For example, if you want to Harry Potterify git status you can do:</p>\n<p><code>$ git config --global alias.accio 'status -v'</code></p>\n<p>Or if you think monkeys are the greatest, to which, I agree, you can do:</p>\n<p><code>git config --global alias.monkeysrock 'status -v'</code></p>\n","tags":["git"],"date_published":"2017-04-03T00:00:00.000Z"},{"id":"http://localhost/posts/foreign-keys-in-rails/","url":"http://localhost/posts/foreign-keys-in-rails/","title":"Foreign Keys in Ruby on Rails","language":"en","content_html":"<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>\n<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>\n<!--more-->\n<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>\n<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>\n<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>\n<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>\n<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>\n<p>In one file here’s how you associate the officers table with the tasks table:</p>\n<pre><code class=\"language-ruby\">class Officer &lt; ActiveRecord::Base\n    has_many :tasks\nend\n</code></pre>\n<p>And in another file here’s how you can associate tasks with the officers table:</p>\n<pre><code class=\"language-ruby\">class Task &lt; ActiveRecord::Base\n    belongs_to :officers\nend\n</code></pre>\n<p>Breaking it Down:</p>\n<pre><code class=\"language-ruby\">class Officer &lt; ActiveRecord::Base\nclass Task &lt; ActiveRecord::Base\n</code></pre>\n<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>\n<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>\n<pre><code class=\"language-ruby\">has_many :tasks\nbelongs_to :officers\n</code></pre>\n<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>\n<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>\n<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>\n<p>This is how to create the Officer and Task models mentioned above within the terminal:</p>\n<pre><code class=\"language-bash\">rails generate model Officer name:text rank:text\nrails generate model Task task:text officer_id:integer\n</code></pre>\n<p>And here is the code that’s generated in the task and officer migration files:</p>\n<pre><code class=\"language-ruby\">class CreateTasks &lt; ActiveRecord::Migration\n  def change\n    create_table :tasks do |t|\n      t.integer :id\n      t.integer :officer_id\n      t.text :task    \n    end\n  end\nend\n\nclass CreateTasks &lt; ActiveRecord::Migration\n  def change\n    create_table :tasks do |t|\n      t.integer :id\n      t.integer :officer_id\n      t.text :task    \n    end\n  end\nend\n\n</code></pre>\n","tags":["ruby","rails","databases"],"date_published":"2017-04-01T00:00:00.000Z"}]}