coding, ruby

Map Reduce plus filter using Ruby

Previous two articles were dedicated to JavaScript and map reduce and filtering of somewhat large data, elegance of code, etc.

Going forward, i’d like to evaluate other languages doing the same exact thing.

And i’m curious about performance too.


Here is my version of the same code i wrote but in Ruby (Ruby 2.1.1 was used). Didn’t even run in Jruby 1.7 unfortunately.


 class Time
 	def to_ms
 		(self.to_f * 1000.0).to_i

 total = 300 *10000
 data =

 total.times.each {|x|
 	data.push({name: 'it', salary: 33*x})

 data.push({name: "it", salary: 100})
 data.push({name: "acc", salary: 100})

 def self.timeMe
 	start =
 	endtime =
 	puts "Time elapsed #{endtime - start} ms"

 timeMe do
 	boom = {|j|  j[:salary] if j[:name] =='it' }.compact.reduce(:+)
 	puts "and  boom: #{boom} "



and  boom: 148499950500100
Time elapsed 1194 ms

Not bad but not as fast as javascript, probably due to array containing Hash, as opposed to one of the core types in javascript (a simple object) .

I’m not entirely sure why Jruby didn’t run, id love to learn.

Update (2014/april 18):¬†Java 1.6 with JVM parameters (needed) posted results of around 600ms , JVM 1.8 didn’t seem to work at the moment.

On another note, Ruby’s syntax is simply lovely {|j|  j[:salary] if j[:name] =='it' }.compact.reduce(:+)


Share This:

coding, ruby

Ruby interview challenge

I had a pleasure of getting an interview with an upcoming startup (i won’t disclose which one). Besides implementing fizz buzz in ruby, i was asked to write a method that would check for input to be a palindrome.

Palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or backward.

Keep in mind: using reverse is not allowed ūüôā

I wrote two versions since i wasn’t pleased with my first one.


Rspec – testing driven development

require 'spec_helper'
require 'blah'

describe "Blah" do 

	it "should match reversed order of the word " do
		palindrome("abba").should == true
		palindrome("abcba").should == true
	it "should reject if reversed order doesnt match" do 
		palindrome("abbac").should_not == true

	it "should handle empty string with passing" do 
		palindrome("").should == true

	it "should handle various cases " do
		palindrome("AbbA").should == true

	it "should handle empty spaces " do
		palindrome("   Ab  bA").should == true


Version 1

def palindrome2(word)

i = 0
last = -1
word.each_char do |c|
	if word[i] != word[last]
		return false
	last -=1

return true



Version 2

def palindrome(word)
	word = word.downcase.gsub(" ","").chars 
	word.each{|c| return false if  word.shift != word.pop  }

I have a feeling there is a better way of writing this.


Share This:


Lambda in ruby in one line

This example explains a lot

One line, anonymous and flexible

lambda {|x| puts "hey #{x} " }.call "Serge"


Results in lambda being (.called ) with parameter. Output would be > hey Serge

Nice , isn’t it :), all in one line.

Share This:

coding, javascript, ruby

Date Time mess with JavaScript is a breeze in Ruby world

Those of you whoa are seasoned in web develop know what i mean when i say date/time parsing , formatting can be ugly and time consuming. You turn to DateJS or momentJS (my currently favourite), you always (you should) look at the last time the project was updated, you make sure its not dependent on jquery or something else.  You also would hope that it works nicely within your nodeJS app.


Then there comes the time where you need a nice date time picker, there are many but some forget about time picking. I’ve used a few , moved to different, etc. Sometimes these date pickers have their own date time format , GREAT! now you should be careful when parsing on front end is okay and parsing on backend works too.

Here is why its a pleasure of having DateTime class baked right into Ruby, for parsing and formatting.

Here is a handy url

Share This:

coding, ruby

Dot Freeze – fairly easy to prevent objects from modification

Freeze and Frozen?

Too bad NodeJS doesn’t have this feature but Ruby does!

The freeze method in class Object prevents you from changing an object, effectively turning an object into a constant. After we freeze an object, an attempt to modify it results in TypeError.


str.freeze  begin


rescue => err




Share This:

architecture, coding, ruby

Simple threading/async execution in Ruby – tip on rake

Ever need to suspend a task or a small job in Ruby (rails included) ? Of course you do. I won’t get into the lengthy discussion about green-threads in Ruby and yes Ruby is single threaded, JRuby can and does use ¬†JVMs java.lang.Thread , in fact there is a very handy safe class ¬†you should use “ScheduledThreadPoolExecutor”.



I love the existence of this very very handy gem, it covers most of the situations for you to “call and forget” when it comes to¬†asynchronous¬†tasks. It is based on the concept of Actors and Watchers(Observers). In fact it reminds me of Java’s AOP but it’s not it.

This gem is super easy to use.

  1. Obviously add it to gemfile
  2. Add “include Celluloid” to your class and …
  3. call “async.<method of that class” and

Celluloid will handle the rest, it’ll figure out if it needs to use fiber, fork or java’s thread.

You can observe and control your actors or just leave it.

Here is the tip, sometimes we need to execute rake task (say ¬†scheduled task) , you would assume that celluloid will work just fine. it WON’T.

What happens is that rake will shutdown the process and cellulo”id” method will never be execute AND you won’t get an error.

How to make it work?

simple, just add “require ‘celluloid/autostart'” to your rake file and everything will be asynchronously run.


Thanks & hope it helps



Share This: