Friday, 16 September 2011

Scope of block variables in Ruby 1.8 and 1.9

There is a difference in the scope of the variables in blocks w.e.f ruby 1.8 and 1.9  I have pasted en example to high light the change. It created trouble when I moved my application from 1.8 to 1.9

a = 1
b = 2
r = 0
n =0

(0..10).each do |n|
  a = n
  r = r  + n

puts "a = #{a} ; b = #{b} ; r = #{r} ; n = #{n}"

ruby 1.8.7
  a = 10 ; b = 2 ; r = 55 ; n = 10

ruby 1.9.2
  a = 10 ; b = 2 ; r = 55 ; n = 0

Wednesday, 7 September 2011

Postfix on Ubuntu, send mail using telnet.

Post fix is said to be better than send mail as I read from many sources.

"sudo aptitude install postfix" Installed postfix for me.  Give various details when prompted for.
"/etc/postfix/" Has all the config details.

#sudo postfix start -> starts post fix if not already running.

#telnet localhost 25

ehlo localhost
mail from: user@localhost
rcpt to: someone@localhost
Subject: my subject

 How are you?
. (Type the .[dot] in a new Line and press Enter )

#mailq  will show you the mails to be delivered.

Monday, 13 June 2011

Rack on Rails and Custom webserver

Rails on Rack

What would be interesting
  • Where does  Rack fit in web framework
  • How to install
  • Build our own framework
  • Use our middle ware in Rails.
Rack is an interface for Ruby webserver. It provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

Rack provides an interface between different web servers and your application, Making it very simple for your application to be compatible with any web server that supports Rack - Passenger, Litespeed, Mongrel, Thin, Ebb, Webrick etc ...

It is also possible to use multiple frameworks in a single application. Rails and Sinatra integration is a good example.

Think of Middle ware as a Rails before_filter/after_filter that are reusable across different rack supported frameworks. Ex: We can use the same 'anti_spamming' middle ware for our Rails app, Sinatra app etc ..

Rack handlers

Rack in our web frame work

  • gem install rack
  • Require 'rack' for further usage
  • "rake middle ware" gives you the list of classes/modules usable as middle ware in our application
Rack specification

A Rack application is a Ruby object(not a class) that responds to call. It takes exactly one argument, the environment, and returns an Array of exactly three values: The status, the headers and the body.

 class HelloWorld

     def call(env)
         [200, {"Content-Type" => "text/html" }, "Hello Rack !"]


sample Rack environment is pasted here

sample Rack environment

We will have another syntax to bring a Rack server up and running, Using various handlers.

require 'rubygems'
require 'rack' proc{|env| [200, {"Content-Type" => "text/html"}, "Hello Rack"]}, :Port => 9292
INFO  WEBrick::HTTPServer#start: pid=4557 port=19292

Now the server is running on 19292 port and 'http://localhost:19292/' gives "Hello Rack"

Some of the other Rack handlers are listed below,
  • Rack::Handler::CGI
  • Rack::Handler::EventedMongrel
  • Rack::Handler::FastCGI
  • Rack::Handler::LSWS
  • Rack::Handler::Mongrel
  • Rack::Handler::SCGI
  • Rack::Handler::WEBrick
  • Rack::Handler::Thin

Custom middle ware for Rails

Use an existing rails application or create a new one.
$ rackup   #will bring up the rack server. This depends on the values in file in RAILS_ROOT.

rub {|env|  [200, {"Content-Type" => ""text/html"},  "Hello Rack!"]}

config.middleware.use(new_middleware, args) # adds a new middle ware at the end of middle ware stack.

Example :
 Adding a middle ware to an existing Rails application. This filter redirects page to 'google' if the requested resource is not present with in rails app.

require 'rubygems'
require 'rack'

class MyRackMiddleWare
    def initialize(app)
        @app = app

    def call(env)
        #executes the request using Rails app
        status, headers, body =
        if status == 404
            [301, {"Location" => ''}, 'redirecting to google as the requested resource is not available']
            [status, headers, body]
Add middle ware details in the 'environment.rb'
  config.middleware.use(MyRackMiddleWare) # Make sure to require the file.

 Run the application and try for an invalid page in the URL, the page should get redirected to google.

Friday, 3 June 2011

rails - bundle exec

"It’s worth noting that typing in rake foo (or anyexecutable foo) in the presence of aGemfile.lock, and expecting it to execute in the bundler sandbox doesn’t make any sense" - Yahuda

Ex: "rake middleware" should be replaced as "bundle exec rake middleware"

Wednesday, 25 May 2011

Testing your email notifications with MailCatcher.

    Often its hard to test email notifications sent from our application. MailCatcher is a gem which helps you to check those mails sent from your application.
Install the gem as 'gem install mailcatcher' and run 'mailcatcher' on your console. This  will start two servers. One smtp server(on 1025 port), use this server to sent mails. Another server, web server, helps us to view the 'sent' email on our browser 'http://localhost:1080/'. I tested it on Chrome 5.

Tuesday, 24 May 2011

Art of Light: plugin for loading application constants in rails.

Art of Light: plugin for loading application constants in rails.

plugin for loading application constants in rails.

I have created a simple plugin which could be helpful in your rails projects. It is available at . This plugin, once installed , loads constants from a config file, while the application gets initialized (server start/restart, rails console etc ...). 

There will be situations when application specific constants are required in our application. Which we could configure as needed.

This plugin helps to load values from a config file(yml) file based on the 
environment, with out writing extra code. ie: if you have a config file as 'app_config.yml' and has values in it as title: 'Caption'.The plugin helps you to call as "AppConfig::TITLE". If your config file name is 'ror_values.yml' then the values can be accessed as RorValue::TITLE". In short ::.
I have tested against ruby 1.9 and rails 3.0

How to use:
  1. Install the plugin.
  2. This will create a folder 'app_config' inside 'config' folder of your rails application.
  3. A mock config file, named 'app_config.yml', will be created with sample data.
  4. User can edit the file with proper values.
  5. The file name can be renamed
  6. Based on the file name the namescope will be created to access the values( :: ).
  7. Restart the server if you change the file name.
  8. You can expect helpful error messages being raised on invalid access.
  9. When you un-install the files in config/app_config/* will be retained, 
  10. expecting that it would contain useful data. You can manually delete it if needed.

    I have tested it against rails 3.0.x and 2.3.8 .