<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Plough => Ruby]]></title>
  <link href="/atom.xml" rel="self"/>
  <link href="/"/>
  <updated>2021-07-03T05:31:04+00:00</updated>
  <id>/</id>
  <author>
    <name><![CDATA[{"name"=>"andhapp", "url"=>"https://twitter.com/andhapp", "email"=>"dutta.anuj@gmail.com"}]]></name>
    <email><![CDATA[dutta.anuj@gmail.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Yarn Install On Mojave]]></title>
    <link href="/2019/09/27/yarn-install-on-mojave/"/>
    <updated>2019-09-27T00:00:00+00:00</updated>
    <id>/2019/09/27/yarn-install-on-mojave</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Yarn install on Mojave
date_string: 27 September 2019
—</p>

<p>If you’re having issues in <code class="language-plaintext highlighter-rouge">yarn install</code> on Mojave, then it’s probably down to the fact
that Mojave doesn’t support different i386 architecture and anything that involves native
building, gems or node modules, seems to fail. The solution is to clean the slate and start
fresh. That’s exactly what I had to do to fix the yarn installation errors:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rm yarn.lock
yarn cache clean
yarn install
</code></pre></div></div>

<p>These commands worked like a charm!</p>

<p>Hope it helps!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing Ffi 1.11.1 On Mojave]]></title>
    <link href="/2019/09/27/installing-ffi-1.11.1-on-mojave/"/>
    <updated>2019-09-27T00:00:00+00:00</updated>
    <id>/2019/09/27/installing-ffi-1.11.1-on-mojave</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Installing ffi 1.11.1 on Mojave
date_string: 27 September 2019
—</p>

<p>If you’ve installed to Mojave and are frustrated by the errors you get on installing ffi (1.11.1), then this blog 
post is for you.</p>

<p>I had the same issue last week and after trawling through the internet and trying every possible suggestion, one finally
worked!</p>

<p>But, before that, a little discussion on the actual error is in order. Below is a small snippet of the actual error on <code class="language-plaintext highlighter-rouge">bundle install</code>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Configuring libffi for i386
configure: error: in `/Users/andhapp/Projects/github/startup/pimful/vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c/libffi-i386':
configure: error: C compiler cannot create executables
See `config.log' for more details
make[1]: *** No targets specified and no makefile found.  Stop.
make: *** ["/Users/andhapp/Projects/github/startup/pimful/vendor/ruby/2.6.0/gems/ffi-1.11.1/ext/ffi_c"/libffi-i386/.libs/libffi_convenience.a] Error 2
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">libffi</code> is trying to configure for i386 architecture. Mojave doesn’t support that architecture, so the actual linking file is missing. A peek into the 
<code class="language-plaintext highlighter-rouge">config.log</code> reveals that fact:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture i386 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
ld: dynamic main executables must link with libSystem.dylib for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre></div></div>

<p>There are several suggestion to actually link the file present in Mojave and make the missing file available that way, but that doesn’t work either.</p>

<p>After several frustrating hours, the following suggestion worked perfectly:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>brew reinstall libffi

export LDFLAGS="-L/usr/local/opt/libffi/lib"
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
</code></pre></div></div>

<p>Reinstallation of <code class="language-plaintext highlighter-rouge">libffi</code> installs it correctly for Mojave and then <code class="language-plaintext highlighter-rouge">bundle install</code> works like a charm.</p>

<p>Hope it helps!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dont Blindly Follow Blog Posts]]></title>
    <link href="/2016/10/06/dont-blindly-follow-blog-posts/"/>
    <updated>2016-10-06T00:00:00+00:00</updated>
    <id>/2016/10/06/dont-blindly-follow-blog-posts</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Don’t blindly follow blog posts
date_string: 06 October 2016
—</p>

<p>Technlogy changes fast!</p>

<p>And it’s a blatant fact.</p>

<p>And with changing technology opinions, benchmarks and blog posts become obsolete, fast.</p>

<p>I was reading a blog post recently about adding some <a href="http://marianposaceanu.com/articles/improve-rails-performance-by-adding-a-few-gems">performance related gems</a>
to improve rails’s performace. It’s a very nice, detailed article with benchmarks.</p>

<p>Instead of just blindly adding the suggested gems to the project, I ran the scripts again and found that:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">escape_utils</code> gem makes things worse on <code class="language-plaintext highlighter-rouge">ruby 2.3.1</code> and <code class="language-plaintext highlighter-rouge">rails 5</code>, so no point in adding it.</li>
  <li><code class="language-plaintext highlighter-rouge">fast_blank</code> gem, on the other hand, makes things slightly better, so definitely worth adding. With
slightly better I mean you are able to make more requests/sec.</li>
  <li><code class="language-plaintext highlighter-rouge">je</code> and <code class="language-plaintext highlighter-rouge">jemalloc</code> gem didn’t install for <code class="language-plaintext highlighter-rouge">ruby 2.3.1</code> at all.</li>
</ul>

<p>Well, the point of this short blog post was to make you aware that blog posts go out of date and sometimes
it’s necessary to re-evaluate the findings and ensure you are adding the gems for the right reasons.</p>

<p>Hope it helps!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Use Sequel For Non Rails Ruby Apps]]></title>
    <link href="/2016/09/14/Use-Sequel-for-non-rails-ruby-apps/"/>
    <updated>2016-09-14T00:00:00+00:00</updated>
    <id>/2016/09/14/Use-Sequel-for-non-rails-ruby-apps</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Use Sequel for non-rails ruby apps
date_string: 14 September 2016
—</p>

<p>I am writing a ruby script to retireve some data via HTTP call and then add it to the database.</p>

<p>Now, I decided to use ActiveRecord as an ORM and wrote a <a href="https://github.com/andhapp/activerecord_sans_rails">gem</a> for the same.</p>

<p>Nothing complicated. It’s just a Rakefile that provides popular activerecord rake tasks.</p>

<p>But, then I stumbled across blog post on <a href="https://www.toptal.com/ruby/api-with-sinatra-and-sequel-ruby-tutorial">Sequel</a> and I feel
Sequel is a better choice for an ORM for a non-rails app.</p>

<p>Why?</p>

<p>Because, Rails is opinionated.</p>

<p>And it’s nuances works nice for a Rails app.</p>

<p>Anything outside of the realms of a standard Rails app and all bets are off.</p>

<p>It is like fitting a square peg in a round hole. It’s possible but you will have to shave off the extra bits.</p>

<p>Sequel on the other hand seems like a better fit for a ruby script.</p>

<p>Will post my experience with Sequel. It has ample documentation and the project was last updated on 16th September 2016.
There are no outstanding pull requests or issues on github which implies the project is active and nicely looked after.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Improve Bundler Cli]]></title>
    <link href="/2016/08/29/Improve-bundler-cli/"/>
    <updated>2016-08-29T00:00:00+00:00</updated>
    <id>/2016/08/29/Improve-bundler-cli</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Improve Bundler CLI
date_string: 29 August 2016
—</p>

<p>Learn another programming language.</p>

<p>Learn from another programming ecosystem.</p>

<p>Learn from another programming language’s tools and bring it back to the language you love.</p>

<p>Fact is, I love Ruby!</p>

<p>However, I’ve been spending a lot of time with Node.js lately. NPM provides a nice way to install
any new packages straight from the command line, like:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>npm install react --save
</code></pre></div></div>

<p>I like this convenience.</p>

<p>With an intention to improve Ruby’s ecosystem and tools, I would like to bring this to Ruby.</p>

<p>So, I’ve added a <a href="https://github.com/bundler/bundler/issues/4932">new issue</a> to Bundler. Let’s see what others feel about it.</p>

<p><strong>Update - 31 August 2016</strong></p>

<p>I had a couple of responses for my Bundler issue and it turns out there is indeed a way to add gems via bundler’s CLI.</p>

<p>You can use the <code class="language-plaintext highlighter-rouge">inject</code> command like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bundle inject pg 0.18.4
</code></pre></div></div>

<p>You have to specify the exact version that you want to add though.</p>

<p>Next version of Bundler is going one better and will expose <code class="language-plaintext highlighter-rouge">add</code> command that will add the gem without needing a version number.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Timing Attack Ruby Vulnerability]]></title>
    <link href="/2016/02/21/timing-attack-ruby-vulnerability/"/>
    <updated>2016-02-21T00:00:00+00:00</updated>
    <id>/2016/02/21/timing-attack-ruby-vulnerability</id>
    <content type="html"><![CDATA[
<hr />
<p>layout: post
title: Timing attack vulnerability in Rails
date_string: 21 February 2016
—</p>

<h3 id="disclaimer">Disclaimer</h3>

<p>I am not a security expert and this post is my attempt to learn a bit more about web security.</p>

<h3 id="why-now">Why now?</h3>

<p>Well, I am signed up to Ruby on Rails security mailing list to make sure vulnerabilities don’t go unnoticed and all my projects are running patched versions. Usually I just read the emails and ensure to upgrade or patch the affected versions and this system has worked very smoothly, however, I have never been happy with this approach and when <a href="https://groups.google.com/forum/#!topic/rubyonrails-security/ANv0HDHEC3k">a timing attack vulnerability</a>was announced on 25th January 2016 (CVE-2015-7576), I decided to research and learn more about it.</p>

<h3 id="what-is-a-timing-attack">What is a timing attack?</h3>

<p>A timing attack allows an adversary to learn more about the system under attack by statistically analysing information based on different response times. Here are a few examples:</p>

<ol>
  <li>
    <p>The size of response for a signed in user and a signed out user will be significantlly different, thus allowing the adversary to determine user’s current state.</p>
  </li>
  <li>
    <p>A string comparison with no matching characters will be slightly faster than one with 2 matching characters.</p>
  </li>
</ol>

<p>There are many more examples along the same lines. One thing worth mentioning is that it takes a lot more analysis and patience for a successful timing attack, but it’s not impossible.</p>

<h3 id="how-can-we-fix-the-string-comparision-to-run-in-constant-time">How can we fix the string comparision to run in constant time?</h3>

<p>Although, there are several ways to use the timing attack but in the interest of this article the scope is limited to secure string comparison in Ruby. There are two ways to do it:</p>

<h4 id="activesupport">Activesupport</h4>

<p>Activesupport provides a <code class="language-plaintext highlighter-rouge">secure_compare</code> method that can be used like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ActiveSupport::SecurityUtils.secure_compare(a, b)
</code></pre></div></div>

<h4 id="dont-use-activesupport">Don’t use Activesupport</h4>

<p>I took this implementation from <a href="https://goo.gl/nMFEqk">Time Trial - Racing Towards Practical Remote Timing Attacks</a> paper and looks similar to activesupport’s implementation.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
def secure_compare(a, b)
	return false if a.empty? || b.empty? || a.bytesize != b.bytesize l = a.unpack "C#{a.bytesize}"
	res = 0
	b.each_byte { |byte| res |= byte ^ l.shift } 
	res == 0
end


</code></pre></div></div>

<h4 id="why-no-secure-compare-in-ruby">Why no secure compare in Ruby?</h4>

<p>You may ask why does Ruby not have a secure compare as part of the core API? I have no idea, but, there has been movement on that front lately and you can follow that discussion in <a href="https://bugs.ruby-lang.org/issues/10098#change-56910">ruby’s bug discussion area</a>. It’s currently waiting for Matz approval.</p>

<h3 id="research">Research</h3>

<p>I don’t want to repeat the research that has been conducted by others, so here’s a review of things I researched and if you’re interested you should read these to gain more understanding.</p>

<h4 id="tools">Tools</h4>

<p>Whilst researching I came across the following tools to test for a timing attack. I’ve not tried them myself but give them a go:</p>

<ul>
  <li>https://github.com/dmayer/time_trial</li>
  <li>https://github.com/ecbftw/nanown</li>
  <li>https://github.com/aj-code/TimingIntrusionTool5000</li>
</ul>

<h4 id="papersarticles">Papers/Articles</h4>

<ul>
  <li>
    <p><a href="https://codeseekah.com/2012/04/29/timing-attacks-in-web-applications/">Timing Attacks in Web Applications</a> - 
Gives a very basic introduction to timing attack vulnerability.</p>
  </li>
  <li>
    <p><a href="https://codahale.com/a-lesson-in-timing-attacks/">A Lesson In Timing Attacks (or, Don’t use MessageDigest.isEquals)</a> - A very detailed explanation with examples of the actual vulnerability with solutions to fix the issue.</p>
  </li>
  <li>
    <p><a href="http://www.security-assessment.com/files/documents/presentations/TimingAttackPresentation2012.pdf">Login timing attacks for mischief and mayhem</a> - This is a presentation into the timing attacks but being a persentation with no audio it’s a bit hard to know the intention of the speaker, nonetheless, full of useful information.</p>
  </li>
  <li>
    <p><a href="https://www.securitee.org/files/timing-attacks_ccs2015.pdf">The Clock is Still Ticking:
Timing Attacks in the Modern Web</a> - Probably one of the best papers I read with practical examples of attacking popular sites like Facebook and LinkedIn to gain a lot more information 
regarding site users.</p>
  </li>
  <li>
    <p><a href="http://www.cs.rice.edu/~dwallach/pub/crosby-timing2009.pdf">Opportunities and Limits of Remote
Timing Attacks</a> - This paper goes into the details of the actual statistical mesaurements required as part of the a successful timing attack. To be honest, I read the beginning
and realised the Maths was too complicated for my simple brain.</p>
  </li>
  <li>
    <p><a href="https://www.blackhat.com/docs/us-15/materials/us-15-Morgan-Web-Timing-Attacks-Made-Practical-wp.pdf">Web Timing Attacks Made Practical</a> - Again, very thought provoking yet a bit heavy on the statistical analysis which is an integral part of a timing attack.</p>
  </li>
  <li>
    <p><a href="https://goo.gl/nMFEqk">Time Trial - Racing Towards Practical Remote Timing Attacks</a> - Very detailed and full of great insights. Here’s a <a href="https://www.youtube.com/watch?v=md6NEmq1n-A">companion presentation</a> on youtube.</p>
  </li>
</ul>

<h4 id="books">Books</h4>

<ul>
  <li>The Tagled Web A Guide to Securing Modern Web Applications</li>
  <li>The Web Application Hackers Handbook</li>
</ul>

<p>This research is not comprehensive and I hope its given you enough to carry on this journey by yourself.</p>

<h3 id="update-17032016">Update (17.03.2016)</h3>

<p>Good news! Just seen update on <a href="https://bugs.ruby-lang.org/issues/10098#change-57513">the ruby issue</a> and apparently OpenSSL provides a timing safe binary comparison method called
<code class="language-plaintext highlighter-rouge">CRYPTO_memcmp</code> which is what will be used in ruby internally and exposed from it to be used by other libraries.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Webpack dev server and Rails]]></title>
    <link href="/2015/12/06/webpack-dev-server-and-rails/"/>
    <updated>2015-12-06T00:00:00+00:00</updated>
    <id>/2015/12/06/webpack-dev-server-and-rails</id>
    <content type="html"><![CDATA[<p><a href="https://webpack.github.io/docs/webpack-dev-server.html">Webpack dev server</a> is pretty awesome. In my <a href="http://ploughthroughruby.co.uk/2015/11/30/integrate-react-es6-webpack-babel-with-rails.html/">last post</a>, I set up React, ES6, Babel and Webpack and the only thing outstanding was the webpack dev server.</p>

<p>With a webpack dev server running, developers can update their code and have the code changes show up straightaway in the browser, thanks to websockets. In order to implement it for a rails application, I did the following:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>&lt;% if Rails.env.development? %&gt;
  &lt;%= javascript_include_tag 'http://localhost:9999/bundle.js', 'data-turbolinks-track' =&gt; true %&gt;
&lt;% else %&gt;
  &lt;%= javascript_include_tag 'bundle', 'data-turbolinks-track' =&gt; true %&gt;
&lt;% end %&gt;
</code></pre></div></div>

<p>For development mode, I am downloading the <code class="language-plaintext highlighter-rouge">bundle.js</code> from <code class="language-plaintext highlighter-rouge">localhost:9999</code>. That’s the port where the webpack dev server is running at. For the production mode, the <code class="language-plaintext highlighter-rouge">bundle.js</code> is being read from the `app/assets/javascripts’ directory.</p>

<p>I have the following in my <code class="language-plaintext highlighter-rouge">package.json</code> to to run the webpack dev server on port 9999:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{
  "name": "ecommerce-checklist",
  "version": "0.0.1",
  "description": "Ecommerce checklist",
  "scripts": {
    "start": "webpack-dev-server --content-base ../app/assets/javascripts --port 9999",
    "build": "./node_modules/webpack/bin/webpack.js --progress",
    "build:prod": "./node_modules/webpack/bin/webpack.js -p --progress --config webpack.production.config.js"
  },
  "dependencies": {
    "react": "^0.14.2",
    "react-dom": "^0.14.2"
  },
  "devDependencies": {
    "babel-core": "^6.0.20",
    "babel-eslint": "^4.1.5",
    "babel-loader": "^6.0.1",
    "babel-preset-es2015": "^6.0.15",
    "babel-preset-react": "^6.0.15",
    "babel-preset-stage-0": "^6.0.15",
    "eslint": "^1.8.0",
    "eslint-loader": "^1.1.1",
    "eslint-plugin-react": "^3.6.3",
    "html-webpack-plugin": "^1.6.2",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.12.2",
    "webpack-dev-server": "^1.12.1"
  }
}
</code></pre></div></div>

<p>Note the bit under <code class="language-plaintext highlighter-rouge">scripts -&gt; start</code>.</p>

<p>This completes the react and webapck setup for a rails application.</p>

<p>One thing to bear in mind, is to create a production ready bundle.js before deploying this application, but that can be part of a capistrano deployment hook.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Integrate React, ES6, Webpack and Babel with Rails]]></title>
    <link href="/2015/11/30/integrate-react-es6-webpack-babel-with-rails/"/>
    <updated>2015-11-30T00:00:00+00:00</updated>
    <id>/2015/11/30/integrate-react-es6-webpack-babel-with-rails</id>
    <content type="html"><![CDATA[<h3 id="roll-your-own">Roll your own</h3>

<p>If you have got two projects using the same functionality, then write a <strong>Rubygem</strong> to promote code reuse and modularity. Writing a gem is not a new thing. In fact, there are 6,710,940,000 gems and counting, as per the <a href="https://rubygems.org/">rubygems homepage</a>.</p>

<p>Years ago Jamis Buck said in one of his blogs that if you don’t understand the inner workings of a gem, don’t use it as a dependency. Very fair comment indeed! I’m guilty of using the gems without really knowing their inner workings. To be honest, as a modern day programmer with a constant client expectation to deliver solutions, it’s okay to take your eyes of the ball.</p>

<p>However, I decided to make amendments when it came to integrating a react app into rails. I wanted to learn and fix the issues that comes with integrating and therefore, decided to start it from scratch. If you wish to use, there are a couple of nice integrate react with rails gems out there, namely, <a href="https://github.com/shakacode/react_on_rails">react_on_rails</a> and <a href="https://github.com/reactjs/react-rails">react-rails</a>.</p>

<h3 id="what-are-we-going-to-do">What are we going to do</h3>

<p>Recently, I have crossed over to the Javascript world and been doing a lot of React, ES6 (with babel) and webpack and I wanted to use the same ideas in a Rails project.</p>

<p>There are a few things to keep in mind:</p>

<ol>
  <li>A react component will serve as the client talking to the rails as a backend.</li>
  <li>The rails will be used a backend.</li>
  <li>It’s not an isomorphic application meaning there will be no server-side rendering of react components.</li>
  <li>It should play nicely with rails asset pipeline.</li>
</ol>

<p>As part of this article, we are going to set-up a react app in both development and production-mode that will play nicely with a rails app.</p>

<h3 id="setting-up-a-rails-app">Setting up a rails app</h3>

<p>As a seasoned rails developer, I expect you guys to know how to do this.</p>

<h3 id="setting-up-a-react-app">Setting up a react app</h3>

<ul>
  <li>Create a directory called <code class="language-plaintext highlighter-rouge">client</code> at the same level as the <code class="language-plaintext highlighter-rouge">app</code>.</li>
  <li>Create a file called <code class="language-plaintext highlighter-rouge">package.json</code> in the <code class="language-plaintext highlighter-rouge">client</code> directory. Package.json is to npm what Gemfile is to Bundler. Copy the following contents into the client/package.json. Most of it is pretty self-explanatory except the scripts section. Npm allows you to create these tasks inside the scripts section runnable from the command line via <code class="language-plaintext highlighter-rouge">npm run</code> command. Under <code class="language-plaintext highlighter-rouge">scripts</code>, <code class="language-plaintext highlighter-rouge">start</code> starts a webpack dev server useful for reloading the app whilst working in development. <code class="language-plaintext highlighter-rouge">build</code> is for building the app locally and uses the default webpack config and finally, <code class="language-plaintext highlighter-rouge">build:prod</code> builds the final distributable js file using webpack’s production config.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{
  "name": "react-app",
  "version": "0.0.1",
  "description": "React App",
  "scripts": {
    "start": "webpack-dev-server --content-base dist/ --port 9999",
    "build": "./node_modules/webpack/bin/webpack.js --progress",
    "build:prod": "./node_modules/webpack/bin/webpack.js -p --progress --config webpack.production.config.js"
  },
  "dependencies": {
    "react": "^0.14.2",
    "react-dom": "^0.14.2"
  },
  "devDependencies": {
    "babel-core": "^6.0.20",
    "babel-eslint": "^4.1.5",
    "babel-loader": "^6.0.1",
    "babel-preset-es2015": "^6.0.15",
    "babel-preset-react": "^6.0.15",
    "babel-preset-stage-0": "^6.0.15",
    "eslint": "^1.8.0",
    "eslint-loader": "^1.1.1",
    "eslint-plugin-react": "^3.6.3",
    "html-webpack-plugin": "^1.6.2",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.12.2",
    "webpack-dev-server": "^1.12.1"
  }
}
</code></pre></div></div>

<ul>
  <li>Create <code class="language-plaintext highlighter-rouge">webpack.base.config.js</code> in the <code class="language-plaintext highlighter-rouge">client</code> directory with the following contents:*</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>var webpack = require('webpack');

module.exports = {
    entry: './src/js/app.js',
    output: {
        path: '../app/assets/javascripts/',
        filename: "bundle.js"
    },
    module: {
        preLoaders: [
            {
                test: /(\.js$)/,
                exclude: /node_modules/,
                loader: "eslint-loader"
            }
        ],
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                query: {
                   presets: [ "react", "es2015", "stage-0" ]
                }
            }
        ]
    },
    eslint: {
        configFile: '.eslintrc'
    },
    plugins: [
        new webpack.NoErrorsPlugin()
    ]
}

</code></pre></div></div>

<ul>
  <li>Create <code class="language-plaintext highlighter-rouge">webpack.config.js</code>  in the <code class="language-plaintext highlighter-rouge">client</code> directory with the following contents. This is the default config used by webpack.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>var webpack = require('webpack'),
    config = require('./webpack.base.config.js');

config.entry = [
    'webpack-dev-server/client?http://0.0.0.0:9999',
    config.entry
];

config.output = {
    path: config.output.path,
    filename: config.output.filename
};

module.exports = config;

</code></pre></div></div>

<ul>
  <li>Finally, create <code class="language-plaintext highlighter-rouge">webpack.production.config.js</code>  in the <code class="language-plaintext highlighter-rouge">client</code> directory with the following contents. This is mainly used for creating a production ready distributable bundle. It is invoked with a <code class="language-plaintext highlighter-rouge">-p</code> option at the command line to trigger automatic minification.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>var webpack = require('webpack'),
    config = require('./webpack.base.config.js');

config.entry = [
    config.entry
];

config.output = {
	path: config.output.path,
    filename: config.output.filename
};

module.exports = config;

</code></pre></div></div>

<ul>
  <li>
    <p>Create a directory called <code class="language-plaintext highlighter-rouge">src/js</code> in the <code class="language-plaintext highlighter-rouge">client</code> directory.</p>
  </li>
  <li>
    <p>In the <code class="language-plaintext highlighter-rouge">src/js</code> directory, create a file called <code class="language-plaintext highlighter-rouge">app.js</code>. This is the entry file for webpack. Entry file is the file webpack uses to workout all the other dependencies that it needs to bundle up in the final distributable file.</p>
  </li>
  <li>
    <p>The <code class="language-plaintext highlighter-rouge">app.js</code> file should have the following contents. This is the entry point for the app. Here it’s rendering the react app in a dom node with the id <code class="language-plaintext highlighter-rouge">react-app</code>. Make sure a dom node with the same id exists in the layout file of your rails app, otherwise, react will have no place to mount the app.</p>
  </li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import React from 'react'
import ReactDOM from 'react-dom'
import App from './components/App.react'

ReactDOM.render(
    &lt;App /&gt;,
    document.getElementById('react-app')
);
</code></pre></div></div>

<ul>
  <li>One last thing, create the following directory, src/js/components and in there create a root react component called App.react.js with the following contents. As you can see, the <code class="language-plaintext highlighter-rouge">App</code> is written in ES6 classes and takes advantage of the modular dependency provided by ES6 out of the box.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import React, { Component, PropTypes } from 'react'

class App extends Component {
    render() {
        return (
            &lt;div&gt;
                &lt;h1&gt;React App&lt;/h1&gt;
            &lt;/div&gt;
        );
    }
}

export default App

</code></pre></div></div>

<h3 id="workflow">Workflow</h3>

<ul>
  <li>Open a terminal window and navigate to the client directory that we created above.</li>
  <li>Run <code class="language-plaintext highlighter-rouge">npm run build</code>. This will create a bundle.js (development version) and to <code class="language-plaintext highlighter-rouge">app/assets/javascripts</code> directory.</li>
  <li>Make sure to refer to it from the layout file. I added it to the <code class="language-plaintext highlighter-rouge">config/initializers/assets.rb</code> as a precompiled file:</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Rails.application.config.assets.precompile += %w( bundle.js )
</code></pre></div></div>

<ul>
  <li>
    <p>Added it at the bottom of the layout file with a script tag to ensure the DOM node that react app depends upon is there before react’s mounting kicks in.</p>
  </li>
  <li>
    <p>Opened the rails root page in a browser to check the react app is being mounted correctly and <code class="language-plaintext highlighter-rouge">React App</code> (the contents of the App.react.js component) is being rendered on the page.</p>
  </li>
</ul>

<h3 id="phew-further-investigation-things">Phew, further investigation things!</h3>

<ul>
  <li>Firstly, you will see there is an error in the dev tools. The app still works fine. This means the at the time of the mount react didn’t find the DOM node which seems bizzare because it works fine. I noticed removing the line of code that brings in <code class="language-plaintext highlighter-rouge">application.js</code> in your rails layout file fixes this issue.</li>
</ul>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.
</code></pre></div></div>

<ul>
  <li>You will also notice a lot of sock.js errors in the dev tools. This is because the in the webpack development config we’ve set it up to communicate with the local webpack dev server and update any changes to the app via websocket. I haven’t quite worked out how the webpack-dev-server communication with this will work, so ignore those for now, or just build the bundle.js using webpack production config.</li>
</ul>

<p>And, that’s it!</p>

<p><strong>Update: 06 December 2015</strong></p>

<p>The following error</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.
</code></pre></div></div>

<p>is my fault. In my <code class="language-plaintext highlighter-rouge">app/assets/javascripts/application.js</code>, I had the line to include the bundle.js file and since this file is brought into the layout in the head section - when the DOM node that react app needs is not ready - it throws an error. We use <code class="language-plaintext highlighter-rouge">bundle.js</code> as a precompiled asset, so we don’t need to bring it in as part of <code class="language-plaintext highlighter-rouge">appication.js</code>. There’s an easy fix in Sprockets for it. If you would like to ignore a file in your directory you use the <code class="language-plaintext highlighter-rouge">stub</code> call. So, my <code class="language-plaintext highlighter-rouge">application.js</code> file now looks like:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= stub bundle
</code></pre></div></div>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rebooting an old gem]]></title>
    <link href="/2015/10/04/rebooting-an-old-gem/"/>
    <updated>2015-10-04T00:00:00+00:00</updated>
    <id>/2015/10/04/rebooting-an-old-gem</id>
    <content type="html"><![CDATA[<p>Novelty of writing a gem has worn off. Back in the days when Rails had
just come out, writing a gem (or a plugin) was an achievement in itself.</p>

<p>What has a gem got to do with Rails? You write ruby gems and rails is
just another gem, no?</p>

<p>Correct!</p>

<p>It has nothing to do with Rails but my Ruby adventure began with Rails
and I for one give the credit where it’s due, without a shadow of doubt.</p>

<h4 id="history">History</h4>

<p>Anyways, around 4 years ago, I created a gem called <a href="https://github.com/andhapp/is_bot">is_bot</a>. It offers some spam protection for web forms. I can’t even remember the project I used it on back then.
However, recently we’ve been marketing <a href="https://www.suggestion.io/">Suggestion.io</a> and encountered a lot of spam sign ups and decided to fix the issue.</p>

<h4 id="why-not-just-use-a-captcha">Why not just use a captcha?</h4>

<p>I hate asking prospective clients to fill in yet another field. You want
to make it easier not difficult to sign up. is_bot is different. It just
adds a invisible text field making it hidden from the actual users. Time
to get one back on the damn bots!</p>

<h4 id="why-reboot-why-not-use-a-rails-4-gem">Why reboot? Why not use a Rails 4 gem?</h4>

<p>I looked at few existing gems but none worked nicely. It was time to
reboot is_bot. Back then it was written for Rails 3 so had to upgrade it
for Rails 4 APIs which was a bit of a pain since it’s hard to find what
changed from Rails 3 to Rails 4, easily. Reading through Rails source
code and looking at some of the other gems got us through.</p>

<h4 id="technology-is-flying">Technology is flying…</h4>

<p>I also took this opportunity to change a few more things…I say a few,
but so much has changed, hasn’t it?</p>

<ul>
  <li>Replaced Jeweler with Bundler as a gem to manage my ruby gem. Just
find it easier to use.</li>
  <li>Started using semantic versioning. Makes perfect sense for open source project.</li>
  <li>Upgraded to latest RSpec.</li>
  <li>Replaced <code class="language-plaintext highlighter-rouge">.rvmrc</code> to <code class="language-plaintext highlighter-rouge">.ruby-gemset</code> and <code class="language-plaintext highlighter-rouge">.ruby-version</code>.</li>
</ul>

<p>It was a bit of a work, but in the end all the efforts paid off.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Removing a method in Ruby]]></title>
    <link href="/2015/03/01/removing-a-method-in-ruby/"/>
    <updated>2015-03-01T00:00:00+00:00</updated>
    <id>/2015/03/01/removing-a-method-in-ruby</id>
    <content type="html"><![CDATA[<p>There are several ways of adding a method to a ruby class (or instance),
for example, define_method, method_missing, or by just opening up the
class itself.</p>

<p><em>However, can we remove the methods from a Ruby class?</em></p>

<p>I encountered two ways of removing methods from a class:</p>

<ul>
  <li>
    <p><a href="http://ruby-doc.org//core-2.2.0/Module.html#method-i-remove_method">remove_method</a></p>
  </li>
  <li>
    <p><a href="http://ruby-doc.org//core-2.2.0/Module.html#method-i-undef_method">undef_method</a></p>
  </li>
</ul>

<p>There are subtle differences between the two. The former removes the
method from the current class, where as the latter, stops responding to
the methods in the current class.</p>

<p>Let’s look at an example for the same. Imagine we have a parent class,
Appliance (a.k.a Kitchen Appliance), and child class, Can opener. Here
are the definitions for the two classes:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Appliance
  def name
   puts "I'm a kitchen appliance"
  end
end

class CanOpener &lt; Appliance
  def name
    puts "I'm a can opener"
  end
end

</code></pre></div></div>

<p>Now, let’s try <code class="language-plaintext highlighter-rouge">remove_method</code> and see what happens.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>can_opener = CanOpener.new
can_opener.name

class CanOpener
  remove_method :name
end

can_opener.name
puts "Can Opener has a name? #{can_opener.respond_to?(:name)}"   
</code></pre></div></div>

<p>This will produce the following output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>I'm a can opener
I'm a kitchen appliance
Can Opener has a name? true
</code></pre></div></div>

<p>As soon as you remove a method from a class it treats the method as
undefined and goes up the ancestor chain to look for an implementation, 
which it finds in the parent class. Since, there’s an implementation 
<code class="language-plaintext highlighter-rouge">respond_to</code> returns true.</p>

<p>However, <code class="language-plaintext highlighter-rouge">undef_method</code> works slightly differently and let’s see how:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Appliance
  def name
   puts "I'm a kitchen appliance"
  end
end

class CanOpener &lt; Appliance
  def name
    puts "I'm a can opener"
  end
end

can_opener = CanOpener.new
can_opener.name

class CanOpener
  undef_method :name
end
 
puts "Can Opener has a name? #{can_opener.respond_to?(:name)}"
can_opener.name
</code></pre></div></div>

<p>This will produce the following output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>I'm a can opener
Can Opener has a name? true
/tmp/execpad-f7201e261f52/source-f7201e261f52:21:in `&lt;main&gt;': undefined method `name' for #&lt;CanOpener:0x41017dcc&gt; (NoMethodError)
</code></pre></div></div>

<p>Unlike <code class="language-plaintext highlighter-rouge">remove_method</code>, <code class="language-plaintext highlighter-rouge">undef_method</code> stops responding to the method
and doesn’t travel up the ancestor chain. It totally disregards the
method implementation in the parent class.</p>

<p>Quite interesting, however, I’ve never seen it used in any of the
production code before, so, hopefully, I will find a good use case for
it soon.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby command line flag for debugging]]></title>
    <link href="/2015/01/25/ruby-command-line-flags/"/>
    <updated>2015-01-25T00:00:00+00:00</updated>
    <id>/2015/01/25/ruby-command-line-flags</id>
    <content type="html"><![CDATA[<p>Reading open source code is almost always a good idea. Open source code
has the benefit of being reviewed by a lot of committed users and it
just shows the good patterns of programming. For instance, I once
looked at Arel, and the code demonstrates an exemplary use of visitor
pattern. There isn’t much code there, yet it manages to achieve so much.</p>

<p>Recently, I was reading some open source code recently and spotted $DEBUG
environment variable in quite a few places. At the time, I thought it
must be something the author used to simplify debugging. But, no, it’s
defined and used by ruby core. Ruby provides a debug flag (-d or –debug) 
which when used would set the $DEBUG to true.</p>

<p>Try running the following script in irb, pry or whatever else you use:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ruby -d -e 'if $DEBUG; puts "Debugging"; end'
</code></pre></div></div>

<p>and you should see something like this as output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RUBY_GC_HEAP_FREE_SLOTS=200000 (default value: 4096)
RUBY_GC_MALLOC_LIMIT=90000000 (default value: 16777216)
Exception `LoadError' at /Users/andhapp/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems.rb:1222 - cannot load such file -- rubygems/defaults/operating_system
Exception `LoadError' at /Users/andhapp/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems.rb:1231 - cannot load such file -- rubygems/defaults/ruby
Debugging
</code></pre></div></div>

<p>Don’t worry about the two exceptions as in <a href="http://stackoverflow.com/a/1851232/20301">debug mode exceptions are displayed</a> even when they are rescued.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Setting Up Chrome Selenium driver for Capybara when using chrome canary on a Mac]]></title>
    <link href="/2014/12/31/setting-up-chrome-selenium-driver-for-capybara-using-chrome-canary-on-a-mac-copy/"/>
    <updated>2014-12-31T00:00:00+00:00</updated>
    <id>/2014/12/31/setting-up-chrome-selenium-driver-for-capybara-using-chrome-canary-on-a-mac copy</id>
    <content type="html"><![CDATA[<p>We have been adding Rspec + Capybara to <a href="https://suggestion.io">Suggestion.io</a> 
to ensure we don’t create any regression bugs. There are plenty of resources 
online to set-up Capybara to use Selenium’s Chrome driver (instead of Firefox).</p>

<h3 id="but-what-if-you-are-using-chrome-canary-as-your-browser">But, what if you are using Chrome Canary as your browser?</h3>

<p>You would probably see the following error:</p>

<pre>
Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary
</pre>

<p>There are two ways to fix this issue:</p>

<p>### Solution 1 - Download Chrome</p>

<p>Whilst, searching for the solution, I ended up on <a href="https://code.google.com/p/selenium/wiki/ChromeDriver">ChromeDriver’s wiki</a>. 
It clearly states that Chrome driver expects the Chrome binary to be in:</p>

<pre>
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
</pre>

<p>That’s exactly where Chrome is installed on a Mac, so, to fix this issue
just download Chrome.</p>

<h3 id="solution-2---pass-the-location-of-chrome-canarys-binary-to-selenium-chrome-driver-via-capybara">Solution 2 - Pass the location of Chrome Canary’s binary to Selenium Chrome Driver via Capybara</h3>

<p>Downloading Chrome just for the sake of passing specs doesn’t make
sense. Googling revealed that there’s something called ‘ChromeOptions’, but, 
how the hell do I pass that option from Capybara, so that it get’s passed 
correctly to Selenium Chrome driver. After, trawling through the
capybara and selenium-webdriver source, I found a way to pass the location 
of Chrome Canary as the binary. Here’s how:</p>

<pre>
Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, 
    browser: :chrome, 
    desired_capabilities: {
      "chromeOptions" =&gt; {
        "binary" =&gt; '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary' 
      } 
    })
end
</pre>

<p>The ‘binary’ option specifies the location of Chrome Canary’s binary on
your Mac.</p>

<p>That was a couple of hours well spent. Hope it helps!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[An unsual thing about kanji(Japanese)]]></title>
    <link href="/2014/10/19/an-unusual-thing-about-kanji(Japanese)/"/>
    <updated>2014-10-19T00:00:00+00:00</updated>
    <id>/2014/10/19/an-unusual-thing-about-kanji(Japanese)</id>
    <content type="html"><![CDATA[<p>Recently, I was reading an article on natural language processing of
Japanese characters and came across something very unusual about the way
Japanese characters are written, there is no delimiters between the
words, for example, if ‘Ruby’ and ‘Blog’ are two kanji characters then
they will be written as ‘RubyBlog’ with no delimiter (space in English)
between them. It makes segmenting Japanese text a lot harder since
combination of characters could mean two entirely different things.</p>

<p>I just found it very fascinating and challenging at the same time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RSpec verifying doubles]]></title>
    <link href="/2014/09/20/rspec-verifying-doubles/"/>
    <updated>2014-09-20T00:00:00+00:00</updated>
    <id>/2014/09/20/rspec-verifying-doubles</id>
    <content type="html"><![CDATA[<p>RSpec 3 has been full of some good stuff and I have full admiration for
the people behind it. Even the upgrade process was well thought out
keeping in mind the end users. As developers, we are used to handle poor
upgrade process pretty well, but RSpec totally changed my opinion.</p>

<p>Whilst <a href="https://github.com/andhapp/textrazor/pull/4">merging pull request for TextRazor</a>, I decided to upgrade the gem
to RSpec3. I was meant to do that for sometime anyways, and the pull
request opened up the perfect opportunity for it. I stumbled upon a very
interesting new feature in RSpec3, called <a href="https://relishapp.com/rspec/rspec-mocks/v/3-0/docs/verifying-doubles/using-an-instance-double">verifying doubles</a>. This
functionality makes <a href="https://github.com/xaviershay/rspec-fire">rspec-fire</a> totally obsolete. It verifies that any
methods being stubbed would be present on the instance of the class
being stubbed and also the number of argument the method accepts. This
is pretty cool. I always used rspec-fire to make sure that my stubbed
method existed on the class. In the light of these updates, I removed
<a href="https://github.com/andhapp/textrazor/commit/48aeff496be89c7433dadfbbedd3e1d3e4375b64">rspec-fire as a dependency from TextRazor</a>. Makes it even more
lightweight.</p>

<p>Thanks again to RSpec team!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby puts command]]></title>
    <link href="/2014/06/15/ruby-puts-command/"/>
    <updated>2014-06-15T00:00:00+00:00</updated>
    <id>/2014/06/15/ruby-puts-command</id>
    <content type="html"><![CDATA[<p>Ruby’s puts command will lead the ‘most used command’ competition in the language. It’s probably the first command you run when you fire up irb, or write HelloWorld.rb. I have been using it from day 1, for debugging, printing out the progress of long running scripts and so on. I recently found couple of nifty things you can do with puts command:</p>

<ul>
  <li>You can pass it multiple arguments and it will print them on the screen with a line break, for example:</li>
</ul>

<pre>
irb(main):001:0&gt; puts "First", "day"
First
day
=&gt; nil
</pre>

<ul>
  <li>Secondly, you can pass it an array of elements, and it will print the elements with a line break, for example:</li>
</ul>

<pre>
irb(main):004:0&gt; puts ["Second", "Day"]
Second
Day
=&gt; nil
</pre>

<p>Hope you can use this to replace multiple calls to puts in your code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[You want to be a programmer?]]></title>
    <link href="/2014/03/17/you-want-to-be-a-programmer/"/>
    <updated>2014-03-17T00:00:00+00:00</updated>
    <id>/2014/03/17/you-want-to-be-a-programmer?</id>
    <content type="html"><![CDATA[<h3>But, you have no time</h3>

<p>No one has time!</p>

<p>The rise of billion dollar startups that get acquired in 6 months (or longer, perhaps) from launch gives a false impression that programming is easy, and hence shouldn’t take very long to learn. After all, you have a billion dollar idea you would like to start working on.</p>

<p>Let’s conduct a small study. Why don’t you spend an hour on StackOverflow? Why? To observe how users, without doing any research, ask the same questions over and over and over again. They just want to learn how this works, not why this works and not something else. I’m possibly guilty of the same mistakes, and I have been trying to amend them ever since.</p>

<h3>You need this</h3>

<p>As a result of these observations, and experience in teaching, I’ve come up up with a list of few qualities that can make you a better programmer:</p>

<ul>
  <li>
    <p>Learner - Learning never stops for a programmer and that’s the fun of it. You will have to spend extra hours every week to brush up your skills, gain new skills, and get better at your trade.</p>
  </li>
  <li>
    <p>Committed - It’s not easy to become a programmer. It will require a lot of commitment, practice, and more programming, of course.</p>
  </li>
  <li>
    <p>Investigative - You may end up spending days trying to fix a Javascript memory leak, or why your Rails app is running slow and leaking memory. It will require a lot of investigation, research and thinking about every aspect of the application, and it’s behaviour. You will have to read API documents, source code, and so on.</p>
  </li>
</ul>

<p>All in all, it’s not going to be easy.</p>

<p>Now, do you still want to be a programmer?</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Abbreviation in Ruby]]></title>
    <link href="/2014/01/10/abbreviation-in-ruby/"/>
    <updated>2014-01-10T00:00:00+00:00</updated>
    <id>/2014/01/10/abbreviation-in-ruby</id>
    <content type="html"><![CDATA[<p>Ruby’s standard library is filled with several unique, non-standard classes/modules, one such module is <a href="http://ruby-doc.org/stdlib-2.1.0/libdoc/abbrev/rdoc/Abbrev.html">Abbrev</a>.</p>

<p>Abbrev calculates the set of unique abbreviations for a given set of strings. The following code demonstrates it properly:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'abbrev'
require 'pp'

pp Abbrev.abbrev(['ruby', 'rules'])
</code></pre></div></div>

<p>This code produces the following output where all the keys are
abbreviated and unique, and point to their respective words.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>{"ruby"=&gt;"ruby",
 "rub"=&gt;"ruby",
 "rules"=&gt;"rules",
 "rule"=&gt;"rules",
 "rul"=&gt;"rules"}
</code></pre></div></div>

<p>This also provides an extension for an Array, so you can call ‘abbrev’
method straight on an array. The code above will then become:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'abbrev'
require 'pp'

pp ['ruby', 'rules'].abbrev
</code></pre></div></div>

<p>I found a couple of use cases of the Abbrev module on Google:</p>

<ol>
  <li>
    <p>For creating <a href="http://www.aimred.com/news/developers/2010/05/11/rediscovering_ruby_abbrev/">unique labels</a> for a bar graph.</p>
  </li>
  <li>
    <p>For creating an <a href="http://endofline.wordpress.com/2010/12/25/ruby-standard-library-abbrev/">auto-completer</a> on console, intriguing, right?</p>
  </li>
</ol>

<p>Hope this will make you aware of such a nifty module and please share your
use-cases with the rest of us.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Cryptic global variables in Ruby]]></title>
    <link href="/2014/01/07/cryptic-global-variables-in-ruby/"/>
    <updated>2014-01-07T00:00:00+00:00</updated>
    <id>/2014/01/07/cryptic-global-variables-in-ruby</id>
    <content type="html"><![CDATA[<p>Do you know what $! means in Ruby?</p>

<p>Years ago, I was discussing some issue regarding GemCutter (now that makes
it ancient in programming age), and we were talking about global
variables in Ruby, for example, $; and $/. At the time, we couldn’t
really find a place to look them up, even Google <a href="https://www.google.co.uk/search?q=%24!+Ruby&amp;oq=%24!+Ruby&amp;aqs=chrome..69i57.2487j0j7&amp;sourceid=chrome&amp;espv=210&amp;es_sm=91&amp;ie=UTF-8">isn’t very effective</a> given 
the nature of the query.</p>

<p>Anyways, while looking through Ruby’s standard library, I found the file
<a href="http://ruby-doc.org/stdlib-2.1.0/libdoc/English/rdoc/English.html">English.rb</a>. 
This library has English names for all the cryptic global variables. For
example: $ERROR_INFO represents $!.</p>

<p>If you ever have to look up the English names, which I suggest you do as
it makes code easier to read, just refer to that file.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Nested exceptions in Ruby 2.1.0]]></title>
    <link href="/2014/01/06/nested-exception-in-ruby-2.1.0/"/>
    <updated>2014-01-06T00:00:00+00:00</updated>
    <id>/2014/01/06/nested-exception-in-ruby-2.1.0</id>
    <content type="html"><![CDATA[<p>With Ruby 2.1.0, one can easily trace the original exception.
Previously, on rescuing an exception one would have no reference to the original exception
(thrown by a gem/library). There are a couple of <a href="https://github.com/skorks/nesty">gems</a> that can help you
keep track of the exceptions, but with Ruby 2.1.0 you can work with
nested exceptions without any issues. Here’s some trivial code to
achieve the same:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class Car
  def self.start
    begin
      1/0
    rescue =&gt; ex
      puts "Exception: #{ex}"
      raise StandardError.new "Can't start the car"
    end
   end
end

begin
  Car.start
rescue =&gt; ex
  puts "Cause: #{ex.cause}"
  puts "Exception: #{ex}"
end
</code></pre></div></div>

<p>This will produce the following output:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Exception: divided by 0
Cause: divided by 0
Exception: Can't start the car
</code></pre></div></div>

<p>You can <a href="https://eval.in/86466">play around</a> with the code yourself.
It’s not as sophisticated as the gems out there, but it’s getting there.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Aspect Oriented Programming]]></title>
    <link href="/2013/12/30/aspect-oriented-programming/"/>
    <updated>2013-12-30T00:00:00+00:00</updated>
    <id>/2013/12/30/aspect-oriented-programming</id>
    <content type="html"><![CDATA[<p>I was looking into Ruby’s <a href="http://www.ruby-doc.org/core-2.0.0/TracePoint.html">TracePoint</a> class recently. TracePoint is an objectified Kernel#set_trace_func method. TracePoint was 
added in Ruby2, but before that there was a gem that had same function as TracePoint
class. Surprisingly, it was also called <a href="https://github.com/rubyunworks/tracepoint">tracepoint</a>.</p>

<p>Anyways, TracePoint is not the scope of this post. This post is all about AOP,
or Aspect Oriented Programming.</p>

<p>Wikipedia defines it as <em>“aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development.”</em></p>

<p>There are couple of things worth noting, Modularity and Cross-cutting concerns.</p>

<h3 id="modularity">Modularity</h3>

<p>In English, Modularity means based on modules, easily assembled, or repaired 
and the reason it’s easily repaired is because modules are
self-contained and talk to each other via a defined interface. Interface
could be hardware pins, RAM slots, or intangible ones, defined in your
Ruby or Java class.</p>

<p>In Ruby world, modules and modularity is the go-to thing to achieve
separation of concern. You got a piece of code that is used in two
different places and has no state of its own, just create a
module to be included/extended or prepended.</p>

<h3 id="cross-cutting-concerns">Cross-cutting concerns</h3>

<p>Cross-cutting concern can be defined as any piece of code that’s more widely 
used across the application, for example, logging, security, or authentication, perhaps. 
Something, like a before_filter in Rails controllers that’s applied to a set of actions.</p>

<p>There are libraries that one could use to achieve same and even more
than before_filter functionality outside of Rails. The one that I
briefly looked at is called
<a href="https://github.com/gcao/aspector">Aspector</a>. It provides a lot of examples
as well just in case you are stuck.</p>

<h3 id="why-not-just-use-ruby-modules">Why not just use Ruby Modules?</h3>

<p>Ruby modules are similar but not exactly same as the AOP concept. One
important difference is that you can apply an aspect (aspect is the
piece of code with common functionality, like a module) to a class from
outside, without opening the class. Here’s some aspector code snippet to
elaborate the point:</p>

<pre>
TestAspect.apply A
</pre>

<p>Here A is the class, and TestAspect is the aspect. As you can see, you
can just apply it from outside. Sorry, not very clear, but I didn’t want
to tie the concept to a particular library implementation.</p>

<p>One good use case of using AOP concepts would be with something like
debugging, for example, a user performed an action and you want to check
the log for parameters that are getting passed in to methods, or what
methods are getting called when certain action is performed. But, that’s
what TracePoint does, right? Well, it definitely allows one to hook into
the events and print debugging information. With AOP, one can create
more focussed debugging. Imagine, a request going through Rails stack
will hit a lot of methods and you don’t want to enable tracing and then
having to go through a long console output.</p>

<p>These are just some of the initial thoughts I had on reading AOP and TracePoint. 
Hope this post will encourage you to investigate and learn more about these topics.</p>

]]></content>
  </entry>
  
</feed>
