Computer

Introduction to Ruby

If you are looking for a flexible but powerful programming language that can be used in internet/web development, text processing, creation of games and as a part of the web framework Ruby on Rails, Ruby easily fits the description.

Ruby’s Selling Points

  • High-level-It’s easy to read and write.
  • Interpreted-To run and write Ruby you don’t need a compiler.
  • Object-oriented-it allows users to manipulate data structures known as objects in order to build and execute programs.
  • Easy to use-it emphasizes human needs over those of the computer.

In Ruby data can come in the form of numbers, Booleans and strings. You cannot use quotation marks with Booleans otherwise Ruby will interpret it is as a string instead of a value that can be False or True. Ruby is case sensitive. It can also do math. Here a variable is a basic computer programming concept and it’s a name or word that grasps a particular value. To declare a variable in Ruby, you just write out a name and use the equals sign. e.g.

my_num=50

Exponentiation raises a number (the base) to the power of the other (exponent). e.g.

2**3 is 8 i.e. 2*2*2
3**2 is 9 i.e. 3*3

Modulo returns the division remainder. e.g.

25 % 7 would be 4

The code is written in the editor, it’s interpreted and the result shown on the console. The program that takes a written code and runs it is known as the Interpreter. The print command takes whatever you give it and prints it on the screen. The put command adds a new blank line after what you want it to print.

Everything in Ruby has certain inbuilt abilities called methods. You call a method in ruby using the .operator. The .length method tells how long a string is i.e. the number of characters including numbers, letters, symbols and spaces of a string. e.g.

“I love coffee”.length
# == > 13

This can be applied in a credit card scenario. Ruby can check the validity of the credit card. The .reverse method gives a backwards version of a given string. This can be quite useful in a situation where one wants to sort out values from highest to lowest as opposed to lowest to highest. The .downcase and .upcase methods convert strings to lowercase or uppercase respectively. The # sign is for comments in ruby.

Multiline comments require a =begin and =end.

Control Flow

Control flow gives the flexibility that enables programs to produce different results depending on the environment. Different outcomes result from computations, information typed by the user or values from another part of the program.

Statements

In Ruby, the If statement executes a block of code after the If statement if it’s true but skips it if it’s false. Indentation of the print statement isn’t absolutely necessary since Ruby doesn’t recognize whitespace i.e. blank lines and spaces. Rubyists follow the convention though. The block of code after an If should be indented using two spaces. e.g.

If 1 > 10
Print “I’m Successful”.
End

The end part tells Ruby you are done with your If statement.

In addition to the If statement, there is an else statement. It actually tells Ruby that if an expression is true, Ruby should run a block of code but if it’s not true, the code after the else statement should be run. e.g.

If 1 < 10
Print “I’m not successful”
Else
Print “I’m successful”

You might require more than two options though. The else if statement can be used in that case. It can add any alternative number to an if/else statement. e.g.

If y < x Puts “y is less than x” Else if y > x
Puts “y is greater than x”
Else
Puts “y equals x”
End

There are times you might want to use control flow for checking if something is false and not true. You can either reverse your If/else statement or make use of an Unless statement. E.g. one might want to eat unless hungry i.e. while one is not hungry, they read, but if hungry, they eat.

Unless hungry
#Do some reading
Else
#Have some food

We already established that if we want to assign values to variables, the assignment operator = is used but if we want to check whether two things are equal, the comparator/relational operator == is used. E.g.

a = 2
b = 2
If a == b
Print “a and b are equal”
End

The != comparator is used to check if values are not equal. You can also check if a value is greater than, less than, less than or equal to or greater than or equal to. The operators look like below:

  • Greater than >
  • Less than <
  • Less than or Equal to <= • Greater than or Equal to >=

In addition to comparators, one can also use Boolean or logical operators. Ruby includes three: (||), (&&) and not (!). Boolean operators result in two values: false or true. The Boolean operator and (&&) results in true only when the expressions on either side of (&&) are true. It works in the following manner:

  • True && True #=> true
  • True && False #=> false
  • False && False #=>false
  • False && True #=>false

The Or (||) operator is known as an inclusive Or since it adds to true when both or either expression is true i.e.

True || True #=> true
True || False #=> true
False || True #=> true
False || False #=> false

The Boolean operator not (!) makes false values true and vice-versa.

! False #=> true
! True #=> false

A combination of Boolean operators can be a useful tool for your programs. Parentheses are used to control the evaluation order. This is because expressions in parentheses are evaluated before what is outside parentheses.

Be the FIRST to Know - Join Our Mailing List!

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Previous ArticleNext Article
The creative energy behind nairobiTechie. He loves being caught up in the rain and he is happiest with a cup of tea on his side creating content and innovating. Contact him on kyaloeriq@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Send this to a friend