if . . . elsif . . . else in Ruby on rails

By: Brian Marick Emailed: 1607 times Printed: 2079 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Here is Ruby’s if statement in all its glory, wrapped in a method:

Download if-facts/describe.rb

def describe(inhabitant)

if inhabitant == "sophie"

puts 'gender: female'

puts 'height: 145'

elsif inhabitant == "paul"

puts 'gender: male'

puts 'height: 145'

elsif inhabitant == "dawn"

puts 'gender: female'

puts 'height: 170'

elsif inhabitant == "brian"

puts 'gender: male'

puts 'height: 180'

else

puts 'species: Trachemys scripta elegans'

puts 'height: 6'

end

end

If given ’paul’, the method would work like this:

irb(main):001:0> load 'describe.rb'

=> true

rb(main):002:0> describe 'paul'

gender: male

height: 145

=> nil

The expressions on the if and elsif lines are called test expressions. Ruby test expressions executes each of them in turn until it finds one that’s true. Then it executes the immediately following body (in this case, the lines that body use puts). If none of the test expressions is true, the body of the else is executed.

Just like everything else in Ruby, if returns a value. The value of a body is the value of its last statement (just as with method bodies), and the value of the entire if construct is the value of the selected body. So in the case of describe ’paul’, the value of the if is the value of puts ’height: 145’ (which happens to be nil).

You can leave out either or both of elsif and else. The if and end are required. If there’s no else and none of the test expressions is true, the if “falls off the end,” in which case its value is nil. Scripters often use if to pick which of several values is returned from a method. The following method returns a description of an inhabitant of my house:

Download if-facts/description.rb

def description_of(inhabitant)

if inhabitant == "sophie"

['gender: female' , 'height: 145' ]

elsif inhabitant == "paul"

['gender: male' , 'height: 145' ]

elsif inhabitant == "dawn"

['gender: female' , 'height: 170' ]

elsif inhabitant == "brian"

['gender: male' , 'height: 180' ]

else

['species: Trachemys scripta elegans' , 'height: 6' ]

end

end

I had the method return an array because puts prints each element of an array on a separate line:

irb(main):004:0> load 'description.rb'

=> true

irb(main):005:0> puts description_of('dawn')

gender: female

height: 170

=> nil


Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Brian Marick
The Question Mark Operator in Ruby on rails
unless in Ruby on rails
if . . . elsif . . . else in Ruby on rails
Methods in Ruby on rails
Command-line Arguments in Ruby on rails
Naming conventions in Ruby on rails

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
Open and manipulate CSV files in Ruby
Security in dRuby
Action Pack - Ruby On rails from request to response
if . . . elsif . . . else in Ruby on rails
The Question Mark Operator in Ruby on rails
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
escapeElement() in Ruby
header() in Ruby
What is dRuby?
Using remote objects under dRuby
Reading URL content using Ruby (HTTP)
Using Proxy to connect to URLs in Ruby
Most Emailed Articles (in Ruby)
encoding and decoding in Ruby using Base64 Module
header() in Ruby
Defining tables and classes (using MySQL) in Ruby on Rails
Benchmark module in Ruby
Using Generator in Ruby
Naming conventions in Ruby on rails
Module Abbrev in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
Security in dRuby
IMAP in Ruby
Reading emails using POP3 in Ruby
Sending emails using SMTP in Ruby
CGI class in Ruby
URL encode a string in Ruby
unescapeElement() in Ruby