A summary of some basic syntax knowledge points in Ruby.

Source: Internet
Author: User

A summary of some basic syntax knowledge points in Ruby.

Let's write a simple ruby program. All Ruby source files will use the extension. rb. Therefore, put the following source code in a test. rb file.

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

Here, if you have installed the Ruby interpreter, you can find it in the/usr/bin directory. Now try to run this program as follows:

$ ruby test.rb

This produces the following results:

Hello, Ruby!

Through the above example, we have seen a simple Ruby program. Now let's look at several basic concepts about Ruby Syntax:
Blank characters in Ruby program:

White spaces, such as spaces and tabs, are generally ignored in Ruby code unless they appear in strings. However, sometimes they are used to explain Ambiguous reports. This type generates a warning when the-w option is enabled.

Instance:

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

End of Ruby program line:

Ruby interprets a statement to end with a semicolon and line break. However, if Ruby encounters an operator such as +,-, or a line ending with a backslash, the statement continues.
Ruby identifier:

Identifiers are variables, constants, and methods. Ruby identifiers are case sensitive. Ram and RAM are two identifiers with different meanings in Ruby.

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

Heredoc IN Ruby:

"Here Document" is to create a multi-line string. After <, you can specify a string or an identifier to terminate the string literal. The value of the terminator string for all rows following the current row.

If the Terminator is a reference, the quote type determines the type of the String constant for the row. Note <The Terminator cannot contain spaces.

The following are different examples:

#!/usr/bin/ruby -wprint <<EOF  This is the first way of creating  here document ie. multiple line string.EOFprint <<"EOF";        # same as above  This is the second way of creating  here document ie. multiple line string.EOFprint <<`EOC`         # execute commands echo hi there echo lo thereEOCprint <<"foo", <<"bar" # you can stack them I said foo.foo I said bar.bar

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 therelo there    I said foo.    I said bar.

Ruby in statement
Syntax:

BEGIN {  code}

The declared code is called before the program runs.
Example:

#!/usr/bin/rubyputs "This is main Ruby Program"BEGIN {  puts "Initializing Ruby Program"}

This produces the following results:

Initializing Ruby ProgramThis is main Ruby Program

Ruby END statement
Syntax:

END {  code}

The declared code is called the end of the program.
Syntax:

#!/usr/bin/rubyputs "This is main Ruby Program"END {  puts "Terminating Ruby Program"}BEGIN {  puts "Initializing Ruby Program"}

This produces the following results:

Initializing Ruby ProgramThis is main Ruby ProgramTerminating Ruby Program

Ruby notes:

Annotate and hide a line. Some or several lines of the line of the Ruby interpreter ignore the interpreter code. A line starting with a hash character (#) that can be used:

# I am a comment. Just ignore me.

Alternatively, the comment may be a statement or expression after the same row:

Name = "Madisetti" # This is again comment

You can comment out multiple rows 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 of comment hides several lines of comment: = begin/= end:

=beginThis is a comment.This is a comment, too.This is a comment, too.I said that already.=end


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.