Ruby Syntax
Let's write a simple Ruby program. All Ruby files have the extension .rb. So, place the following source code in a file named test.rb.
Example
!/usr/bin/ruby -w
puts
"
Hello, Ruby!
"
;
Run Example »
Here, assume you already 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 result:
Hello, Ruby!
You have seen a simple Ruby program. Now let's look at some basic concepts related to Ruby syntax:
Whitespace in a Ruby Program
Whitespace characters in Ruby code, such as spaces and tabs, are generally ignored, except when they appear in strings. However, sometimes they are used to disambiguate statements. When the -w option is enabled, such interpretations produce warnings.
Example:
a + b 被解释为 a+b (这是一个局部变量)
a +b 被解释为 a(+b) (这是一个方法调用)
End of Line in a Ruby Program
Ruby interprets semicolons and newlines as the end of a statement. However, if Ruby encounters an operator at the end of a line, such as +, -, or a backslash, they indicate the continuation of a statement.
Ruby Identifiers
Identifiers are the names of variables, constants, and methods. Ruby identifiers are case-sensitive. This means Ram and RAM are two different identifiers in Ruby.
Ruby identifier names can contain letters, digits, and the underscore character ( _ ).
Reserved words
The following table lists the reserved words in Ruby. These reserved words cannot be used as names of 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 | yield |
| def | in | self | FILE |
| defined? | module | super | LINE |
Here Document in Ruby
"Here Document" refers to creating a multi-line string. 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 quotes, the type of quote determines the type of the line-oriented string. Note that there must be no space between<< and the terminator.
The following are different examples:
Example
!/usr/bin/ruby -w
-- coding : utf-8 --
<<
EOF
This is the first way to create a
here
document
. multi-line string.
EOF
<<
"
EOF
"
;
Same as above
This is the second way to create a
here
document
. multi-line string.
EOF
<<`
EOC
`
Execute the command
echo
hi
there
echo
lo
there
EOC
<<
"
foo
"
, <<
"
bar
"
You can stack them
I
said
foo
.
foo
I
said
bar
.
bar
Run Example »
This will produce the following result:
这是第一种方式创建here document 。
多行字符串。
这是第二种方式创建here document 。
多行字符串。
hi there
lo there
I said foo.
I said bar.
Ruby BEGIN Statement
Syntax
BEGIN {
code
}
Declaration code is called before the program runs.
Example
!/usr/bin/ruby
puts
"
This is the main Ruby program
"
BEGIN
{
puts
"
initialize the Ruby program
"
}
This will produce the following result:
初始化 Ruby 程序
这是主 Ruby 程序
Ruby END Statement
Syntax
END {
code
}
Declaration code is called at the end of the program.
Example
!/usr/bin/ruby
puts
"
This is the main Ruby program
"
END
{
puts
"
terminate the Ruby program
"
}
BEGIN
{
puts
"
initialize the Ruby program
"
}
This will produce the following result:
初始化 Ruby 程序
这是主 Ruby 程序
停止 Ruby 程序
Ruby Comments
Comments hide a line, part of a line, or several lines from the Ruby interpreter. You can use the character ( # ) at the beginning of a line:
I am a comment, please ignore me.
Alternatively, a comment can follow a statement or expression on the same line:
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 kind of block comment hides the lines between =begin/=end from the interpreter:
=begin
This is a comment. This is also a comment. This is also a comment. This is still a comment.
=end