Installing rmagick on Windows

Prerequisites:
Ruby > 1.8.6
DevKit (any version)
No other ImageMagick installation or PATH entry

Step 1:
Installing ImageMagick:

Download ImageMagick:

http://www.imagemagick.org/download/binaries/ImageMagick-6.7.0-5-Q16-windows-dll.exe

Install ImageMagick:
*Important: The installation path should NOT contain any spaces. Ideally select “C:\ImageMagick-6.7.0-Q16″

Please make sure to select the below options:

Step 2:
Installing rmagick:

Use the following command to install rmagick gem:

gem install rmagick --version=2.12.2 --platform=ruby -- --with-opt-lib=c:/ImageMagick-6.7.0-Q16/lib --with-opt-include=c:/ImageMagick-6.7.0-Q16/include

Most of the rmagick components have no documentation. So don’t worry about the “no rdoc”/”no ri” warnings.

Happy coding !!!

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Leave a Comment

Filed under Ruby, Ruby on Rails, Windows

Class Instance Variables

Ruby has a unique concept, namely Class Instance Variables that is not common in other programming languages. Ruby has instance variables, class variables and class instance variables. Lets take a quick look at what I am talking about.

Example 1

class Resource
  @@developers = []


  def self.developers
    return @@developers
  end


  def add_to_pool
    @@developers << self
  end


  def initialize(resource_name)
    @resource_name = resource_name
  end
end


Resource.new(‘Matz’).add_to_pool
Resource.new(‘David’).add_to_pool
Resource.new(‘James’).add_to_pool


puts Resource.developers.size    # 3

The output is pretty obvious here. Now lets consider a practical problem with this approach.

Example 2

class RubyDeveloper < Resource; end
class JavaDeveloper < Resource; end


RubyDeveloper.new(‘Matz’).add_to_pool
RubyDeveloper.new(‘David’).add_to_pool


JavaDeveloper.new(‘James’).add_to_pool


puts RubyDeveloper.developers.size    # ?
puts JavaDeveloper.developers.size    # ?

The output here is:
>3
>3

Here’s the problem, how do we restrict the class variable to a given class and prevent any modification by its subclass?

Class Instance Variables to the rescue

Example 3

class Resource


  class << self
    def developers
      return @developers
    end


    def developers=(developers)
      @developers | |= [ ]
    end
  end


  def add_to_pool
    self.class. developers | |= [ ]
    self.class. developers << self
  end


  def initialize(resource_name)
    @resource_name = resource_name
  end
end


class RubyDeveloper < Resource; end
class JavaDeveloper < Resource; end


RubyDeveloper.new(‘Matz’).add_to_pool
RubyDeveloper.new(‘David’).add_to_pool


JavaDeveloper.new(‘James’).add_to_pool


puts RubyDeveloper.developers.size    # 2
puts JavaDeveloper.developers.size    # 1

Simplifying the code:

class << self
  def developers
    return @developers
  end


  def developers=(developers)
    @developers | |= [ ]
  end
end

can be achieved by:

class << self
  attr_accessor :developers
end

Rewriting the Resource class

class Resource
  class << self; attr_accessor :developers; end


  def add_to_pool
    self.class. developers | |= [ ]
    self.class. developers << self
  end


  def initialize(resource_name)
    @resource_name = resource_name
  end
end

Just to summarize:

Declaring the Class Instance Variable:

class << self; attr_accessor :developers; end

Accessing the Class Instance Variable:

Inside the class, where self is the class object:

self.developers

Inside an action in the class, where self is the instance:

self.class.developers

Hope that was interesting.
Happy coding!!!

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Leave a Comment

Filed under Ruby, Ruby on Rails

Access Control in Ruby

After stumbling upon the access level modifiers for a long time, one day I decided to understand the ins-and-outs about the concept of public, protected and private in Ruby.

Let’s understand the concept of Access Control in Ruby with the help of some simple examples:

Understanding Private methods in Ruby:

Example 1

def MyClass
  def one
    puts “From one”
  end


  def two
    one
  end


  private :one
end

mc = MyClass.new
mc.two    # From one

NOTE: In the method “two”, the implicit receiver is “self”, where “self” is the instance “mc”
Pretty obvious output, as expected.

Let’s take a deep dive:

Example 2

class MyClass
  def one
    puts “From one”
  end


  def two
    MyClass.new.one
  end


  private :one
end

mc = MyClass.new
mc.two    # ?

The output here is:
NoMethodError: private method ‘one’ called for <MyClass:0x3074f24>

Before we get into the details, let’s consider a couple more examples:

Example 3

class MyClass
  def one
    puts “From one”
  end


  def two
    self.one
  end


  private :one
end

mc = MyClass.new
mc.two    # ?

The output here is:
NoMethodError: private method ‘one’ called for <MyClass:0×3067540>

One last example with private and we should be good to conclude.

Example 4

class MyClass
  def one
    puts “From one”
  end


  private :one
end


class MySubClass < MyClass
  def two
    one
  end
end

mc = MySubClass.new
mc.two    # ?

The output here is:
>From one

To conclude:

Private Visibility:

1. Private methods are private to the instance (and not to the class).
2. Private methods are accessible from sub-classes.
3. While accessing private methods, you cannot specify the receiver (even if its “self”), receiver is always implicit, never explicit.
4. “Private” in Ruby is what “Protected” is in many other languages.
5. In Ruby, no method is perfectly private or hidden.

Understanding Protected methods in Ruby:

Example 5

class MyClass
  def one
    puts “From one”
  end


  def two
    one
    MyClass.new.one
    self.one
  end


  private :one
end

mc = MyClass.new
mc.two    # ?

The output here is:
>From one
>From one
>From one

To conclude:

Protected Visibility:

1. Protected methods are visible to all the instances of the same class as well as sub-classes.
2. Protected methods are also accessible from sub-classes. In short, Access control in Ruby neither applies to nor impacts the object hierarchy.
3. While accessing protected methods, you can specify the receiver, receiver can by explicit or implicit.
4. “Protected” in Ruby is not quite common in other programming languages.
5. What’s the use if its accessible to all the instances as well as from sub-classes?
Private and Protected methods are not directly accessible outside the class.

Public Visibility:

1. The default access level is “Public”. Public methods are accessible from anywhere*.
(*anywhere – within the class, outside the class, same instance, other instances of same class and sub-classes)

Important: Also, in a subclass, one can override the access level modifier specified for any given action in the parent class.

Hope this helps understanding the basics of Access Control in Ruby. As always said, Ruby is truly object-oriented.
With this said, Access Level Modifiers should not be a hurdle hereon.

Food for Thought:
Private and Protected method declaration in Ruby is by-passed by the
message sending syntax.

class MyClass
  def one
    puts “From one”
  end


  private :one
end

mc = MyClass.new
mc.one    # NoMethodError: private method 'one' called for <MyClass:0x3089244>

mc.send(:one)    # From one

I haven’t been able to figure out the reason for this ambiguity in Ruby, the research is still on. If anyone has any ideas, please keep me posted.

In Ruby 1.9, we have a new method defined in the “Object” class named “public_send”. This respects the access level modifiers specified for any given action.

Happy coding !!!

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Leave a Comment

Filed under Ruby, Ruby on Rails

Riding Rails on Ruby Enterprise Edition

Passenger allows Ruby on Rails applications to use about 33% less memory, when used in combination with Ruby Enterprise Edition

I am putting down steps to install Ruby Enterprise Edition and configure Passenger to use Ruby Enterprise Edition

Download Ruby Enterprise Edition

You can download Ruby Enterprise Edition here.

Or through terminal,

wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-X.X.X-YYYY.MM.tar.gz

Install Ruby Enterprise Edition

Extract the files:

tar -xvzf ruby-enterprise-X.X.X.tar.gz

Run the installer. As root,

./ruby-enterprise-X.X.X/installer

Set the path environment variable:

vim ~/.bash_profile

Add the following line:

export PATH=/opt/ruby-enterprise-X.X.X-YYYY.MM/bin:$PATH

Load the path environment variable in the current session:

As root,

source ~/.bash_profile

That’s it, we are done installing Ruby Enterprise Edition.

You can verify by:

ruby -v

ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0×6770, Ruby Enterprise Edition 2010.01

DO remember that this is a fresh installation of Ruby, that implies, you need to install all the necessary gems again. Ruby Enterprise Edition by default installs the latest stable version of Rails and the Passenger gem as well.

Now you can install and configure the Passenger module for Apache as described in article found at http://adhirajrankhambe.wordpress.com/2010/03/20/riding-rails-the-php-way/.

References:

http://www.rubyenterpriseedition.com/
http://www.rubyenterpriseedition.com/documentation.html

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Leave a Comment

Filed under Ruby, Ruby on Rails

Riding Rails the PHP way

I have been working with PHP for quite some time now, though my first love has always been Ruby on Rails :)

While working with PHP, I realized that deployment of a PHP application is far more easier as compared to deployment of a Ruby on Rails application. So I was wondering if there could be a way to deploy a Ruby on Rails application in a similar fashion, as easy as you check out the Rails project in a directory on the server, configure a VirtualHost in the webserver, restart the webserver and there you go, everything should work as expected.

Luckily I did find a saviour,

“Phusion Passenger” a.k.a “mod_rails” or “mod_rack”

Here I am putting down steps to ride Rails on Passenger.

Install Passenger

Passenger is available as a Rubygem. As root,

gem install passenger

Install Passenger module for Apache

passenger-install-apache2-module

During installation it will prompt:

The Apache2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /usr/bin/ruby

NOTE: Do NOT copy-paste from here. Make sure you add the lines that passenger prompts you with.
It is specific to the OS installation. Mine was a Fedora Core 10 64-bit installation.

Next, it will help you to configure a VirtualHost for Apache

<VirtualHost *:80>
  ServerName www.yourhost.com
  DocumentRoot /somewhere/public

    <Directory /somewhere/public>
      AllowOverride all
      Options -MultiViews
    </Directory>

</VirtualHost>

Restart Apache:

As root,

/sbin/service httpd restart

That's it, we are done.
Point your browser to your domain, and you should be able to see your Ruby on Rails Application.

Do keep in mind that the default Rails environment while using Passenger is “production”
To access your application in “development” or any other environment add the following line in your VirtualHost configuration:

RailsEnv development

Restarting your application deployed in production environment:

To restart your Rails application deployed in production environment you can restart Apache.
But that's not a good solution if you are hosting multiple applications on the same server, or even generally, it doesn't sound like a good idea to restart Apache every now and then.
To overcome that, you can restart your application in the following way:

From Rails Root Directory,

touch tmp/restart.txt

That's it, this will restart you Rails application.

The best part is, Passenger is compatible with all the Rubygems and Rails plugins.

So nothing should break. I personally tried running a complex Rails application that uses Solr as the search engine along with acts_as_solr plugin, backgroundRB server for background processes, attachment_fu for file uploads etc, etc.

Everything worked like a charm.

For further configuration, please refer to the Passenger documentation found here:

You can also use Passenger to serve Non-Rails Ruby web applications. That can be a good alternative to the already prevalent mod_ruby module for Apache.
If you are using Nginx as your webserver, you can use Passenger module for Nginx.

There's just one disadvantage to Passenger:

It doesn't work on Windows. In any case, I personally never prefer to develop Rails (or any other) applications on Windows :)

Good news for Mac users:

fingertips has come up with an OS X preference pane for Phusion Passenger (a.k.a. mod_rails) that makes it a cinch to deploy Rails applications using Passenger on the Mac. It can be as simple as dragging a Rails application folder onto the preference pane! This is absolutely ideal for quick and easy Rails development on OS X.

You can download the Passenger Preference Pane here

References:

http://www.modrails.com/documentation/Users%20guide%20Apache.html

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

1 Comment

Filed under Ruby, Ruby on Rails