Ruby Ifelse

Ruby Ifelse operator is a standard conditional operator that allows us to take a certain action based on certain condition.

Ruby IfElse Example

#!/usr/bin/ruby
# Filename: ifelse.rb
 
ruby_var  = 1
if ruby_var  == 2
 puts "i am two"
else 
 puts "i am one"

Ruby If-ElseIf Example

#!/usr/bin/ruby
# Filename: ifelse.rb
 
ruby_var  = 1
if ruby_var  == 2
 puts "i am two"
elseif ruby_var == 3 
 puts "i am three"
else 
 puts "i am one"

Conditions in Ruby

Following are the conditions used in Ruby along with their signs:

==  	equal
!= 	not equal to
> 	greater than
< 	less than
>= 	greater than or equal to
<= 	less than or equal to 

Ruby String comparison can be learnt for this another section.

Post your Answer

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Links to specified hosts will have a rel="nofollow" added to them.

More information about formatting options