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
Advertisements

October 4, 2010

Ruby On Rails Ebooks – Free downloads

Filed under: Ebooks — Tags: , , , , , , — Veerasundaravel @ 3:27 pm
Ruby on Rails logo

Image via Wikipedia

Some popular Ebooks for Ruby on Rails beginners and as well as for experts. You can find the download links with book name.

  1. Beginning Ruby: From Novice to Professional
  2. Building Dynamic Web 2.0 Websites with Ruby on Rails
  3. Ruby on Rails For Dummies
  4. Agile Web Development with Rails
  5. The Ruby Way: Solutions and Techniques in Ruby Programming
  6. Beginning Ruby on Rails
  7. Rails Recipes
  8. Rails Cookbook
  9. Ajax on Rails
  10. The Art of Rails Programmer to Programmer

February 15, 2010

Rails3 Beta installation

This post will explain how I proceeded with latest beta version Rails3 and the issues I faced.

My current version of rails and ruby:

veera@client25:~$ rails -v
Rails 2.3.5
veera@client25:~$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
veera@client25:~$

trying to install beta version of rails3.

gem install tzinfo builder i18n memcache-client rack  rake rack-test rack-mount erubis mail text-format thor bundler
gem install rails –pre

I forget to add sudo, so that I got so many errors. Let me try with sudo.

Ok some what better am getting error message only while installing generating documentation for builder-2.1.2

gem install rails –pre – was not successfull. As it haven’t installed rails3 version as I expected.

Getting error like below:

WARNING:  RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 1 gems from http://gems.rubyforge.org/

surfed google for the error, http://bparanj.blogspot.com/2008/11/warning-rubygems-12-index-not-found-for.html

lemme try the idea mentioned in that

veera@client25:~$ sudo gem sources -a http://gems.github.com
http://gems.github.com added to sources
veera@client25:~$

or else remove all the gem sources from you location, so that it can any of the source directly from net

I assume installing sudo gem install rack-mount –version=0.4.0, solved my issue.

Now I can see that rails3 is installed in my machine.

after this try the following options:

$ sudo gem install tzinfo builder memcache-client rack rack-test erubis mail text-format bundler thor i18n
$ sudo gem install rack-mount –version=0.4.0
$ sudo gem install rails –pre
$ rails -v
Rails 3.0.0.beta

then create the app in rails3 version

rails RailsBeta3

I can find a new file application.rb under config directory. Seems we have to use it instead of environment.rb

* Issues found:

1. Couldn’t find support for postgresql with Rails3.
2. Not able to start the rails app using mongrel_rails start.

My initial scaffold is working fine. rails generate scaffold group title:string description:text

Referred http://www.slideshare.net/GreggPollack/rails-3-beautiful-code-3219240 for the updated methods and options in Rails3.l,
Cool, simply and effective slide show about Rails3. Some of the important points from that:

1. ruby script/* is modified as rails(ex ruby script/server -> rails)
2. amazing and simplified routes. map.resources is modified as resources and lot more.
3. Improved syntax for respond_to method.
4. Simplified action mailer option.
5. Active record syntax is also modified, Person.all(:conditions=>”blah blah) to Person.where(“blah blah”)
6. form_remote_tag, link_to_remote tag is also simplified and the respective html source is also looks very simple. (no more onclick”new Ajax request ………..”)

looking for few more documents for the modified methods in rails3.

Tried to generate a gem in Rails3.

restful-authentication plugin is not working in rails – May be auth_logic plugin will work.

Grrrrrrr, I can’t create a older version of rails app  – rails _2.3.5_ blog235

/usr/local/lib/site_ruby/1.8/rubygems.rb:827:in `report_activate_error’: RubyGem version error: railties(3.0.0.beta not = 2.3.5) (Gem::LoadError)
from /usr/local/lib/site_ruby/1.8/rubygems.rb:261:in `activate’
from /usr/local/lib/site_ruby/1.8/rubygems.rb:68:in `gem’
from /usr/bin/rails:18

Hope rails3 alpha version should have fix for this. So now am reverting back to old version of rails by simply uninstalling all the gems related to rails3.

Older Posts »

%d bloggers like this: