<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adrien</title>
    <description>The latest articles on DEV Community by Adrien (@agiboire).</description>
    <link>https://dev.to/agiboire</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F412966%2Fb49835c1-4ca4-4755-b778-f008cdd402a5.png</url>
      <title>DEV Community: Adrien</title>
      <link>https://dev.to/agiboire</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/agiboire"/>
    <language>en</language>
    <item>
      <title>Fixed IP for WSL instances</title>
      <dc:creator>Adrien</dc:creator>
      <pubDate>Wed, 08 Sep 2021 08:21:50 +0000</pubDate>
      <link>https://dev.to/agiboire/fixed-ip-for-wsl-instances-2ilf</link>
      <guid>https://dev.to/agiboire/fixed-ip-for-wsl-instances-2ilf</guid>
      <description>&lt;p&gt;Bored of having to check for your WSL instance IP everytime you reboot your computer? I found the solution!&lt;/p&gt;

&lt;h2&gt;
  
  
  The issue
&lt;/h2&gt;

&lt;p&gt;I recently published an article on &lt;a href="https://writtenby.adriengiboire.com/2021/09/06/find-the-ip-address-of-your-wsl-instance/" rel="noopener noreferrer"&gt;how to find the IP of a WSL instance&lt;/a&gt;. At the time, something I did not know and just figured out is that the IP is dynamic. Meaning it changes everytime you restart your computer. Maybe even when you stop/start your instance, too lazy to check. Let me know.&lt;/p&gt;

&lt;p&gt;This messes with your configuration. For example, I have a GUI app to play with my DB (for the curious, I use &lt;a href="https://dbeaver.io/" rel="noopener noreferrer"&gt;DBeaver&lt;/a&gt;). I configured the DB connections like in the image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytb7qu9n6dipcvkrosn3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytb7qu9n6dipcvkrosn3.png" alt="Untitled"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now, it works on setting it up. But then, you have to change this &lt;strong&gt;every time&lt;/strong&gt; you reboot your computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;While looking for a way to assigned fixed IP addresses to WSL instances, I figured that there is currently no way to do this.&lt;/p&gt;

&lt;p&gt;After more readings, I found out that setting the IP address in the &lt;code&gt;hosts&lt;/code&gt; file would make things easier. You configure your connections with a custom domain, say &lt;code&gt;wsl.local&lt;/code&gt;, and now, you have only one place where to update the IP address and all your apps are good to go.&lt;/p&gt;

&lt;p&gt;But that's still tedious, and someone worked on that problem. Here comes &lt;a href="https://github.com/shayne/go-wsl2-host" rel="noopener noreferrer"&gt;WSL2Host&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  WSL2Host
&lt;/h3&gt;

&lt;p&gt;WSL2Host installs itself as a service. Every time a WSL instance launches, it updates the &lt;code&gt;hosts&lt;/code&gt; file with the IP of the instance and assign it a domain based on the name of the distribution the WSL instance is running. For example, "&lt;em&gt;Ubuntu-18.04&lt;/em&gt;" becomes &lt;code&gt;ubuntu1804.wsl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that's all! It's a great piece of software! Read the installation instructions and you'll be good2go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Troubleshooting
&lt;/h3&gt;

&lt;p&gt;When I set it up, I had an issue where the service was registered but was not running at startup. I could run the service manually tho but that's not the purpose of it.&lt;/p&gt;

&lt;p&gt;After watching at the system logs, I found an explicit error message (🙏) that stated my current user I was logged in with did not have the rights to run such a service. To fix that, look for the service in &lt;em&gt;Services&lt;/em&gt;, it's called &lt;em&gt;WSL2Host&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxiz9oty6scr5u3hal85o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxiz9oty6scr5u3hal85o.png" alt="Untitled 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Access to its properties via right click, go to the &lt;em&gt;Connexion&lt;/em&gt; tab, and make sure the service log on as Local System account.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmei697wjwgwii2ss8ax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmei697wjwgwii2ss8ax.png" alt="Untitled 2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You should be good2go from there.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>development</category>
      <category>wsl</category>
    </item>
    <item>
      <title>Find the IP address of your WSL instance</title>
      <dc:creator>Adrien</dc:creator>
      <pubDate>Mon, 06 Sep 2021 14:09:37 +0000</pubDate>
      <link>https://dev.to/agiboire/find-the-ip-address-of-your-wsl-instance-4kog</link>
      <guid>https://dev.to/agiboire/find-the-ip-address-of-your-wsl-instance-4kog</guid>
      <description>&lt;p&gt;In Windows, I'm exclusively using WSL2 for development purposes. But sometimes, it's nice to have a GUI to do some works. For example, you are using a DB engine running in Ubuntu via WSL2 but you'd like to read your data in a nice way. When you find out how it works, it's simple.&lt;/p&gt;

&lt;p&gt;Well, I did the digging for you and I'll show you how it works (whatever the WSL version you're using).&lt;/p&gt;

&lt;h2&gt;
  
  
  Check your WSL instances
&lt;/h2&gt;

&lt;p&gt;For various reasons, you might have more than one WSL instances on your machine. For example, using docker will set up some depending on your uses. We need to find if this is our case. If it is, then we need to check which one is the default one and change it to the one we are interested in.&lt;/p&gt;

&lt;p&gt;Make sure to run these commands in a PowerShell terminal.&lt;/p&gt;

&lt;p&gt;List all the instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    PS C:\Users\adrien&amp;gt; wsl -l -v
      NAME                   STATE           VERSION
    * docker-desktop-data    Stopped         2
      Ubuntu                 Running         2
      docker-desktop         Stopped         2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, I have 3 instances of WSL. Since Docker is not running at the time, it shows as &lt;code&gt;Stopped&lt;/code&gt;. The &lt;code&gt;*&lt;/code&gt; shows the default instance which is not the one we are interested in. We want &lt;code&gt;Ubuntu&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If this is not your case, meaning you only have one, you can skip the rest and go to "&lt;em&gt;Find the IP address&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;Change the default instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    PS C:\Users\adrien&amp;gt; wsl --set-default "Ubuntu"
    PS C:\Users\Plouf&amp;gt; wsl -l -v
      NAME                   STATE           VERSION
    * Ubuntu                 Running         2
      docker-desktop         Stopped         2
      docker-desktop-data    Stopped         2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Smooth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Find the IP address
&lt;/h2&gt;

&lt;p&gt;Now, that's gonna be simple. Run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    PS C:\Users\adrien&amp;gt; wsl hostname -I
    172.24.72.193
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;EZ.&lt;/p&gt;

</description>
      <category>development</category>
      <category>wsl</category>
    </item>
    <item>
      <title>A basic Ruby solution to the robot journeys coding exercise</title>
      <dc:creator>Adrien</dc:creator>
      <pubDate>Fri, 27 Aug 2021 08:36:05 +0000</pubDate>
      <link>https://dev.to/agiboire/a-basic-ruby-solution-to-the-robot-journeys-coding-exercise-1dlo</link>
      <guid>https://dev.to/agiboire/a-basic-ruby-solution-to-the-robot-journeys-coding-exercise-1dlo</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://writtenby.adriengiboire.com/2019/10/28/a-basic-ruby-solution/"&gt;writtenby.adriengiboire.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A fellow developer, which is an employee of a client of mine, published this &lt;a href="https://github.com/mikehadlow/Journeys"&gt;link&lt;/a&gt; on the client's Slack. When it's not for an interview, I am always keen to give a shot at any test even though lately I've lacked discipline and spent way too much time on Netfouix.&lt;/p&gt;

&lt;p&gt;Tonight, I felt like it and since I've been working with JavaScript almost exclusively for a few months, I wanted to go back to ruby a little.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge
&lt;/h2&gt;

&lt;p&gt;You start with a file containing 0+ journeys. A journey is made of 3 lines looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 3 W
LLFFFLFLFL
2 4 S
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line is the current position of the Robot.&lt;br&gt;
The second line is the list of movements of the Robot.&lt;br&gt;
The third line is where the Robot is supposed to be at the end of its journey.&lt;/p&gt;
&lt;h3&gt;
  
  
  Position
&lt;/h3&gt;

&lt;p&gt;A position is made of the coordinates (&lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;) and the direction the Robot is facing toward (&lt;strong&gt;E&lt;/strong&gt;ast, &lt;strong&gt;S&lt;/strong&gt;outh, &lt;strong&gt;W&lt;/strong&gt;est, &lt;strong&gt;N&lt;/strong&gt;orth).&lt;/p&gt;
&lt;h3&gt;
  
  
  Moves
&lt;/h3&gt;

&lt;p&gt;A move is either the Robot moving &lt;strong&gt;F&lt;/strong&gt;orward, or turning on itself either &lt;strong&gt;L&lt;/strong&gt;eft or &lt;strong&gt;R&lt;/strong&gt;ight.&lt;/p&gt;
&lt;h2&gt;
  
  
  The implementation
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Build the data
&lt;/h3&gt;

&lt;p&gt;The first thing I went through was building the data and cleaning the inputs.&lt;/p&gt;

&lt;p&gt;Remember, you are getting the journeys from a file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;inputs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'journeys.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each_line&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;inputs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gsub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we already started cleaning the data. A catch here is the carriage &lt;code&gt;"\n"&lt;/code&gt;. If you leave it as it is, you will never reach equality because what you will see looks like &lt;code&gt;1 2 E&lt;/code&gt; but what the computer reads is actually &lt;code&gt;1 2 E\n&lt;/code&gt; so when you compared what you get from the file and your result, you will always get a &lt;code&gt;false&lt;/code&gt; return.&lt;/p&gt;

&lt;p&gt;Then, for ease of usability, we want to delete blank lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;Array#delete&lt;/code&gt; transforms the original array (sic), we don't have to assign the result.&lt;/p&gt;

&lt;h3&gt;
  
  
  The TDD-ish approach
&lt;/h3&gt;

&lt;p&gt;I decided to write the final code the way I intended it to work. At first, it would fail since nothing was implemented but then, incrementally implement the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;wallee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;wallee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Actual: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;wallee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Expected: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;wallee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;position&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing serious here. Notice just the use of &lt;code&gt;Array#each_slice&lt;/code&gt; to facilitate the job.&lt;/p&gt;

&lt;p&gt;I love functional but when writing ruby I am used to OOP and as you'll see, I did not change this habit :)&lt;/p&gt;

&lt;h3&gt;
  
  
  The Robot directions
&lt;/h3&gt;

&lt;p&gt;Here is the skeleton of what we've seen so far. This is the minimum list of functions we will have to implement to make our &lt;em&gt;test&lt;/em&gt; pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Robot&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt; &lt;span class="n"&gt;moves&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_right&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_left&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_forward&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the position is made of coordinates and a direction, we have to deconstruct the data in order to exploit it later easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;
    &lt;span class="vi"&gt;@coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:to_i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same here, we have a trick with the types. Coordinates are a tuple of integers. If we don't make the conversion, we will have issues when modifying data as you'll notice later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Robot, move!
&lt;/h2&gt;

&lt;p&gt;Next, we want to work on the basic moves.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning the moves
&lt;/h3&gt;

&lt;p&gt;The simplest are the directions. I went with a basic array of strings that contains the 4 possible directions, ordered. Then, we can simulate the rotation by calculating the new index according to the new direction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="no"&gt;DIRECTIONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'N'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'E'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'S'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'W'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_right&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@direction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
    &lt;span class="vi"&gt;@direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_left&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@direction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
    &lt;span class="vi"&gt;@direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DIRECTIONS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then comes the forward move. This is a little bit more complex. What I mean is I did not find an elegant way of doing. I'm opened to suggestions!&lt;/p&gt;

&lt;p&gt;Basically, you change by +/- 1 either &lt;code&gt;x&lt;/code&gt; or &lt;code&gt;y&lt;/code&gt; according the direction the Robot is pointing toward when moving forward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move_forward&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="vi"&gt;@direction&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'N'&lt;/span&gt;
      &lt;span class="vi"&gt;@coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'E'&lt;/span&gt;
      &lt;span class="vi"&gt;@coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'S'&lt;/span&gt;
      &lt;span class="vi"&gt;@coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'W'&lt;/span&gt;
      &lt;span class="vi"&gt;@coordinates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing difficult but a lot more verbose than what we've seen so far.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execute the moves!
&lt;/h3&gt;

&lt;p&gt;Now that our Robot knows how to move, let's actually move. No big deal coming here. A hash containing the different moves as keys, associated to the method that knows how to execute the given move.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="no"&gt;MOVES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;L&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'move_left'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'move_right'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'move_forward'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, all that's left is the method &lt;code&gt;move&lt;/code&gt; which calls the method with the help of the &lt;code&gt;MOVES&lt;/code&gt; hash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt; &lt;span class="n"&gt;moves&lt;/span&gt;
    &lt;span class="n"&gt;moves&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MOVES&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_sym&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The arrival
&lt;/h2&gt;

&lt;p&gt;Now that we know how to parse the journeys, we just have to reconstruct the calculated position to be able to compare it to the journeys data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;position&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="vi"&gt;@coordinates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@direction&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there we are.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ending word
&lt;/h2&gt;

&lt;p&gt;This was a fun test. Since it's been a while since I have been writing some ruby, I had to check the doc for simple stuff. It took about 30 minutes. Nothing crazy, I'm a normal human being. I'm not an alien :P&lt;/p&gt;

&lt;p&gt;Hope you enjoyed the journey! If you have suggestion to make it better, I am willing to learn as always!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>development</category>
      <category>exercise</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
