Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby syntax


May 12, 2021 Ruby


Table of contents


Ruby syntax

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

#!/usr/bin/ruby -w
puts "Hello, Ruby!";

Try it out . . .

Here, let's say that the Ruby interpreter is already available in your /usr/bin directory. Now try running the program as follows:

$ ruby test.rb

This will produce the following results:

Hello, Ruby!

Now that you've seen a simple Ruby program, let's look at some basic concepts related to Ruby syntax:

White space in Ruby's program

Blank characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in a string. H owever, sometimes they are used to interpret ambiguous statements. This interpretation produces a warning when the -w option is enabled.

Instance:

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

The end of the line in the Ruby program

Ruby interprets the sign and line break as the end of the statement. However, if Ruby encounters operators at the end of a line, such as a s, - or backslash, they represent a continuation of a statement.

Ruby identifier

The identifier is the name of the variable, constant, and method. R uby identifiers are case sensitive. This means that Ram and RAM are two different identifiers in Ruby.

The name of the Ruby identifier can contain letters, numbers, and underscore characters.

Keep the word

The following table lists the reserved words in Ruby. T hese reserved words cannot be used as the name of a constant or variable. 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 establishing a multi-line string. After that, you can specify a string or identifier to terminate the string, and all lines after the current line until the terminator are the value of the string.

If the terminator is enclosed in quotation marks, the type of quotation marks determines the type of string facing the line. Please note that there must be no space between the terminator and the terminator.

Here are the 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 results in the following:

    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

Grammar

BEGIN {
   code
}

Declaration code is called before the program runs.

Instance

#!/usr/bin/ruby

puts "This is main Ruby Program"

BEGIN {
   puts "Initializing Ruby Program"
}

This results in the following:

Initializing Ruby Program
This is main Ruby Program

Ruby END statement

Grammar

END {
   code
}

Declaration code is called at the end of the program.

Instance

#!/usr/bin/ruby

puts "This is main Ruby Program"

END {
   puts "Terminating Ruby Program"
}
BEGIN {
   puts "Initializing Ruby Program"
}

This results in the following:

Initializing Ruby Program
This is main Ruby Program
Terminating Ruby Program

Ruby comments

Comments hide a line, or part of, or several lines, from the Ruby interpreter. You can use the character at the top of the line.

# 我是注释,请忽略我。

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

name = "Madisetti" # 这也是注释

You can comment on more than one line, as follows:

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

Here's another form. This block comment hides the line between the s begin/end of the interpreter:

=begin
这是注释。
这也是注释。
这也是注释。
这还是注释。
=end