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
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 Span>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.
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 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
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
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
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:
Or, the comment can follow the same line of the statement or expression:
You can comment multiple lines as follows:
The following is another form. This block comment hides the line between =begin/=end on the interpreter: