home PYTHONJAVA
 

Ruby syntax

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

Instance

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

Here, suppose you have a Ruby interpreter available in your /usr/bin directory. Now try running this program as follows:

$ ruby test.rb 

This will produce the following results:

Hello, Ruby!

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

Blank in Ruby program

Blank characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in a string and are not ignored. However, sometimes they are used to explain ambiguous statements. This interpretation generates a warning when the -w option is enabled.

Instance:

a + b interpreted as a+b (This is a local variable)
a +b  is interpreted as a(+b)< Span class="pln"> (This is a method call)

The end of the line in the Ruby program

Ruby interprets semicolons and newlines as the end of a statement. However, if Ruby encounters operators at the end of the 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.

The name of a Ruby identifier can contain letters, numbers, and underscore characters ( _ ).

Reserved words

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

BEGINdonextthen
ENDelseniltrue
aliaselsifnotundef
andendorunless
beginensureredountil
breakfalserescuewhen
caseforretrywhile
classifreturnwhile
definself__FILE__
defined?modulesuper__LINE__

Here Document in Ruby

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

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

The following are different examples:

Instance

#!/usr/bin/ruby -w # -*- coding : utf-8 -*- print <<EOF This is the first way to createhere Document . Multi-line string. EOF print <<"EOF"; # Same as above This is the second way to createhere Document . Multi-line string. EOF print <<`EOC` # Excuting an order 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 results:

This is the first way to createhere document . 
Multi-line strings. 
This is the second way to createhere document . 
Multi-line strings. 
Hi there
Lo there
I said foo.
I said bar.

Ruby BEGIN statement

Syntax

BEGIN {
   Code
}

Declaration code will be called before the program runs.

Instance

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

This will produce the following results:

initialize Ruby Programs
This is the main Ruby Programs

Ruby END statement

Syntax

END {
   Code
}

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

Instance

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

This will produce the following results:

initialize Ruby Programs
This is the main Ruby Programs
Stop Ruby Programs

Ruby Notes

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

# I am a comment, please ignore me.

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

name = "Madisetti" < Span class="hl-comment"># This is also 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.

The following is another form. This block comment hides the line between =begin/=end on the interpreter:

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





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.