Ruby Course – Worldwideasy – 04

Ruby lesson 4. I will focus on Getting Control Statements (IF statement, Case Expressions, loops), Comments, and File Control. It can provide extensive knowledge.

Control Statements

A control statement is a statement that determines whether other statements are active. If a statement decides whether another statement should be executed, or one of the two statements to be executed. A loop determines the number of times another expression should be executed.

Basically have 3 control statement

  • IF
  • Switch
  • Loops

IF Statement in Ruby

If a statement is used to check the condition, it can be a condition or more. The special feature of it is that you can check any gap. The following example will make it clear to you.

ismale = false 
if ismale
   puts "you are male"
   puts "you are not male"

This Code output is you are not male.

Case Expressions

The Case Expressions is used to check several non-gap conditions. This works very fast and gets the idea below

def get_day_name(day)
day_name=" "

case day
when "mon"
day_name = "Monday"
when "tue"
day_name = "Tuesday"
when "wed"
day_name = "Wednesday"
when "tur"
day_name = "Thursday"
when "fri"
day_name = "Friday"
when "sat"
day_name = "Saturday"
when "sun"
day_name = "Sunday"
day_name = "Invalid"
return day_name
puts get_day_name("wed")

This code output is Wednesday.


Loops have main 3 loops in php

  • For loop
  • While loop

Now I show the First 3 loops using Code. See Below all code output is the same. output is : 1,2,3,4,5,6,7,8,9,10.

For Loop with Arry

num = [1,2,3,4,5,6,7,8,9,10]
for num in num 
puts num

While Loop

index = 1
while index < 8
puts index 
index + = 1

Ruby Comment

Comments are a special character in programming.

# single line comment
= multiline
  comment =

File Control using Ruby

Here we show you how to handle files without opening them. see below

Reading File"test.txt",r) do |tile|
Write File"employee.txt","a") do |file|
file.write ("sanka")
Create File ("index.html","w") do |file|
Read and Write ("employee.txt","rt") do |file|
file.write ("overridden")

Class & Object

Class is collection of objects and functions. In computer programming, a function object is a construct that allows an object with the same syntax to be called or called a normal function. Active objects are often referred to as funksters.

This is the mining of the object and class. now we can try to add it ruby. look below example.

class Book
attr_accessor :title, :author, :pages

book1 =
book1.title = "Harry Potter"
book1.auther = "JK"
book1.pages = 400
puts book1.pages

Output is 4000.


See the below example. that use code reuses it easy for developing softwares.

attr_accessor:name, :major, :gpa
def initialize (name,major,gpa)
@ name = name
@ major = major
@ gpa = gpa
student1 = ("harry","bio","3.6")
student2 = ("jhone","chem","2.1")

Output is harried.

Create a Ruby 2D Game

First, install ruby 2d gem. open cmd

gem install ruby2d

Click Here to get the Source code.

you can see the below output in-game.

Now over the Ruby basic course. you go to this link and answer the question and get a Ruby certificate. Get Certificate

Thank you for all!

More Ruby ClickHere!

See you Next Lesson Soon!