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 deliver the accompanying results:

Hello, Ruby!

You have seen a basic Ruby program, presently how about we investigate a portion of the essential ideas identified with 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 neighborhood variable) 

a +b  is translated as a(+b) (This is a strategy call)

The stopping point in the Ruby program

Ruby translates semicolons and newlines as the finish of an announcement. Be that as it may, if Ruby experiences administrators toward the stopping point, for example, +, - or oblique punctuation lines, they speak to a continuation of an announcement.

Ruby Identifier

Identifiers are the names of factors, constants, and techniques. Ruby identifiers are case touchy. This implies Ram and RAM are two distinct identifiers in Ruby.

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

Reserved words

The following table records the saved words in Ruby. These saved words can't be utilized as names for constants or factors. In any case, they can be utilized as strategy names.

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

Here Document in Ruby

"Here Document" alludes to the making of multi-line strings. After <<, you can indicate a string or identifier to end the string, and all lines after the present line up to the eliminator are the estimation of the string.

If the eliminator is encased in quotes, the sort of statements decides the line-arranged string type. Note that there must be no spaces between the << and eliminators.

The following are distinctive 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 conceal a line from the Ruby mediator, or a piece of a line, or a couple of lines. You can utilize the character ( # ) toward the start of the line:

# I am a remark, if it's not too much trouble overlook me.

Or, the remark can pursue a similar line of the announcement or expression:

name = "Madisetti" # 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.