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"
else
   puts "you are not male"
end

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"
else
day_name = "Invalid"
end
return day_name
end
puts get_day_name("wed")

This code output is Wednesday.

Loops

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
end

While Loop

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

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
file.open("test.txt",r) do |tile|
puts file.read
end
Write File
File.open("employee.txt","a") do |file|
file.write ("sanka")
end
Create File
File.open ("index.html","w") do |file|
file.write("<h1>Hello</h1>")
end
Read and Write
File.open ("employee.txt","rt") do |file|
file.readline()
file.write ("overridden")
end

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
end

book1 = Book.new()
book1.title = "Harry Potter"
book1.auther = "JK"
book1.pages = 400
puts book1.pages

Output is 4000.

Method

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

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

puts.student1.name

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!

Leave a Reply

Your email address will not be published.