×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Syntax

By IncludeHelp Last updated : November 16, 2024

The syntax of Ruby is widely similar to that of Python and Perl. Keywords and braces are used to define the blocks for the code. We have got various keywords to define classes and objects. The syntax differs from Python and Perl in a way that all the instance variables are kept private and are only accessible through Access Modifiers.

Let us go through the basic syntaxes:

Variables

Variables are used to hold some data and reside at a specific memory location. Ruby supports five types of variables:

1. Global variable

You need to add $ at the beginning of the variable name if you want to make the variable global. By default, nil is assigned to the variable if you don't give a value to it explicitly.

Example

$global_variable = 90

2. Instance variable

If you want to create an Instance variable, add @ at the beginning of its name. Here also nil is assigned to it in case it is not initialized.

Example

@cust_id = 12
@student_name = "Hrithik"

3. Class variable

Class variables begin with @@ and it is mandatory to initialize them.

Example

@@people_present = 0

4. Local variable

Local variables start with _ or a lowercase letter. The scope of a local variable starts from opening braces { and ends with the closing braces }.

Example

myname = "Hrithik"
_siteaddress= "Includehelp.com"

5. Constants

They always begin with uppercase letters. You will have to face an error if you are referencing a constant which is not initialized.

CONSTANT12 = 890
CONSTANTSTR = "INCLUDEHELP"

Strings

Strings are a sequence of characters. They can be declared in Ruby either by using single quotes ' ' or by using double quotes " ".

"You are learning at Includehelp.com"
'This is also a string'

Arrays

An array can be declared in Ruby in the following way:

[1,'Shyam',5.34,'Include help']

Hashes

A hash in Ruby looks like the example which is given below,

{name: "Harish"}
{:name=> "Satish"}

Basic structure of a Ruby Code

The basic structure of Ruby code is quite easy to understand. The method gets is used to input data from the user at the run time and the method puts is used for displaying the information to the user.

Observe the code given below, you will find chomp, it is used for eliminating the new line character \n from the data which is entered.

You will also see that Type conversion is taking place. By default, the values are of string type and they are getting converted into integer type through the .to_i method.

puts "Enter first value"
num=gets.chomp

puts "Enter second value"
num1=gets.chomp

pro=num.to_i*num1.to_i

puts "The product of #{num} and #{num1} is #{pro}"

Output

Enter first value
12
Enter second value
34
The product of 12 and 34 is 408

Ruby Flow Controls

Following are the flow control statements which are supported in Ruby programming language:

  1. if statement
  2. if/else statement
  3. elsif statement
  4. unless statement
  5. switch statement

Let us study each of them with examples,

1. if statement

if statement only works for a single expression. It executes it and checks whether the statement stands to be true or false.

Syntax

if (condition)
  #statement executes if the condition comes out to be True
end

Example

a = 70
b = 9

if a>b
  puts "a is greater"
end

Output

a is greater

2. if-else statement

It is very similar to if statement, only it has an additional else with it. It works in the way that if the if condition is True then the code block inside it executes otherwise the code block inside else executes.

Syntax

if(condition)
  #code block
else
  #code block
end

Example

a = 70
b = 900

if a>b
  puts "a is greater"
else
  puts "b is greater"
end

Output

b is greater

3. elsif statement

If there are more than two options in your code, then elsif statement can be used. First the if condition is checked, if it comes to be false then the pointer moves to elsif condition, if it turns out to be true to the situation then elsif code block executes.

Syntax

if (condition)
  #statements
elsif(condition)
  #statements
else
  #statements
end

Example

a = 70
b = 900
c = 567

if (a>b and a>c)
  puts "a is greater"
elsif (b>a and b>c)
  puts "b is greater"
else
  puts "c is greater"
end

Output

b is greater

4. unless statement

unless statement is used to verify a statement false rather than it is true.

Syntax

unless (condition)
  #code block
else
  #code block
end

Example

Entry = false
unless Entry
  puts "you have to wait"
else
  puts "you are good to go!"
end

Output

you have to wait

5. switch statement

The switch statement is used where you have multiple options. It is similar to elsif statement but it is more preferable than elsif. when keyword is used to check the conditions and whichever comes out to be true, the statements under it executes.

Syntax

case (variable name)
when (condition)
  #statements
when (condition)
  #statements
else
  #statements
end

Example

puts "which fruit do you like?"
fruit = gets.chomp

case (fruit)
when 'apple'
  puts 'Red colour'
when 'banana'
  puts 'Yellow colour'
when 'papaya'
  puts 'Green colour'
when 'kiwi'
  puts 'Green colour'
else
  puts 'bad choice'
end

Output

Which fruit do you like?
banana
Yellow color

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.