Questions on Ruby? Ask An Expert at RubyLearning

by Satish Talim on November 24, 2011

Questions on Ruby? Ask An Expert at RubyLearning

RubyLearning is happy to announce the start of a series of blog posts titled “Ask An Expert”. We are assembling experts in various areas of Ruby programming who will answer your questions as a blog post here.

To begin with, some of the experts who have agreed to answer your questions are:

  • Gautam Rege on Ruby with MongoDB, Mongo, MongoMapper and Mongoid
  • Gonçalo Silva on Ruby/Rails performance
  • Sau Sheong Chang on Sinatra
  • Sethupathi Asokan on the Ruby devise gem
  • Staffan Nöteberg on Ruby regex
  • Steve Klabnik on Testing in Ruby

I am sure there are more Ruby experts to follow.

Please feel free to ask your questions on the topics mentioned above, as comments to this blog post. We will put up these questions to the experts who will answer them here as a separate blog post, provided there are at least 7-8 questions per topic. If not, they would answer your questions directly as comments.

Here’s some information about our experts:

Gautam Rege Gautam Rege is a Director and Co-founder of Josh Software Private Limited a Pune based software company that specializes in Ruby related services and product development. He is currently busy writing the book “Ruby and MongoDB Web Development Beginners Guide”.

Gonçalo Silva Gonçalo Silva, is a full-time Ruby on Rails developer at and has participated in the Ruby Summer of Code 2010. He loves and contributes to many open-source projects, being a fan of Linux, Ruby and Android. He likes to call himself a hacker, but that’s just an excuse for being in front of the computer all the time. Oh, and he tweets at @goncalossilva.

Sau Sheong Chang Sau Sheong Chang, is currently the Director of the Applied Cloud Computing Lab, in HP Labs Singapore. Prior to this he was the CTO of Garena Online, one of the largest game publishing provider in Southeast Asia and before that, the Engineering Director for Yahoo! Southeast Asia. All in all, he has more than 15 years of application development experience, mostly web application and in the past 5 years, mostly in Ruby. He published a book Ruby on Rails Web Mashups in 2008 and recently published a new book Cloning Internet applications with Ruby in August 2010. He is active in the Ruby community in Singapore, where he is currently living, being one of the pioneers in Singapore Ruby Brigade.

Sethupathi Asokan Sethupathi Asokan is a Director and Co-founder of Josh Software Private Limited a Pune based software company that specializes in Ruby related services and product development.

Staffan Nöteberg Staffan Nöteberg wrote the critically acclaimed productivity book Pomodoro Technique Illustrated. He knows the regex (regular expressions) stack all the way from the automata theory base up to the newest regex idioms. And he’s proficient in Oniguruma – the Ruby regex engine.

Steve Klabnik Steve Klabnik, is a software craftsman, writer, and former startup CTO. Steve tries to keep his Ruby consulting hours down so that he can focus on maintaining Hackety Hack and being a core member of Team Shoes, as well as writing regularly for multiple blogs.

Please support RubyLearning: Buy the Kindle version of the The Ultimate Guide to Ruby Programming eBook on Amazon.

Technorati Tags: , , , ,

Posted by Satish Talim

{ 43 comments… read them below or add one }

Rohit Kelkar November 24, 2011 at 8:07 am

Gautam, I have two questions:

1. I have a lot of problems installing some of the gems on my Windows box. Is there a solution for us “poor Windows users”?

2. MongoDB has so many Ruby drivers – mongo, mongomapper, mongoid – where to use which driver and why?


Satish Talim November 24, 2011 at 8:12 am

Rohit maybe this is what you need for your first question. Thanks to Luis Lavena.

I’ll let Gautam answer your second question in a separate blog post.


Gautam Rege November 24, 2011 at 3:40 pm

Just to add to the list there is also mongo-odm and mongomatic :)

Remember this is open-source world – so there is no particular ‘mapper’ which one can recommend strongly. MongoMapper was the first and very popular. I prefer Mongoid because of its very robust and has *excellent* documentation. Choice of mappers is only about ease of use and your comfort level.

Do read this post from Peter Cooper:


Manasi Tambe November 24, 2011 at 8:31 am

Sau, are there any other resources like Heroku where I can host my Sinatra app for free?


Konstantin Haase November 24, 2011 at 3:09 pm

Engine Yard has a free trial:
Cloud Foundry Beta is free:
DotCloud has a free option:


Satish Talim November 25, 2011 at 8:20 am

Konstantin, just bought your “Sinatra Up and Running” book. Looking forward to finding some more insights.


Sau Sheong November 25, 2011 at 8:31 am

Konstantin answered it pretty well. I hardly use anything else other than Heroku because of the convenience but the ones that Konstantin are great.


Konstantin Haase November 25, 2011 at 12:54 pm

Great! I hope you’ll like it. Really looking forward for your feedback.


Amit Tandon November 24, 2011 at 8:54 am

Steve, Unit Test or MiniTest and why? What’s the difference?

Staffan, I always have had problems using regex. What’s the best way to learn and use regex?


Gautam Rege November 24, 2011 at 4:07 pm

Having problems while learning regex is a GOOD problem to have :) The more you struggle, the more you learn.

I love – its a Ruby regex site, it has a reference sheet, it has a very neat interface to check and test your regex.

I typically use these 2 examples to learn most aspects of regex:
+ Validate an email address format
+ Validate an IP address format

You learn almost ALL the regex aspects with these 2 examples.


Steve Klabnik November 24, 2011 at 10:11 pm

I prefer MiniTest. Test::Unit is only still around because it used to be the standard framework, but it’s pretty vestigial at this point. MiniTest is an attempt to implement the same

MiniTest’s code is also incredibly small and readable. You’re also able to mix and match more advanced features too; one of my favorites is to use minitest/spec for the organization of my code, and use the assert style matchers rather than .should.

The only downside to MiniTest is mocks; I much prefer rspec’s mocks, and use them even when I’m using MiniTest to test code.


Staffan Nöteberg November 25, 2011 at 2:32 pm

You should take regex learning as seriously as learning any other programming language, e.g. Ruby. Regex is declarative (not imperative like Ruby), it’s a domain specific language (not multi-purpose like Ruby) and it has no delimiters or extra whitespace. If you’re not familiar with languages with these traits, it’s even more important that you put effort in learning regex extensively. If you really want to learn regex, you should do most of the following activities:

Read a regex book. My favorite is Regular Expressions Cookbook by Goyvaerts/Levithan
Take a full day regex course. I give a course like that in Europe (, and there’re probably similar courses in Asia and North America.
Answer at least 50 regex questions at Stackoverflow. It doesn’t matter if your answer is upvoted, you should try to solve these problems to increase your own proficiency.
Use test-driven development (TDD) when you develop regexes, in order to get immediate feedback. It’s hard to learn without feedback.

If you want to master regex, you should also read a book on automata theory. My favorite is Introduction to Automata Theory, Languages, and Computation by Hopcroft/Motwani/Ullman

I agree with Gautam that is a great tool and can be used as a test bench. However, be aware of that is (right now) based on Ruby 1.8.7. Ruby 1.9 has a new regex engine – called Oniguruma – with changed behaivour.


Susan Miller November 24, 2011 at 9:20 am

A question for Gautam. There are so many NoSQL databases available, how does MongoDB stand up compared to them?


Gautam Rege November 24, 2011 at 3:55 pm

Most NoSQL databases serve some purpose – some look at durability, consistence, scalability, speed etc. All NoSQL are BASE databases and not ACID. Basically Available, Soft state and Consistent – in-short, eventually consistent and scalable.

MongoDB is typically used for scale and data with loosely defined structure. The name itself is derived from “huMONGOous data”. Similarly Redis is for speed (in-memory database) but not suitable for large data, after all how much memory can you have? Riak is a fault tolerance distributed database. Cassandra is good combination speed and scale. Hadoop (HBase), BigTable are all used for storing vast amounts of data (and typically for map-reduce operations).

MongoDB differs from others that it saves data as BSON (Binary JSON), functions can be written in Javascript. Being able to serve web-requets via JSON, you ‘could’ actually hit the database from a web-page, get JSON results and render. (MongoDB also serve jsonp requests).


Ben Pols November 24, 2011 at 9:30 am

A Ruby example that accesses a MongoDB database created on MongoHQ and MongoLab would help. Gautam, which out of the two would you recommend?


Gautam Rege November 24, 2011 at 4:02 pm

Have a look at kipwiki – it was a hackathon project we created in the Pune Rails Meetup! Its hosted on Heroku and uses MongoLabs add-on. We used mongoid mapper for this.

Source :

Both MongoHQ and MongoLabs are good. Both are hosted on Amazon EC2. MongoHQ is older than MongoLabs but MongoLabs gives more free space! So pick anyone.


Suraj Reddy November 24, 2011 at 9:45 am

How do you make exe files with Ruby?


Anders November 24, 2011 at 4:50 pm

I guess you mean standalone exe-files for Windows environment.

You can use Ocra for this.

To install ocra open a command window and enter:

gem install ocra

To build an exe file type:

ocra yourProgramScript.rb 

Ocra has a lot of parameters that you can add when you build your exe file, look at


Sau Sheong November 25, 2011 at 8:39 am

Hi Suraj, you can try this ->

It’s rather old but the last time I used it, it was pretty solid.


Sunil Antony November 24, 2011 at 11:13 am

Gonçalo, I have a question on rails -3 performance. I was using Rails 2.3.8. Recently I migrated to Rails 3.1.0. After the migration I noticed that my custom rake task which is using some ActiveRecord queries is slower now. What is the real cause of this? Is there any work around for this?


Sethupathi Asokan November 24, 2011 at 11:32 am

This is a generic query. How did you figure out that activerecord is the bottleneck? Anyway, check out the following things:
1. Are you using correct/latest version for database adapter gem?
2. Check that you are using appropriate clause (‘where’, ‘order’)
3. Are there any indexing constraints causing the issue?
4. Check if you are using ‘includes’ if you are retrieving the associated objects.

Once you post your findings, stats and log, it would help us in analyzing the issue.


Yamini November 24, 2011 at 1:39 pm

I have one question: I want MySQL database as a backend to ROR. How do I link these two? I changed the database.yml file (for MySQL) what else do I need to change?


Sau Sheong November 25, 2011 at 8:40 am

You need to have the MySQL gem and the necessary MySQL drivers.


Chris Kimpton November 24, 2011 at 4:40 pm


Well you asked, and I am stuck on this problem at the moment… my rails app is giving me a 406 Not acceptable from my action – its for specific function type actions and then it does a redirect_to the “show” action. Its not doing the redirect call, but instead gives the 406 error. It is doing the first part of the action, pre the redirect… See, for example, the method next_ball in this controller –

Thanks for any tips/pointers – chris.


Chris Kimpton November 24, 2011 at 10:30 pm

Fixed it :) – I had an action named “status” and that seemed to confuse Rails. Renaming it fixed my problem. Would you consider that a bug in Rails?


Gautam Rege November 25, 2011 at 11:49 am

Yeah! I was trying to remember where I have seen this problem. In that case it was ‘action’ being passed a GET parameter — this overrides the default :action parameter in the route and returned a 406 error.

The status method is defined in the action_controller/metal.rb and is responsible for sending back HTTP status code. So, you sent back incorrect status when you defined status as your method in the controller and overwrote the default status method :)

This is not a bug in Rails – this is a case of RTFM (no offense meant).


niyazi ates November 24, 2011 at 5:01 pm

Do you think shoes can replace gtk-ruby, qt-ruby and tk-ruby? What are the advantages and / or disadvantages?


Steve Klabnik November 24, 2011 at 10:23 pm

It won’t ever be a _replacement_, but I do think it’ll have its place.

The biggest advantage of Shoes is that it feels like actual Ruby. The use of blocks is really great, and makes callbacks super simple. The other libraries are simple wrappers over the C code, and so feel like C libraries. Very odd.

Shoes is also way simpler. Shoes’ ‘background’ command looks like this:

background “#000″…”#FFF”

That’ll give you a background gradient from black to white. Implementing that same method in QTRuby is 14 lines:

Also, Shoes’s packager, while a bit buggy, is great for actually distributing apps. And while those all work on Windows or the Mac, it’s kinda rough getting GTK and QT to work, and they’re _massive_ dependencies. A Shoes app has about 30MB of overhead.

That said, the downsides of Shoes do exist. The biggest one is that the project is progressing really slowly; It’s me and two or three plucky other people; and we’re all part-time. So if Shoes doesn’t work for you or is missing a feature, I can’t guarantee things will be included in a reasonable time. Also, the packager is great, but has some bugs, and Shoes can get crashy sometimes.


Sau Sheong November 25, 2011 at 8:46 am

I’m using Shoes extensively in my new book to run simulations. It’s a great toolkit and it’s a damned shame it hasn’t gotten too much usage or have too many people on it so kudos to Steve for being one of the few plucky people plugging at it.

I’d say one of the biggest downsides I faced so far is that the debugging capabilities are rather poor. I can’t output to a screen (except to the console in debug, but that’s not quite the same and it’s rather dicey in animation loops). Also, I can’t switch to a different Ruby version or even a different standard library. For example I was using the Vector from the Matrix package but the Ruby 1.9.1 included in the latest Shoes version doesn’t include Vector division and I can’t switch in Ruby 1.9.2. I ended up open classing Vector implementing my own Vector division.

What I really liked about Shoes is that it’s so easy to use and to read, something most other UI toolkits not only in Ruby but in most languages suffer from. So if you’re up to some quick prototyping and simple apps it’s an excellent choice.


googya November 24, 2011 at 7:36 pm

I am a newbie to Ruby on Rails. What is the best way to master it? I was a Javaer.


Satish Talim November 25, 2011 at 8:51 am

You can check out Michael Hartl’s excellent online resource – “Ruby on Rails Tutorial“.


Pixoo November 25, 2011 at 4:21 am

With Rails, how can I allow users to upload images in app/assets/images using FTP and get them processed by The Asset Pipeline in production. I enabled on-the-file compilation but it doesn’t seem to work.


prem November 25, 2011 at 3:01 pm

I installed aptana rad rails in eclipse. While running newly created application via start server in debug mode it shows:

{C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find ruby-debug-base19 (~> 0.11.20) amongst [actionmailer-3.1.3, actionmailer-2.3.5,................. (Gem::LoadError)

and I have ruby-debug-ide19 (0.4.12 ruby-debug19 (0.11.6) in my gem list..
(its work fine while starting server in normal mode)

What are the changes I should do for running application in debug mode?

My ruby version-ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
rails -2.3.5


Peter Harris November 25, 2011 at 4:24 pm

Sinatra (version 1.3.0 and up) supports content streaming from one’s application. A simple example for a newbie would help.


Shen November 28, 2011 at 4:57 pm

I just installed RoR and MySQL on my Lion Server. I used Homebrew to intall MySQL. After that, I typed “mysql -uroot” in terminal which shows a error message “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2)”. How can get through this? Thanks a lot:)


Sethupathi Asokan November 28, 2011 at 5:18 pm

Just check the ‘mysqld’ (mysql server) is running.


Mehul Gurjar December 3, 2011 at 12:11 pm

I have one question
=> How to load a hash {“first” => “first”, “second” => “second”, “third” => “third”} into a constant Orders when Rails server started (values in the hash should be read from a configuration file for easy changing)
How to load an array [“first”, “second”, “third”] into a constant Orders when Rails server started (values in the hash should be read from a configuration file for easy changing)


Thomas W. Lynch February 13, 2014 at 8:04 pm

Should the ‘after’ method in Sinatra actually run after the ‘get’ finishes? Or does the get actually finish only after ‘after’ ? The reason I ask, is I put a time delay in ‘after’ and the time delay occurs before the html goes out tot the website. Here is the code:

class OpenStartup < Sinatra::Base
get '/' do
after do do
EM.add_periodic_timer(1) { puts "Tick …" }
EM.add_timer(5) do
get '/hello’ do

This is rendered via passenger through apache.


Victor Goff February 14, 2014 at 12:54 pm

# Define an after filter; runs after all requests within the same
# context as route handlers and may access/modify the request and
# response.

The documentation states the above. And that it happens before a gets request makes sense. The rendering will be done after the work is done in order to make that rendering.


Thomas W. Lynch February 14, 2014 at 1:15 pm

What you describe is a sort of pipe, where the output from ‘get’ may be modified by ‘after’, and the output of ‘after’ then goes to the page. Hence we see after run before the page renders.

Is this functionally identical to calling a helper function at the end of the other code in the affected ‘get’ blocks? So this saves us from calling such a helper at the end of each and every ‘get’ block when after is unqualified?


Victor Goff February 14, 2014 at 2:06 pm

A pipe? I suppose it could be described that way. More like a hook, though… such as you find with method_undefined or at_exit, it is there for when an event happens. In this case, “after”, as described in the source code comment.

From the Sinatra book: “The after method lets you pass a block to be evaluated after each and every route gets processed.”

The source code, though, is definitive, and well worth looking through.


Thomas W. Lynch February 14, 2014 at 2:40 pm

So in general an ‘after’ block is not functionally the same as adding a helper with the same code at the end of corresponding ‘get’ blocks, because zero or more ‘get’ blocks may fire before the ‘after’ block code is evaluated, and only evaluated once.

The only mildly confusing part is that ‘after ‘code runs before output is sent to rackup, (this must be so, as how else would it affect the response). Hence we see its functional side effects (such as the sleep in the example above) before we see the response in the browser. Alas, I wish there was a ‘run_after’ method, that would execute after Sinatra sent the response to the rackup API so one could run post response maintenance code.

Victor Goff February 14, 2014 at 3:31 pm

I would consider what is the order of events when you call ‘get’ in browsing the site. Where does the routing happen in relation to that get. Or rather “when”.

As far as it being the same as a helper method, no, it isn’t the same, for many reasons, but one is that the helper method runs in a different time frame, and needs to be explicitly called by the person writing the code… where a hook happens upon some event.

As far as the “I Wish”; this is how contributions are born. :)


Leave a Comment

{ 21 trackbacks }

Previous post:

Next post: