Ruby basic syntax getting started tutorial, ruby basic syntax tutorial

Source: Internet
Author: User

Ruby basic grammar introductory learning tutorial, ruby basic grammar tutorial

Let's write a simple Ruby program. All Ruby file extensions are .rb. So, put the following source code in the test.rb file.
Examples
#! / usr / bin / ruby -w

puts "Hello, Ruby!";

Here, it is assumed that the Ruby interpreter is already available in your / usr / bin directory. Now, try to run 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:
White space in Ruby program
White space characters in Ruby code, such as spaces and tabs, are generally ignored unless they appear in the string. However, sometimes they are used to explain ambiguous sentences. When the -w option is enabled, this interpretation produces a warning.
Examples:

a + b is interpreted as a + b (this is a local variable)
a + b is interpreted as a (+ b) (this is a method call)
End of line in Ruby program
Ruby interprets semicolons and newlines as the end of statements. However, if Ruby encounters operators at the end of the line, such as +,-or backslashes, they represent a continuation of a statement.
Ruby identifier
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 underscore characters (_).
Reserved word
The following table lists the reserved words in Ruby. These reserved words cannot be used as constant or variable names. However, they can be used as method names.

Here Document in Ruby
"Here Document" refers to the creation of multi-line character strings. After <<, 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 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
  This is the first way to create a document here.
  Multiline string.
EOF

print << "EOF"; # Same as above
  This is the second way to create here document.
  Multiline string.
EOF

print << `EOC` # execute command
 echo hi there
 echo lo there
EOC

print << "foo", << "bar" # You can stack them
 I said foo.
foo
 I said bar.
bar

This will produce the following result:
  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.

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.