Plough => Ruby

Journey through ruby

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"
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"]
Second
Day
=> nil

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

You Want to Be a Programmer?

But, you have no time

No one has time!

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.

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.

You need this

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:

  • 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.

  • Committed - It’s not easy to become a programmer. It will require a lot of commitment, practice, and more programming, of course.

  • 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.

All in all, it’s not going to be easy.

Now, do you still want to be a programmer?

Abbreviation in Ruby

Ruby’s standard library is filled with several unique, non-standard classes/modules, one such module is Abbrev.

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

1
2
3
4
require 'abbrev'
require 'pp'

pp Abbrev.abbrev(['ruby', 'rules'])

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

1
2
3
4
5
{"ruby"=>"ruby",
 "rub"=>"ruby",
 "rules"=>"rules",
 "rule"=>"rules",
 "rul"=>"rules"}

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

1
2
3
4
require 'abbrev'
require 'pp'

pp ['ruby', 'rules'].abbrev

I found a couple of use cases of the Abbrev module on Google:

  1. For creating unique labels for a bar graph.

  2. For creating an auto-completer on console, intriguing, right?

Hope this will make you aware of such a nifty module and please share your use-cases with the rest of us.

Cryptic Global Variables in Ruby

Do you know what $! means in Ruby?

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 isn’t very effective given the nature of the query.

Anyways, while looking through Ruby’s standard library, I found the file English.rb. This library has English names for all the cryptic global variables. For example: $ERROR_INFO represents $!.

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.

Nested Exceptions in Ruby 2.1.0

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 gems 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Car
  def self.start
    begin
      1/0
    rescue => ex
      puts "Exception: #{ex}"
      raise StandardError.new "Can't start the car"
    end
   end
end

begin
  Car.start
rescue => ex
  puts "Cause: #{ex.cause}"
  puts "Exception: #{ex}"
end

This will produce the following output:

1
2
3
Exception: divided by 0
Cause: divided by 0
Exception: Can't start the car

You can play around with the code yourself. It’s not as sophisticated as the gems out there, but it’s getting there.

Aspect Oriented Programming

I was looking into Ruby’s TracePoint 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 tracepoint.

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

Wikipedia defines it as “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.”

There are couple of things worth noting, Modularity and Cross-cutting concerns.

Modularity

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.

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.

Cross-cutting concerns

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.

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 Aspector. It provides a lot of examples as well just in case you are stuck.

Why not just use Ruby Modules?

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:

TestAspect.apply A

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.

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.

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.

Mina, the Deployer

I recently decided to setup a dedicated website for my company as opposed to just using my personal one. It’s a static site, with barely any content right now.

Now, I usually rely on capistrano for deployment, but since capistrano had recently undergone a major facelift, I sensed it as an opportunity to try Mina. I have known about the gem since it’s very beginning, but the pressures of modern web developer life means less time for playing around, and more for getting things done. In other words, don’t change the deployer, if it ain’t broken.

Mina is very similar to Capistrano, except one small yet pivotal fact. Instead of running commands one-by-one over SSH, it creates a bash script from your configuration and executes it remotely as one SSH command. Sounds astute to me. It has the same configuration and deploy setup as Capistrano, and anyone with some Capistrano experience will pick it up pretty quickly.

One thing that really stuck out for me and propelled me to write this post is it’s reliance on the existing SSH set-up on your machine. For example, in Capistrano, one has to explicitly set up the ssh forward agent in the deploy script itself. But, in Mina, one just needs to specify forward agent in their SSH config, and Mina will pick that up. It’s quite logical, no idea why it wasn’t the case for Capistrano.

Anyways, hope this will motivate you guys to try Mina as well.

DTrace Probes in Ruby 2 and a Startling Discovery

DTrace is a static and dynamic instrumentation framework. I did a talk at LRUG back in October on DTrace and it’s addition in Ruby 2. There’s not much content in the presentation, but it will give you a little pointers to the present situation of DTrace in general, and in Ruby.

The more I look into DTrace, the more I find it intriguing, challenging, surprising, and yet thoroughly enjoyable. Recently, I was refactoring some of the tests for a project, and the test suite takes forever to run, therefore, I decided to use Object.new for creating an instance of the interested object (stub the requests). Another colleague came along and commented…”Oh, why are you not using OpenStruct”. OpenStruct is handy, as it has a hash like structre, which means a method call that hasn’t been stubbed or instantiated will just return nil, just like a hash call when the key is absent from the hash.

I ran some quick benchmarks and here are the results:

Calculating -------------------------------------
         Struct.new       7341 i/100ms
         Object.new      57600 i/100ms
     OpenStruct.new       5462 i/100ms
-------------------------------------------------
         Struct.new     90353.5 (±10.0%) i/s -     447801 in   5.017170s
         Object.new   2335283.3 (±3.2%) i/s -   11692800 in   5.012149s
     OpenStruct.new     65014.7 (±4.9%) i/s -     327720 in   5.053209s

One thing is obvious, if you don’t need the dynamic nature of an OpenStruct, just use Object.new. This led me to think, why Object.new is superfast as compared to Struct or OpenStruct. I wanted to see what method calls is Struct or OpenStruct making, you know, just for my own knowledge. I decided to use DTrace for that purpose. DTrace has been added to Ruby 2, but I still couldn’t find any documentation. However, probes.d file gives a details of all the probe names, arguments in the probe and so on. Please look at that file if you’d like to use DTrace with ruby.

I created a simple ruby file called test.rb, like this:

Object.new

and ran the following dtrace command on a Mac OS X:

sudo dtrace -c 'ruby test.rb' -Zn 'ruby*::method-entry { @[copyinstr(arg0)] = count(); }'

to receive the following output:

dtrace: description 'ruby*::method-entry ' matched 2 probes
dtrace: pid 14009 has exited

  Gem                                                              12
  Gem::Requirement                                                 23
  Gem::Version                                                     36
  Gem::Specification                                              267
  RbConfig                                                        309

Here column 1 is the Class name and the column 2 is the number of times that class was accessed while running this ruby file. This was quite surprising that RbConfig was accessed 309 times. Why? After much thinking, I thought may be RVM has got a hand in this. I haven’t found a solution yet.

I leave you here with this startling discovery and hope to solve it for my next post. I’m planning to run the same dtrace command with ruby installed via rbenv and see what I get and then go from there.