Ruby Basic Syntax

Ruby is a pure Object-Oriented language developed by Yukihiro Matsumoto (also known as Matz in the Ruby community) in the mid 1990’s in Japan. To program in Ruby is easy to learn because of its similar syntax to already widely used languages. Here, we will learn the basic syntax of Ruby language.
Let us write a simple program to print “Hello World!”.

filter_none

edit
close

play_arrow

link
brightness_4
code

# this line will print "Hello World!" as output.
puts "Hello World!";

chevron_right


Output:

Hello World!

End of a line in Ruby

Ruby interprets newline characters(\n) and semicolons(;) as the end of a statement.
Note: If a line has +, – or backslash at the end of a line, then it indicates the continuation of a statement.



Whitespace in Ruby

Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in a string, i.e. it ignores all spaces in a statement But sometimes, whitespaces are used to interpret ambiguous statements.
Example :

a / b interprets as a/b (Here a is a variable)
a b interprets as a(b)
(Here a is a method)

filter_none

edit
close

play_arrow

link
brightness_4
code

# declaring a function named 'a' which accepts an
# integer and return 1
def a(u) return 1 end
   
# driver code
a = 3 
b = 2
   
# this a + b interprets as a + b, so prints 5 as output
puts(a + b)
  
# this a b interprets as a(b) thus the returned
# value is printed
puts(a b)

chevron_right


Output:

5
1

Ruby BEGIN and END statement

BEGIN statement is used to declare a part of code which must be called before the program runs.

Syntax:

BEGIN
{
    # code written here
}

Similarly, END is used to declare a part of code which must be called at the end of program.
Syntax:

END
{
    # code written here
}

Example of BEGIN and END

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program of BEGIN and END
puts "This is main body of program"
   
END 
{
   puts "END of the program"
}
BEGIN 
{
   puts "BEGINNING of the Program"
}

chevron_right


Output:

BEGINNING of the Program
This is main body of program

Ruby comments

A comment hides some part of code, from the Ruby Interpreter. Comments can be written in different ways, using hash character(#) at the beginning of a line.
Syntax :

#This is a single line comment
#This is multiple
#lines of comment
=begin
This is another
way of writing 
comments in a 
block fashion
=end

Identifiers in Ruby

  • Identifiers are names of variables, constants and functions/methods.
  • Ruby identifiers are case sensitive.
  • Ruby identifiers may consists of alphanumeric characters and also underscore ( _ ).

For example: Man_1, item_01 are examples of identifiers.

Keywords in Ruby

The reserved words in Ruby which cannot be used as constant or variable names are called keywords of Ruby.

BEGIN do next then
END else nil true
alias elsif not undef
and end or unless
begin ensure redo until
case for retry while
break false rescue when
def in self __FILE__
class if return while


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.