Ruby syntax

Let's write a simple Ruby program. All Ruby file extensions are .rb . So, put the following source code in the test.rb file.

Examples

 

# ! / usr / bin / ruby ??-w
 
puts " Hello, Ruby! " ;

 

Here, it is assumed that a Ruby interpreter is already available in your / usr / bin directory. Now, try to run this program as follows:

$ ruby ??test.rb

. rb

This will produce the following result:

Hello, Ruby!

, Ruby !

You have seen a simple Ruby program, now let's look at some basic concepts related to Ruby syntax:

White space in Ruby program

White space characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in the string. However, sometimes they are used to explain ambiguous sentences. When the -w option is enabled, this interpretation produces a warning.

Examples:

a + b is interpreted as a + b (this is a local variable)+ b is interpreted as a + b (this is a local variable)

a + b is interpreted as a (+ b) (this is a method call)+ b is interpreted as a (+ b ) (this is a method call)

End of line in Ruby program

Ruby interprets semicolons and newlines as the end of statements. However, if Ruby encounters operators at the end of a line, such as +,-, or backslashes, they represent a continuation of a statement.

Ruby identifier

Identifiers are the names of variables, constants, and methods. Ruby identifiers are case sensitive. This means that Ram and RAM are two different identifiers in Ruby.

Ruby identifier names can contain letters, numbers, and underscore characters (_).

Reserved word

The following table lists the reserved words in Ruby. These reserved words cannot be used as constant or variable names. However, they can be used as method names.

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

Here Document in Ruby

"Here Document" refers to the creation of multi-line character strings. After <<, you can specify a string or identifier to terminate the string, and all lines up to the terminator after the current line are the value of the string.

If the terminator is enclosed in quotation marks, the type of quotation marks determines the line-oriented string type. Please note that there must be no spaces between the << and the terminator.

Here are different examples:

Examples

# ! / usr / bin / ruby ??-w
# -*-coding: utf-8-*-
 
print << EOF
    This is the first way to create a document here . Multiline strings.
EOF print << " EOF " ;                 #Same as above
 
    This is the second way to create a document here . Multiline strings.
EOF Print << ` EOC `                  # execute command
 
    echo hi there echo lo there EOC print << " foo " , << " bar " #You can stack them
    I said foo .
 Foo I said bar .
 Bar

 
This will produce the following result:
This is the first way to create a document here.here document .
Multiline strings.Multiline strings.
This is the second way to create a document here.This is the second way to create a document here .
Multiline strings.Multiline strings.
hi there
lo there
I said foo..
I said bar..

Ruby BEGIN statement

grammar

BEGIN { {  

 code

}}

Declare that the code will be called before the program runs.

Examples

 

# ! / usr / bin / ruby
 
puts " This is the main Ruby program " BEGIN {
    puts " Initialize Ruby program "
}

 

This will produce the following result:

Initialize Ruby program Ruby program

This is the main Ruby programThis is the main Ruby program

Ruby END statement

grammar

END { {    

      code

}}

Declare code will be called at the end of the program.

Examples

 
# ! / usr / bin / ruby
 
puts " This is the main Ruby program " END {
    puts " Stop Ruby program "
}
 BEGIN {
    puts " Initial Ruby program "
}

 
 

This will produce the following result:

Initialize Ruby program Ruby program

This is the main Ruby programThis is the main Ruby program

Stop Ruby programStop Ruby program

Ruby notes

Comments hide one line, part of a line, or several lines from the Ruby interpreter. You can use characters (#) at the beginning of the line:

#I am a comment, please ignore me.

Alternatively, the comment can follow the same line of the statement or expression:

name = " Madisetti " # this is a comment

You can comment multiple lines as follows:

#This is a comment.
#This is also a comment.
#This is also a comment.
#This is still a comment.

Here is another form. This block comment hides the line between = begin / = end from the interpreter:

= begin
This is a comment. This is also a note. This is also a note. This is still a note.
= end

 

Subscribe For Daily Updates