Ruby is incredibly simple, powerful and beautiful language. It is also a language that is easy to learn, even for people who are new to programming. If you want to start with programming, or learn another programming language, this is your chance. This series will help you learn all you need to start programming in Ruby.

Getting Started With Ruby the Easy Way Part 1 (Comments, Variables, Strings).

Getting Started With Ruby the Easy Way Part 2 (Data Types Pt1).

Getting Started With Ruby the Easy Way Part 3 (Data Types Pt2, Control Flow Pt1).

Getting Started With Ruby the Easy Way Part 4 (Control Flow Pt2).

Getting Started With Ruby the Easy Way Part 6 (Methods).

Getting Started With Ruby the Easy Way Part 7 (Recursion, Scope, OOP Pt1).

Getting Started With Ruby the Easy Way Part 8 (OOP Pt2).

Getting Started With Ruby the Easy Way Part 9 (OOP Pt3).

Getting Started With Ruby the Easy Way Part 10 (OOP Pt4 and Beyond).

Control flow Pt3

In the previous part, you’ve learned about how to control flow in your Ruby code with case statement and loops. However, this is still not everything. There are two additional control flow tools in Ruby you can use to write more complex code and build bigger project. These tools are ranges and iterators. Let’s take a look at both.

Ranges

A range represents a sequence. This sequence can be composed of numbers or characters. For example, you can have range from 0 to 10, or from 56 to 82 or from “A” to “Z”. All these are examples of valid ranges. In Ruby, there are two ways, or special operators, you can use when you want to create range. These range operators are .. and ....

The first operator, the two-dot form, is also called “inclusive range”. The second one, the three-dot form, “exclusive range”. These alternative names also suggest where lies the difference. The inclusive range creates a range that includes the high value of the range you specified. The three-dot form creates a range that excludes this value.

Some people learning Ruby find it easier to work with ranges when they use these alternative names. It is much easier to recall that the inclusive range includes high value while the exclusive range excludes it. You will probably agree with it. If you want, remember the alternative names for ranges. I can make it easier for yourself to learn this piece of Ruby syntax so you can choose the right operator for a specific situation.

##
## Example of range with integers using inclusive range
a = (1..10).to_a
print a ## [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

## Example of range with integers using exclusive range
aa = (1...10).to_a
print aa ## [1, 2, 3, 4, 5, 6, 7, 8, 9]

## Example of range with negative integers using inclusive range
b = (-15..-1).to_a
print b ## [-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]

## Example of range with characters using exclusive range
bb = (-15...-1).to_a
print bb ## [-15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2]

## Example of range with characters using inclusive range
c = ("a".."z").to_a
print c ## ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

## Example of range with characters using exclusive range
cc = ("a"..."z").to_a
print cc ## ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"]

## This will not work
c = ("z".."a").to_a
print c ## []

Side note no.1: The _to_a_ method in the example above is used to convert the ranges to an array. You’ve learned about arrays in the second part of this series. If you try to output the values without this method, you will get exactly the code you wrote. For example, you will get _1..10_ instead of an array with all numbers between “1” and “10”.

Side note no.2: Remember that you can output some value to console in Ruby in two ways. You can use either _put_ or _print_ method. You’ve learned about them and console in the first part. The example above uses _print_ because it prints everything on the same line. And, in case of arrays, it also includes the square brackets.

A very good case for using ranges is when you work with case statements. Then, you can use ranges for when values. You’ve actually already used ranges for this example. Where? You’ve done this in the fourth part, the example with age. Let’s take a look at it.

Here, you are using exclusive ranges to create a case statement that returns development period based on child’s age. For example, this statement will return the code inside the first when case when the child is older than “0” and younger than “14”. Or, the age is smaller than “0” and bigger than “14”. Otherwise, it will skip this when case.

## Example of a case statement
## using ranges for when values.
age = 42

case age
when 0..14
  puts "Child"
when 15..24
  puts "Youth"
when 25..64
  puts "Adult"
else
  puts "Senior"
end

There is another situation when using range, the inclusive, is quite handy. This is when you quickly want to generate all characters of the alphabet. You’ve done this in the first example above, using ("a".."z").to_a. This is much faster than writing the whole alphabet by hand. It is at least ten times faster, unless you are a very fast typist.

#ruby #programming #design development

Getting Started With Ruby the Easy Way Pt5
1.60 GEEK