Latest web development tutorials

Ruby syntax

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

Examples

#! / Usr / bin / ruby ​​-w

puts "Hello, Ruby!";

Running instance »

Here, under the assumption that your / usr / bin directory has Ruby interpreter available. Now, try to run the program as follows:

$ Ruby test.rb

This will produce the following results:

Hello, Ruby!

You've already seen a simple Ruby program, now let's look at some basic concepts related to the Ruby syntax:

Ruby program blank

Blank character in Ruby code, such as spaces and tabs will generally be ignored, except when they appear in the string if not ignored. Sometimes, however, they are used to interpret ambiguous statements. When the -w option is enabled, this interpretation will generate a warning.

Example:

a + b is interpreted as a + b (which is a local variable)
a + b is interpreted as a (+ b) (which is a method call)

Ruby program in the end of the line

Ruby put a semicolon and a newline is interpreted as the end of the statement. However, if the end of the line Ruby met operators such as +, -, or backslash, they represent a continuation of a statement.

Ruby identifier

Identifiers are names of variables, constants, and methods. Ruby identifiers are case sensitive. This means that the Ram and RAM are two different identifiers in Ruby.

Ruby identifier name can contain letters, numbers, and the underscore character (_).

Reserved words

The following table lists the reserved words in Ruby. These words can not be retained as the name of a constant or variable. However, they can be used as the method name.

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__

Ruby The Here Document

"Here Document" refers to a multi-line strings. Until all the rows until the terminator << Afterwards, you can specify a string or identifier to terminate the string, and the current row after a string value.

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

The following are different instances:

#!/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"

This produces 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.

RubyBEGIN statement

grammar

BEGIN {
   code
}

Declarationcodewould be called before the program is run.

Examples

#!/usr/bin/ruby

puts "This is main Ruby Program"

BEGIN {
   puts "Initializing Ruby Program"
}

This produces the following results:

Initializing Ruby Program
This is main Ruby Program

RubyEND statement

grammar

END {
   code
}

Declarationcodewill be called at the end of the program.

Examples

#! / Usr / bin / ruby

puts "This is main Ruby Program"

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

This produces the following results:

Initializing Ruby Program
This is main Ruby Program
Terminating Ruby Program

Ruby Comment

Note Ruby interpreter would hide a row, or a part of a line or several lines. You can use the first line character (#):

# I am a comment, please ignore me.

Alternatively, you can comment on the same line of the statement or expression followed by:

name = "Madisetti" # this is a comment 

You can comment multiple lines, as follows:

# This is a comment.
# This is a comment.
# This is a comment.
# This is a comment.

Here is another form. This would explain the block comment Hide = begin / = end row between:

= Begin
This is a comment.
This is also a comment.
This is also a comment.
This is a comment.
= End