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

February 1, 2011

Rails ActiveRecord: validate single attribute


 

In Rails ActiveRecord we can validate a whole object, but we cannot validate a particular attribute of the object something like below:

>> u = User.new(:email=>"blah blah", :name=> "Some Correct Name")
>> u.valid?
>> false

>> u.valid?(:email)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):2:in `valid?'
from (irb):2
>>

 

Here some quick solution for validating a particular attribute of an ActiveRecord object. Create a .rb file like below place in config\initializers folder.

module ValidateAttribute
  def self.included(base)
    base.send :include, InstanceMethods
  end

  module InstanceMethods
    def valid_attribute?(attribute_name)
      self.valid?
      self.errors[attribute_name].blank?
    end
  end
end

ActiveRecord::Base.send(:include, ValidateAttribute) if defined?(ActiveRecord::Base 

 

Now you can validate a particular attribute of an object as follows:

>> u = User.new(:email=>"blah blah", :name=> "Some Correct Name")
>> u.valid?
>> false

>> u.valid?(:email)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):2:in `valid?'
from (irb):2
>>
>> u = User.new(:email=>"blah blah", :name=> "Some Correct Name")
>> u.valid?
>> false

>> u.valid_attribute?(:email)
>> false

>> u.valid_attribute?(:name)
>> true 

%d bloggers like this: