Let's write a simple ruby program. All Ruby source files will be in extension. RB. So, put the following source code in a test.rb file.
#!/usr/bin/ruby-w
puts "Hello, ruby!";
Here, suppose you have a Ruby interpreter installed, which can be found in the/usr/bin directory. Now try running this program as follows:
This will produce the following results:
From the example above, we've seen a simple ruby program, now let's look at a few basic concepts about Ruby syntax:
Whitespace characters in Ruby programs:
In Ruby code, whitespace characters are generally ignored, such as spaces and tabs, unless they appear in a string. However, sometimes they are used to explain ambiguous statements. Annotation of this type-w option produces a warning when enabled.
Instance:
A + B is interpreted as a+b (here's a local variable)
a +b is interpreted as a (+b)
End of Ruby program line:
Ruby interprets the end of a statement with semicolons and line breaks. However, if Ruby encounters an operator, such as a row at the end of a +,-or backslash, the statement continues.
Ruby Identifier:
Identifiers are variables, constants, and methods. Ruby's identifiers are case-sensitive. RAM and Ram are two different identifiers in Ruby.
Ruby's identifier name can be alphanumeric characters and underscores (_).
Reserved words:
The following list shows the reserved words in Ruby. These reserved words cannot be used as constants or variable names. However, they can be used as method names.
Ruby in Heredoc:
"Here Document" refers to the creation of multiple-line strings. Following << you can specify a string or an identifier to terminate the string literal, the value of the Terminator string for all rows after the current line.
If the Terminator is a reference, the type of the quotation marks determines the type of the line-oriented string constant. Note there should be no spaces between the << terminator.
Here are the different examples:
#!/usr/bin/ruby-w
Print <<eof This is the
way of creating here
document IE. multiple line string.
EOF
print << "EOF"; # Same As above this is the
second way of creating to
document IE. multiple line string.
EOF
print << ' EOC ' # execute commands
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 way of creating
her document ie. multiple line string.
This is the second way of creating
her document ie. multiple line string.
Hi there
Lo there
I said Foo.
I said Bar.
Ruby BEGIN Statement
Grammar:
The declaration code is invoked before the program is run.
Example:
#!/usr/bin/ruby
puts "this are main Ruby program"
BEGIN {
puts ' initializing Ruby program '
}
This will produce the following results:
Initializing Ruby program It
main Ruby program
Ruby End Statement
Grammar:
Declaring code is called the end of a program.
Grammar:
#!/usr/bin/ruby
puts ' This is main Ruby program ' end
{
puts ' terminating Ruby program '
}
BEGIN {
puts "initializing Ruby program"
}
This will produce the following results:
Initializing Ruby program It
main Ruby program
terminating Ruby program
Ruby annotation:
Note hides a row, part of a line, or several lines of Ruby interpreter ignores the interpreter code. A line that begins with the hash character (#) that you can use:
# I am a comment. Just ignore me.
Alternatively, the annotation might be a statement or expression after the same line:
Name = "Madisetti" # this is again comment
You can comment out multiple lines as follows:
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.
Here is another form. This block comment hides several lines of comment: =begin/=end:
=begin This is
a comment.
This is a comment, too.
This is a comment, too.
I said that already.
=end