Multiple versions of ruby on ubuntu


Three-Four days ago I was in a situation where I need to have multiple versions of ruby and rubygems on my ubuntu machine. I was lucky, I found an awesome article http://blog.michaelgreenly.com/2008/08/multiple-versions-of-ruby-on-ubuntu-2.html. This really solved my problem, Many thanks to Michael Greenly.

Taskit: Another task scheduler for rails


While searching something I found and interesting plugin ‘Taskit‘, which I want to test for sure in production/staging when ever I get the chance.

Anybody tried it already?

Printing large image diagrams generated by RailRoad


I use RailRoad for generating Ruby on Rails diagrams, but always wish I could print those diagrams. Generally diagrams are too big to print on a single A4 size paper and I didn’t find any tool to print larger images in parts so that I can join them. If I print the image generated by RailRoad on single page, it of no use as it is hardly readable.

Fortunately, two days ago I found something which resolved this issue. It is a linux command and print a particular image on four pages. Here is that command:

lp -o scaling=200 models.png

Hassle free installation of rails stack on debian based system


Want to install rails stack on a machine? Just follow these steps. It will setup a rails stack(Apache + passenger + mysql + ruby + rubygems + common gems + git) on any server(debian based)

  1. apt-get update
  2. apt-get upgrade -y
  3. apt-get -y install build-essential libssl-dev libreadline5-dev zlib1g-dev
  4. apt-get -y install mysql-server libmysqlclient15-dev mysql-client
  5. apt-get -y install ruby ruby1.8-dev irb ri rdoc libopenssl-ruby1.8
  6. install rubygems manually:
    1. download rubygems form rubyforge, >=1.3
    2. unzip files
    3. ruby setup.rb
    4. Check that gem command is in path. Sometimes ‘gem1.8′ is available but ‘gem’ not. In that case copy /usr/bin/gem1.8 to /usr/bin/gem using “cp /usr/bin/gem1.8 /usr/bin/gem”
  7. apt-get -y install libmagick9-dev
  8. apt-get -y install imagemagick
  9. apt-get -y install postfix mailx
  10. apt-get -y install apache2
  11. apt-get -y install apache2-prefork-dev
  12. wget http://webonrails.com/wp-content/plugins/download-monitor/download.php?id=7
  13. ruby install_gems.txt
  14. passenger-install-apache2-module
  15. Download git from git-scm.com

    1. Unzip files
    2. ./configure –without-tcltk
    3. make -j 2
    4. make install

You are all set now, go deploy you rails app. I have tested it on linode(ubuntu8.10), slicehost(ubuntu 8.10), should work for you too

Edge rails: ActiveRecord::Base.each and ActiveRecord::Base.find_in_batches for batch processing


You guys must faced a situation when you need to process large number of records, for example sending newsletters. Then you must have done some sort of batch processing to save it eating up all of your memory. This feature is committed to rails core by DHH.

Now you can do something like User.each and User.find_in_batches.

Please refer here for more details.

Export Mercurial(Hg) repository to git repository


I have been using a git from quite a some time. But there was a project for which we were using Mercurial(Hg).

We decided to move it’s repository to github, after spending some time on google I found many articles but unfortunately none of them worked for me.

But, Finally I managed to move it from Hg to Git.

Following are the steps I followed:

  1. git clone git://repo.or.cz/fast-export.git
  2. mkdir new_git_repo
  3. cd new_git_repo
  4. git init
  5. /path/to/hg-fast-export.sh -r /path/to/hg_repo #hg-fast-export.sh in the clone of step 1
  6. git-repack -a -d -f
  7. git checkout BRANCH_NAME # BRANCH_NAME is the name of Hg branch, in my case it was ‘trunk’

It worked for me very well, hope same to you guys…

Rails Plugin: role_requirment, Clean role-based security for restful_authentication


I just found a very useful plugin role_requirement to manage roles in rails app.

RoleRequirement focuses on a simple approach to role-based authentication. RoleRequirement leverages the power of !Ruby to strike a marvelous balance between simplicity and flexibility.

Features:

* A user can have many roles or one role
* Full test helpers to make it easy to test your controllers.
* Squeaky clean implementation – don’t repeat yourself!
* Code generators: spend more time coding and less time wading through installation instructions.

rake -T does not list my custom rake task


This is just a tip, may be you guys are already aware of it.

Sometimes I wonder why my custom rake task doesn’t appear in rake -T list, however it runs well. I just noticed that if I do not specify description for a task then that particular rake task does not appear in the task list.

Ruby 1.9.1: Hash


In ruby 1.9.1 has many changes for Hash some useful changes are below:

RUBY_VERSION => 1.8.6

RUBY_VERSION => 1.9.1

>> {'name', "Akhil"}
=> syntax error, unexpected ',', expecting tASSOC

>> {name: "Akhil"}
=> {:name=>"Akhil"}

Now Hash preserves order:

RUBY_VERSION => 1.8.6

>> hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
=> {:b=>"B", :c=>"C", :d=>"D", :a=>"A"}
>> hash.to_a
=> [[:b, "B"], [:c, "C"], [:d, "D"], [:a, "A"]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> ["B", "C", "D", "A"]

RUBY_VERSION => 1.9.1

>> hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
=> {:a=>"A", :b=>"B", :c=>"C", :d=>"D"}
>> hash.to_a
=> [[:a, "A"], [:b, "B"], [:c, "C"], [:d, "D"]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> ["A", "B", "C", "D"]

Better to_s method(similar to hash.inspect).

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> "b2c3d4a1"

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> "{:a=>1, :b=>2, :c=>3, :d=>4}"

hash.each and hash.each_pair

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}

hash.select now returns hash instead of array

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>>  hash.select{|k,v| k == :c }
=> {:c=>3}

Tip: Open DB shell/console from rails root dir


Just a quick tip you might be using this already.

If you guys want to open your app’s DB shell. Then you can use rails utility ‘dbconsole’ by issuing “script/dbconsole” from rails root directory.

It will ask for DB password, and open your db shell.

If you use sake, you may like following sake task:

desc 'Launches the database shell using the values defined in config/database.yml'
task 'db:shell', :needs => [ 'environment' ] do
  config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
  command = ""
  case config["adapter"]
  when "mysql" then
    (command << "mysql ")
    (command << "--host=#{(config["host"] or "localhost")} ")
    (command << "--port=#{(config["port"] or 3306)} ")
    (command << "--user=#{(config["username"] or "root")} ")
    (command << "--password=#{(config["password"] or "")} ")
    (command << config["database"])
  when "postgresql" then
    puts("You should consider switching to MySQL or get off your butt and submit a patch")
  else
    (command << "echo Unsupported database adapter: #{config["adapter"]}")
  end
  system(command)
end

Previous Posts

Sponsors

    Sponsor’s Links

    Categories