Ruby syntax


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

Instance

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

Run Instance»

Click the "Run Instance" button to view the online instance

Here, it is assumed that there is already a Ruby interpreter available in your /usr/bin directory. Now, try running this program as shown below:

$ ruby test.rb

This will produce the following result:

Hello, Ruby!

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

Whitespace in Ruby programs

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

Example:

a + b 被解释为 a+b (这是一个局部变量)
a  +b 被解释为 a(+b) (这是一个方法调用)

End of lines in Ruby programs

Ruby interprets semicolons and newlines as the end of statements. However, if Ruby encounters operators such as +, -, or backslash at the end of a line, 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 that Ram and RAM are two different identifiers in Ruby.

Ruby identifier names can contain letters, numbers, 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.

##breakfalserescue whencaseforretrywhileclassifreturnwhile##def##defined?modulesuper__LINE__

Here Document

"Here Document" in Ruby 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 values ​​of the string.

If the terminator is enclosed in quotes, the type of quotes determines the line-oriented string type. Please note that there must be no space between << and the terminator.

Here are different examples:

#!/usr/bin/ruby -w
# -*- coding : utf-8 -*-

print <<EOF
    这是第一种方式创建here document 。
    多行字符串。
EOF

print <<"EOF";                # 与上面相同
    这是第二种方式创建here document 。
    多行字符串。
EOF

print <<`EOC`                 # 执行命令
	echo hi there
	echo lo there
EOC

print <<"foo", <<"bar"	      # 您可以把它们进行堆叠
	I said foo.
foo
	I said bar.
bar

Try it out»

This will produce the following results:

    This is the first 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

Syntax

BEGIN {
   code
}

Statementcode will be called before the program is run.

Example

#!/usr/bin/ruby

puts "This is main Ruby Program"

BEGIN {
   puts "Initializing Ruby Program"
}

This will produce the following results:

Initializing Ruby Program
This is main Ruby Program

Ruby END Statement

Syntax

END {
   code
}

The statement code will be called at the end of the program.

Example

#!/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
This is main Ruby Program
Terminating Ruby Program

Ruby Comments

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

# 我是注释,请忽略我。

Alternatively, a comment can follow a statement or expression on the same line:

name = "Madisetti" # 这也是注释

You can comment multiple lines, as follows:

# 这是注释。
# 这也是注释。
# 这也是注释。
# 这还是注释。

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

=begin
这是注释。
这也是注释。
这也是注释。
这还是注释。
=end
BEGINdonextthen
END elseniltrue
aliaselsifnotundef
andendorunless
beginensureredountil
inself__FILE__