if..elsif

There will no doubt be occasions when you will need to take multiple different actions based on several alternative conditions. One way of doing this is by evaluating one if condition followed by a series of other test conditions placed after the keyword elsif. The whole lot must then be terminated using the end keyword.

For example, here I am repeatedly taking input from a user inside a while loop. An if condition tests whether the user enters “q” (I’ve used chomp() to remove the carriage return from the input). If “q” is not entered, the first elsif condition tests whether the integer value of the input (input.to_i) is greater than 800; if this test fails, the next elsif condition tests whether it is less than or equal to 800:

if_elsif.rb

while input != 'q' do
   puts("Enter a number between 1 and 1000 (or 'q' to quit)")
   print("?- ")
   input = gets().chomp()
   if input == 'q'
      puts( "Bye" )
   elsif input.to_i > 800
      puts( "That's a high rate of pay!" )
   elsif input.to_i <= 800
      puts( "We can afford that" )
   end
end

The problem with this program is that, even though it asks the user to enter a value between 1 and 1,000, it accepts values less than 1 (incidentally, if you really want a salary in negative figures, I’ll be glad to offer you a job!) and greater than 1,000 (in which case, don’t look to me for employment!).

You can fix this by rewriting the two elsif conditions and adding an else section that executes if all the preceding tests fail:

if_elsif2.rb

if input == 'q'
   puts( "Bye" )
elsif input.to_i > 800 && input.to_i <= 1000
   puts( "That's a high rate of pay!" )
elsif input.to_i <= 800 && input.to_i > 0
   puts( "We can afford that" )
else
   puts( "I said: Enter a number between 1 and 1000!" )
end

if_else_alt.rb

Here’s another example of a longer sequence of if..elsif sections followed by a catchall else section. This time the trigger value, i, is an integer:

days2.rb

def showDay( i )
   if i == 1 then puts("It's Monday" )
   elsif i == 2 then puts("It's Tuesday" )
   elsif i == 3 then puts("It's Wednesday" )
   elsif i == 4 then puts("It's Thursday" )
   elsif i == 5 then puts("It's Friday" )
   elsif (6..7) === i then puts( "Yippee! It's the weekend! " )
   else puts( "That's not a real day!" )
   end
end

Notice that I’ve used the range (6..7) to match the two integer values for Saturday and Sunday. The === method (that is, three = characters) tests whether a value (here i) is a member of the range. In the previous example, the following:

(6..7) === i

could be rewritten as this:

(6..7).include?(i)

The === method is defined by the Object class and overridden in descendant classes. Its behavior varies according to the class. As you will see shortly, one of its fundamental uses is to provide meaningful tests for case statements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.32