Introduction to Ruby Grammar learning

Source: Internet
Author: User
Tags assert constructor extend lowercase regular expression require

1, with java,c# and other languages of the Try...catch...finally...throw, Ruby Begin/end ... rescue...ensure ... raise to handle the exception, retry can be used in rescue. You can use either rescue or ensure, when both are used, rescue must be in front of ensure.
2, $arr <<num means that num is added to the $arr as an element of the array.
3, the first letter of the class name should be capitalized, such as student, the method name and parameter name should start with a lowercase letter or begin with an underscore, such as Talk,is_prime.
4, @age. to_s means to convert a number of @age to a string.
5,initialize is the initialization method, which is equivalent to the constructor. Attr_writer and Attr_reader are abbreviated forms of getter and setter.  Attr_accessor:a is equivalent to attr_reader:a; Attr_writer:a.
6,class Student < person refers to the student class inheriting the person class. When a subclass inherits a parent class, you can add new methods, or a method that enhances the parent class (specified in keyword Super), in addition to overriding the method.
The 7,ruby language is only rewritten and has no full sense of overloading.
In the 8,ruby language, a variable is given a value of a data type, and in a program you can give the variable a value of another data type at any time.
9,ruby is a dynamic language, and you can change the structure and function of ruby programs in a Ruby program. Methods, attributes can be added or removed, new classes or objects can be created, and new modules can appear.
10, the method default is public (except initialize, it is always private).
11, define the module with Module...end. Modules are very similar to classes, but:
A the module may not have instance objects.
b) Modules may not have subclasses.
Call a module method, as in defining the module method, with the module name and a dot number "." Before the method name. The modular approach provides a way to access module internal methods outside the module without having to include modules.
The 12,include method contains a module for all objects of a class, and the Extend method contains a module for an object of a class
13,require contains files that are loaded only once and are automatically ignored when the same file is encountered, and files with the same name under different paths are loaded multiple times. Load contains files, loaded multiple times, even the same path as a file.
14,print Arr.join (","), "n" means to convert the array arr to string output, separating each element with "," and wrapping the line. An array can be converted to a string by a join method, and the argument within the join () is also a string that separates each element of the array, for example: Arr.join (",").
15,%q is used to generate a single quote string;%q is used to generate a double quote string. %q or%q followed by the separator, can be paired!!;//; < >; (); []; {}; and so on.
16,ruby, you can use the constructor to explicitly create a regular expression, or you can create a regular expression by using the literal/regular mode/.
Match a regular expression, with "=~", not "= =". "=~" is used to compare whether a regular expression is met, where the return pattern is matched in the string, or return nil.
does not match a regular expression, with "! ~ ", no use!" = ". “! ~ "To assert that it does not conform to a regular expression, return true,flase.

The redundancy, imperfection and dynamics of Ruby language is the true portrayal of the real world.

1, with java,c# and other languages of the Try...catch...finally...throw, Ruby Begin/end ... rescue...ensure ... raise to handle the exception, retry can be used in rescue. You can use either rescue or ensure, when both are used, rescue must be in front of ensure.
2, $arr <<num means that num is added to the $arr as an element of the array.
3, the first letter of the class name should be capitalized, such as student, the method name and parameter name should start with a lowercase letter or begin with an underscore, such as Talk,is_prime.
4, @age. to_s means to convert a number of @age to a string.
5,initialize is the initialization method, which is equivalent to the constructor. Attr_writer and Attr_reader are abbreviated forms of getter and setter.  Attr_accessor:a is equivalent to attr_reader:a; Attr_writer:a.
6,class Student < person refers to the student class inheriting the person class. When a subclass inherits a parent class, you can add new methods, or a method that enhances the parent class (specified in keyword Super), in addition to overriding the method.
The 7,ruby language is only rewritten and has no full sense of overloading.
In the 8,ruby language, a variable is given a value of a data type, and in a program you can give the variable a value of another data type at any time.
9,ruby is a dynamic language, and you can change the structure and function of ruby programs in a Ruby program. Methods, attributes can be added or removed, new classes or objects can be created, and new modules can appear.
10, the method default is public (except initialize, it is always private).
11, define the module with Module...end. Modules are very similar to classes, but:
A the module may not have instance objects.
b) Modules may not have subclasses.
Call a module method, as in defining the module method, with the module name and a dot number "." Before the method name. The modular approach provides a way to access module internal methods outside the module without having to include modules.
The 12,include method contains a module for all objects of a class, and the Extend method contains a module for an object of a class
13,require contains files that are loaded only once and are automatically ignored when the same file is encountered, and files with the same name under different paths are loaded multiple times. Load contains files, loaded multiple times, even the same path as a file.
14,print Arr.join (","), "n" means to convert the array arr to string output, separating each element with "," and wrapping the line. An array can be converted to a string by a join method, and the argument within the join () is also a string that separates each element of the array, for example: Arr.join (",").
15,%q is used to generate a single quote string;%q is used to generate a double quote string. %q or%q followed by the separator, can be paired!!;//; < >; (); []; {}; and so on.
16,ruby, you can use the constructor to explicitly create a regular expression, or you can create a regular expression by using the literal/regular mode/.
Match a regular expression, with "=~", not "= =". "=~" is used to compare whether a regular expression is met, where the return pattern is matched in the string, or return nil.
does not match a regular expression, with "! ~ ", no use!" = ". “! ~ "To assert that it does not conform to a regular expression, return true,flase.

The redundancy, imperfection and dynamics of Ruby language is the true portrayal of the real world.

#ruby_pratice_注释. RB from # start to line end Single-line Comment

Puts 3/5

=begin
Puts 3/5
Multi-line annotations can be used with =begin and =end;
This is also Ruby's inline document RDoc annotation, similar to Javadoc
You can use command RI to produce documents from source files
=end

2, identification name and variable

Ruby's identity name is case-sensitive. keyword, which cannot be used as a constant, variable, method, class, or module name.
Ruby uses a convention to help it distinguish the use of a name: the first character in front of the name indicates the use of the name. Local variables, method parameters, and method names should start with a lowercase letter or begin with an underscore; global variables are prefixed with dollar characters, while instance variables begin with @; class variables begin with @@ 开头 class names, module names, and constants should begin with uppercase letters.

3, data type

Ruby data types have numbers, strings, arrays, hash tables, intervals, regular expressions.

The numeric type has an integer type, a floating-point type. Where floating-point data must be digitally added after the decimal point.

The string type is between the ' or '.

The subscript of an array is also starting at 0, but the different elements of a ruby array can be of different types. such as: [1,2.2, "Hello", [1,2,3]].

Range: 1. 4 means 1,2,3,4;1 ... 4 indicates 1,2,3.

4,ruby The basic assignment is done with "=".

such as a=1,b=1+1 result is: a=1,b=2

= = Compare two values are equal, return true,false such as: a=1; b=1.0; A==b #true

Eql? Compares the values of two objects, whether the types are equal, and returns true,false such as: a=1; b=1.0; A.eql? (b) #flase (A is an integral type, B is a floating-point type)

Equal Compares two objects in an in-memory address for the same, returning true,false such as: a=1.0; b=1.0; A.equal? (b) #flase a=1.0; B=a; A.equal? (b) # True

<=> Compare size of two objects, greater than, equal to, less than, return 1,0,-1

= = = Whether the object on the right is within the left range, returns True, Flase

=~ (Match) is used to compare whether a regular expression is met, where the return pattern is matched in the string, or return nil

!~ (mismatched) assertion does not conform to a regular expression, returns True, Flase

<= < > >= is less than or equal to or greater than or equal to

5, Cycle

While,for,case,break is equivalent to C, C #, Java much the same, the following are some of the more special: times, upto, Downto, each, step
3.times {print "hi!"} #hi!hi!hi!
1.upto (9) {|i| print i if i<7} #123456
9.downto (1) {|i| print i if i<7} #654321
(1..9). Each {|i| print i if i<7} #123456
0.step (11,3) {|i| Print i} #0369

6, Exceptions and threads

In contrast to the Try...catch...finally...throw in C # and Java, Ruby uses begin/end ... rescue...ensure ... raise to handle exceptions, retry can be used in rescue. You can use either rescue or ensure, when both are used, rescue must be in front of ensure.

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.