Wednesday, 9 July 2014

Ruby - If statements

With Case statements we talked about using these to control the flow and response to data being evaluated. A more typical and perhaps thorough way is to use the If statement. Case statements are pretty straight forward and simple, whereas If statements can get a bit gnarly.


YouTube Channel: [WATCH, RATE, SUBSCRIBE]

Let’s do the usual thing to get some data from the user:

puts "Where are you from?"
location = gets.chomp.downcase

Now the basic If statement looks like this:

if location == "London"
     puts "OK, a big city…"

  else
     puts "Ah, I thought you came from London"

end

That’s the most basic evaluation, if this then do something, else do something else. In the even that you have more than one alternate choice, Ruby provides the elesif which you can use multiple times and before the final else statement. Not the odd spelling! Putting this together of if statement would look like this:

puts "Where are you from?"
location = gets.chomp.downcase

if location == "London"
  puts "OK, a big city..."

  elsif location == "scotland"
    puts "ach the noo!"
   
  else
    puts "Ah, I thought you came from London"
 
end

Now you’ll notice that because we’re using gets.chomp.downcase all the date presented to the statement will start with a lower case letter. That means if you type London or Scotland it will be seen as different to london or scotland. We need a way to catch both. The easiest way would be to have a line like this:

if location == "London" or location =="london"

While this will work, it will get messy quick. Think about it, all we want is for Ruby to evaluate the statement to True or False, so we can use another method. That is to look into an array and see if the options we’ve stated is present in it. If so then Ruby will return True, if not then it’ll be false and Ruby moves onto another statement. Here’s what that looks like.

if ["London","london"].include? location

If you wiz over to the Ruby docs page for this, you’ll see it’s part of Array.

The astute will have realised we could declare an array of separate values to evaluate, then just refer to it here if myArray.include? will work just fine. Way to keep the code tidy. The full version using the above now looks like this:

puts "Where are you from?"
location = gets.chomp.downcase

if ["London","london"].include? location
  puts "OK, a big city..."
 
elsif location == "scotland"
    puts "ach the noo!"
   
  else
    puts "Ah, I thought you came from London"
 
end

There is another alternate which is to use Regular Expressions. Try replacing the first if with the following:

if location =~ /\A(london|f)\z/

This will eliminate the need to have the two options spelt out. Your choice which to use, I like arrays as they’re more readable but as you see we have many options.

One last thing on basic If statements. I’ve not used then anywhere. Ruby knows we mean then, but for readability you could include them though your code will work just fine without.

puts "Where are you from?"
location = gets.chomp.downcase

if location =~ /\A(london|f)\z/ then
  puts "OK, a big city..."
 
elsif location == "scotland" then
    puts "ach the noo!"
   
  else
    puts "Ah, I thought you came from London"
 
end

That’s it for now. In the next post we’ll have a quick look at nested If statements and go on to look at more ways to control flow and state depending on certain conditions in the program.


Mark.


0 comments: