Ruby study Note 1

Source: Internet
Author: User
Document directory
  • Three methods for running Ruby:
  • Comments and branches
  • Keywords
  • Identifier
  • Data Type
  • Operator
  • Basic statement
  • Exception

 

Three methods for running Ruby:

Ruby-e 'Ruby statement'

Ruby source file

Irbirb is a ruby interactive environment

Comments and branches


= Begin and = end indicate multi-line comments, which are the same as/* and */In C ++. = there must be no spaces in the middle of begin and must start with a row.

In Ruby, ';' indicates the end of a statement. Multiple rows of statements can exist in one row. line feed also indicates the end of the statement, which can be omitted at this time. If the statement is too long, '/' can be used to connect to the downstream.

Keywords

Module definition: Module

Class Definition: Class

Method definition: def, UNDEF

Check type: defined?

Condition Statement: If, then, else, elsif, case, when, unless

Loop statement: For, in, while, until, next, break, do, redo, retry, yield

Logical judgment: not, And, or

Logical value and null value: True, false, Nil

Exception Handling: Rescue, ensure

Object Reference: Super, self

Embedded Module: Begin, end

File-related: __file __, _ line __

Return Method: Return

Alias: alias

Identifier

Ruby uses conventions to differentiate name usage. The first character of a name indicates the name usage.

Local variables, method parameters, and method names must start with lowercase letters or underscores;

The global variable starts with $;

Instance variables start;

Class variables (equivalent to static data members in the C ++ class) start;

Class Name, Module name, and constant start with an uppercase letter.

Data Type

Ruby data types include numbers, strings, arrays, hash tables, differences, and regular expressions.

The string is enclosed by ''or "".

[] Indicates an array. The subscript starts from 0. The elements in the array can be of different types. For example, [1, 2, 'df',]

Difference 1. 5 indicates 1, 2, 3, 4, 5

  1. .. 5 indicates 1, 2, 3, 4
Operator

= Value assignment operation

= Equal comparison

! = Unequal comparison

Eql? Comparison value and type are equal

Equal? Compare whether object addresses are equal

<> Compare the object size. values greater than, equal to, and less than 1, 0, and-1 are returned respectively.

= Determines whether the object on the right is within the left range

~ Judge Regular Expression

!~ Judge against the regular expression

<= Less than or equal
<Less

> Greater

> = Greater than or equal

Basic statement
1.
If condition then Statement 1; Statement 2; statement... end

2.
(Statement 1; Statement 2; statement...) If condition

3.
If condition
Statement 1; Statement 2; statement...
Elsif Condition
Statement 1; Statement 2; statement...
End

4.
Unless condition = if not condition

5.
Case x
When 1 .. 2
Statement 1; statement...
When 4
Statement
Else
Statement
End

6.
While Condition
Statement
End

7.
(Statement...) while Condition

8.
Until a> = 10
Statement
End
(Until condition = while not condition)

9.
For variable in object
Statement...
End

(Objects can be arrays, differences, sets ...)

10.
Break jumps out of a layer Loop
Next ignores the remaining part of this loop and jumps to the next loop.
Redo starts from scratch.
Retry to start the loop body again
Exception

Try... catch... finally... the same throw function is represented as begin/end in ruby... rescue... ensure... raise, which can be used in rescue

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.