Archive for April, 2008

BackgrounDRb a ruby library along with rails

Friday, April 25th, 2008

Since Rails is single threaded and can’t handle multiple requests at a time , when I need such functionality I was helpless.Then I came to know about BackgrounDRb a ruby library which will help us to make “RAILS” handle multiple requests at the same time.To make use of this library is pretty simple. Following steps will help you to do so.BackgrounDRb depends on chronic and packet gems. Thus lets get started by installing these two gems first

sudo gem install chronic packet

Now we are ready to install backgroundrb , for this we need to do

script/plugin install svn://rubyforge.org//var/svn/backgroundrb

Now backgroundrb plugin is installed on our application, now set it up using the command

rake backgroundrb:setup :this will create three files on our application

creates a config file backgroundrb.yml in config directory of your rails application.
Creates RAILS_ROOT/lib/workers directory for keeping BackgrounDRb workers in one place.
Creates RAILS_ROOT/test/bdrb_test_helper.rb as a test helper for your workers


How this helps to balance the load is very interesting. Here we have one class called “worker“. Using this worker class we can make our time-consuming tasks run in the background of application, instead of running in controller.Now we can create our worker using the command

./script/generate worker <classname> which will generate your worker class and there you can write your time taking process.

Now we can invoke the worker on request using

MiddleMan.new_worker :class => :name_of_your_worker, :args => {arguments to pass}

Before you start your web server do start backgroundrb, you can start and stop this by

./script/backgroundrb/start
./script/backgroundrb/stop


Now our app will act as multi threaded. Try an example given below

Let me first thank my colleague Mr.Unni for his code on “downloading the song from YouTube using a ruby script”, I am borrowing few parts of it. So in the sample app you can download a song from YouTube using the URL of that particular song. Here Backgroundrb plays well to download multiple songs at same time by running it on background. Run this app on clicking the link below:
Download and run me

Shoes - a ruby GUI toolkit

Friday, April 4th, 2008

Ruby is not only showing its power in Rails but also applications like “shoes” ,”shoes” is graphics and windowing toolkit. It will run on all platforms like Windows, Mac OS X and Linux , which help you to build desktop applications easily.Its creating GUI components some what similar to web applications.shoes library making our task easier by providing lots of methods to build a desktop item.Ruby Shoes is written in C and uses Ruby native extensions to allow interaction with Ruby code.
First of all you need to install ruby “shoes” ,available here download

1.Download source

2.Extract(unzip)

3.Install

Now run

$ shoes

Which will give take you to a graphical mode there you can choose your ruby file for shoes.Or run your shoes file by

$ shoes <filename.rb>

Here is examples to show the simplicity and elegance of ruby shoes.Start with a small ones

Example for a button click
Shoes.app {
button("Hello") { alert("You have clicked on hello") }
}

example for drop down box
shape = nil
Shoes.app do
shape = list_box :items => ["Square", "Oval", "Rectangle"]
button “Report” do
Shoes.p [shape.text]
end
end

You can see all the methods available for shoes “CLICK ME” or type on command line
$ shoes --manual - you can see a helper.

Ideal example to see the power of shoes is given below , in those few lines its doing a big job !!
Shoes.app :width => 500, :height => 100, :margin => 10 do
def answer(v)
@answer.replace v.inspect
end
button "Ask" do
answer ask("Enter your name")
end
button "Confirm" do
answer confirm("Like to proceed??")
end
button "Open File" do
answer ask_open_file
end
button "Save File" do
answer ask_save_file
end
button "Select Your Color" do
answer ask_color("Select yours")
end
@answer = para "Answers appear here"
end

Unni’s article gets featured in LinuxGazette

Friday, April 4th, 2008

As you might notice, my blogging has gone down a bit - and you might have also observed that there are lot more posts by Unni & Divya, my colleagues. Chalk that up to new projects, and lots of higher priority items. I’m slowly coming to realize, running an actual business is not so easy as they make it sound. But, it’s an amazing ride, for sure.

Coming to the point, I encourage our developers to take some time off at least once every week and do something they think is fun. Yes, it’s very much based on Google’s 20% time, but we try to make it a group event rather than an individual one. And Unni comes up with some pretty fun projects. One such project, which he blogged about here, was recently featured in LinuxGazette. He might be too modest to post about it here, so I’m doing it for him :) (check out the editor’s comment - pretty funny).

Good stuff guys, keep going.