Neat Ruby Guide
Saturday, October 18th, 2008A pretty basic but interesting ruby guide. There are some surprises too. I didn’t know irb has such a cool tab-completion feature. Neat.
Curious About Everything
A pretty basic but interesting ruby guide. There are some surprises too. I didn’t know irb has such a cool tab-completion feature. Neat.
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
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
Ramaze-means Ruby Amaze , light and modular open-source web application framework written in Ruby.Which is more easy to work with and no complexities for a new user.Ramaze is claiming to have more advantages than ruby on rails in speed , safety , user friendliness etc.
Installation is very simple
$ gem install ramaze installing as rubygem
Now your ramaze is ready to use.You can build ur applications by saying
$ ramaze --create my_app
Which will give u the basic format and you can edit and build your application in the way you want.Ramaze follows MVC architecture , unlike Rails it can work on a single ruby program also.You can run your ramaze application like
$ramaze or
$ruby start.rb
Or if u want to run a single script just run
$ ruby yourscript.rb
Ramaze is well documented with basic examples and easy to use even for a newbie.Ramaze stays close to the ruby’s principles such as simplicity n elegance.Basically Ramaze contains a controller called MainController and which maps to ‘/’. Here data is getting saved in a ‘.yaml’ file instead of database.
class MainController < Ramaze::Controller
end
Also we can map to other URL s if we want.Its pretty simpler than Rails i guess.
class AnotherController < Ramaze::Controller
map '/another'
end
You will get the basic examples to run your application from here.
Ramaze supports a lot of templating engines and such as Haml, Erubis,Markaby an d Ezmar.Also includes support for several adapters, including Mongrel, Evented Mongrel and FastCGIetc.
In the homepage of Ramaze you can see the tutorials ,just give a try you will understand more.