Veerasundaravel's Ruby on Rails Weblog

February 25, 2011

Why Ruby unless doesn’t have elsif or elsunless option

C language example. Illustrates if-else statement.

Image via Wikipedia


if statement supports else and elsif constructs in order to check the conditions in further level. some thing like below:

if customerName == "Fred"
  puts "Hello Fred!"
elsif customerName == "John"
  puts "Hello John!" 
elsif customername == "Robert"
  puts "Hello Bob!"
else
  puts "Hello Customer!"  
end

If  any one of the above condition is true means, it will output the corresponding string. And nothing matched means, it will result the default output string Hello Customer!. So here we can check multiple condition using elsif.

 

elsif or elsunless constructs in unless statement:

But unfortunately unless statement supports only else construct not elsif or elseunless with in it. Lets walk through some example.

unless true
  puts "one"
else
  puts "two"
end

#It will result the output as "two"
unless true
  puts "one"
elsif true
  puts "two"
else
  puts "three"
end

SyntaxError: compile error
syntax error, unexpected kELSIF, expecting kEND
unless true
  puts "one"
elseif true
  puts "two"
else
  puts "three"
end

It wont throw any error, but will result the output as "three" instead of "two".
unless true
  puts "one"
elsunless false
  puts "two"
else
  puts "three"
end

Same like above it wont throw any error, but will result the output as "three" instead of "two".

 

What are the solutions to this:

Depending upon your condition, you need to select a appropriate if or unless statement.
Prefer if statement at first, then only unless statement.
If you need check multiple conditions then go only with if statement, cos in if statement only you can easily use multiple elsif constructs.
Use unless statement when you want to check a false output condition or a meaning full condition. For example, unless housefull print “Tickets are avilable” end

 

In Programmattic way:

Consider the following unless statement with elsif conditions can be modified into if..elsif statement.

unless true
  puts "one"
elsif true
  puts "two"
else
  puts "three"
end

#Above one will result error. Lets try to modify this statement in if..elsif statement as follows:

unless true
  puts "one"
else if true
    puts "two"
  else
    puts "three"
  end
end

September 22, 2010

Bundler: Best way to manage your Gems

Bundler Gem

As the name itself will specify the purpose of it. Bundler gem is used to bundle all of your required ruby gems.

The bundler allows the specification of all dependencies in a separate place from the application itself. You can specify the required version of gems and its entire set of dependencies.
So while deploying your application in a server no need to install the gem from Rubyforge or RubyGems.org etc.

Steps to follow:

1. At the root of your application, run gem install bundler

2. Create a Gemfile in the root of your application

3. Add dependencies to your Gemfile. like gem “rspec”, 1.2.3

4. At the root, run bundle install. It will install all the gems which you specified in Gemfile from the remote system. And it will create a file called Gemfile.lock. The purpose of Gemfile.lock is to get the list of gems installed and its dependencies in your app.

5. And now run bundle package. It will make a copy of your installed gems into your application path vendor/cache. It will more helpful when you are deploying your app in any other server. Cos after running bundle package, your application will look gems into vendor/cache instead of remote systems.

For more details:

http://gembundler.com/
http://gembundler.com/rails23.html
http://yehudakatz.com/2009/11/03/using-the-new-gem-bundler-today

September 17, 2010

ActiveRecord – Tableless Model

If you want to create an ActiveRecord model without any table reference, and want to add validation for that model means, you can go with this example code.

In app/models/base_model.rb

class BaseModel < ActiveRecord::Base
def self.columns
@columns ||= [];
end

def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
sql_type.to_s, null)
end

# Override the save method to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
end

In app/models/post.rb

class Article < BaseModel
column :title, :string
validates_presence_of :title
end

In script/console

Loading development environment (Rails 2.2.2)
>> article = Article.new
=> #<Article title: nil>
>> article.valid?
=> false
>> article.errors
=> #<ActiveRecord::Errors:0x235b270 @errors={“title”=>[“can’t be blank”]}, @base=#<Article title: nil>>


Reference Links:

http://stackoverflow.com/questions/315850/rails-model-without-database/
http://railscasts.com/episodes/193-tableless-model
http://agilewebdevelopment.com/plugins/activerecord_base_without_table

Older Posts »

%d bloggers like this: