Friday, June 25, 2010

Include Vs Extend in Ruby

Include vs Extend

Include

Classes and modules can include modules and reuse the methods defined in the module. The method in the module is available as an instance method. The constants, methods and module variables are available once the module is mixed in.

Extend

You can extend an instance of any class with module. The method in the module is available as an instance method.

The difference is extend is only applicable for that particular instance which was extended. Where as the include is applicable to any instance of the class.

Screen cast IncludeExtend.mov

Addition to IncludeExtendRuby.mov

module Talkable
def talk
p "hi"
end
end

class Foo

end

Foo.extend Talkable

Foo.talk


If extend is used on the class instead of the object, the method becomes available as class method. The extend can be used inside the class or outside the class. Refer the screencast to see how it works.

Thursday, June 24, 2010

Upgrading Snow Leopard to Rails 3.0 Beta 4

1. gem env

should be 1.3.6 or above

2. gem update --system

Update Ruby gems to the latest version

3. sudo gem install i18n tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n bundler

Install required gems for Rails 3.0

4. sudo gem install rails --pre

Installs Rails 3.0 Beta 4

5. sudo mkdir /usr/local/lib/ruby/gems/1.9.1/gems/rails-3.0.0.beta4/lib

Create the lib directory to resolve the following error

ERROR: While executing gem ... (Errno::ENOENT)
No such file or directory - lib

Installing Ruby 1.9.2 on Snow Leopard

1)

Install Readline:

curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.0.tar.gz
tar -xvzf readline-6.0.tar.gz
cd readline-6.0
./configure --prefix=/usr/local
make
sudo make install

I ignored the message: install: you may need to run ldconfig

2)

Install Ruby 1.9.2

curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-rc1.tar.gz
tar -xvzf ruby-1.9.2-rc1.tar.gz
cd ruby-1.9.2-rc1
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
sudo make install-doc


I ignored the message: configure: WARNING: unrecognized options: --with-readline-dir

3) Update ~/.bash_profile with
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

in the last line

Open a new window and check Ruby version: ruby -v
ruby 1.9.2dev (2010-07-02 revision 28524) [x86_64-darwin10.4.0]