Plough => Ruby Journey through ruby

Ruby command line flag for debugging

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.

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.

Try running the following script in irb, pry or whatever else you use:

ruby -d -e 'if $DEBUG; puts "Debugging"; end'

and you should see something like this as output:

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

Don’t worry about the two exceptions as in debug mode exceptions are displayed even when they are rescued.

Setting Up Chrome Selenium driver for Capybara when using chrome canary on a Mac

We have been adding Rspec + Capybara to 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).

But, what if you are using Chrome Canary as your browser?

You would probably see the following error:

Selenium::WebDriver::Error::UnknownError: unknown error: cannot find Chrome binary

There are two ways to fix this issue:

### Solution 1 - Download Chrome

Whilst, searching for the solution, I ended up on ChromeDriver’s wiki. It clearly states that Chrome driver expects the Chrome binary to be in:

/Applications/Google\\ Chrome

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

Solution 2 - Pass the location of Chrome Canary’s binary to Selenium Chrome Driver via Capybara

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:

Capybara.register_driver :chrome do |app|, 
    browser: :chrome, 
    desired_capabilities: {
      "chromeOptions" => {
        "binary" => '/Applications/Google Chrome Chrome Canary' 

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

That was a couple of hours well spent. Hope it helps!

An unsual thing about kanji(Japanese)

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.

I just found it very fascinating and challenging at the same time.

RSpec verifying doubles

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.

Whilst merging pull request for TextRazor, 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 verifying doubles. This functionality makes rspec-fire 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 rspec-fire as a dependency from TextRazor. Makes it even more lightweight.

Thanks again to RSpec team!

Ruby puts command

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:

  • You can pass it multiple arguments and it will print them on the screen with a line break, for example:
irb(main):001:0> puts "First", "day"
=> nil
  • Secondly, you can pass it an array of elements, and it will print the elements with a line break, for example:
irb(main):004:0> puts ["Second", "Day"]
=> nil

Hope you can use this to replace multiple calls to puts in your code.