Translate

November 21, 2012

Ruby - block, Proc, lambda and method

Here is my brief explanation after reading about ruby closures.

Ruby does great stuffs with closure, like in array iteration we are using closures in everywhere.
Ruby has 4 type of closures - Block, Procs, Lambda and Method. I'm trying to explain them in brief.

Block
It's used with '&' sign prefixed variable also executes using yield or *.call.
1
2
3
4
5
6
7
def do_something_with(&block)
  puts "I did this with block using #{block.call}"
end
 
do_something_with {"ruby"}

#=> I did this with block using ruby

Procs (Procedures)
Its used as other ruby data types (array, hash, string, fixnum etc..), it's reusable and could be used across multiple calls. also several procedures could be passed at a time.
1
2
3
4
5
6
7
def do_something_with(block)
  puts "I did this with Proc using #{block.call}"
end
 
do_something_with Proc.new { "ruby" }
 
#=> I did this with Proc using ruby

Lambda's
It's a procedure (Proc) but with required parameter checking.
Also there is another difference between lambda and Proc. Proc's "return" will stop method and will return from Proc on the other hand lambda‘s "return" will only return it's own value to the caller method.
1
2
3
4
l = lambda {|a, b| ...}
l.call('A')

#=> Error need to pass 2 arguments

Example
1
2
3
4
5
6
7
def who_win_the_title?
  l = Proc.new { return "Rafael Nadal" }
  l.call
  return "Roger Federer"
end
 
#=> "Rafael Nadal"
Because Proc's return is just like method's own return. because it works like reusable code snippet.

Method
Method is another way around in ruby to use existing method as closure.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def cheese
  puts 'cheese'
end
 
def say_cheese(block)
  block.call
end
 
say_cheese method(:cheese)
 
#=> cheese

I learned it from hear - http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

November 14, 2012

Rails On Ubuntu 12.10 Quantal Quetzal

Here's my quick guide to setting up Rails on Ubuntu 12.10 Quantal Quetzal

Step 1: Install Dependencies
The following two commands are going to upgrade any existing packages we have as well as install the dependencies we are going to need to build Ruby properly.

$sudo apt-get -y update && sudo apt-get -y upgrade

$sudo apt-get -y install build-essential zlib1g-dev libssl-dev curl libreadline-dev git-core libcurl4-openssl-dev libyaml-dev python-software-properties 

Step 2: Install Ruby 1.9.3
Grab the latest version of Ruby from ruby-lang.org
The following commands will download the Ruby source, extract it, configure and compile it, and then finally install Bundler.

wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p286.tar.gz
tar -xvzf ruby-1.9.3-p286.tar.gz
cd ruby-1.9.3-p286/
./configure
make
sudo make install
sudo gem install bundler


Step 3: Install Your Database
Choose the database you like.

Install Postgres 9.2

sudo apt-add-repository ppa:pitti/postgresql
sudo apt-get -y update
sudo apt-get -y install postgresql-9.2 libpq-dev

OR

Install MySQL 5.X.XX

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

Step 4: Install Nginx and Passenger
To install Nginx, We will use Passenger's install script. Its simple and straight.

gem install passenger
passenger-install-nginx-module

Just choose the first option (1) to download and install it for you. Accept any defaults it suggests, like installation directories.
After installation is finished, you'll get some configuration tips for Nginx to use Passenger.
Since we are compiling nginx from source, we don't get the start/stop script from Ubuntu's package.
You can get an init script from Linode's website and install it.

wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
sudo mv init-deb.sh /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults

Once that's done, you can start and stop nginx like:

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop

Rails On Ubuntu 12.10

gem install rails

and get started.