Terry July Ruby Reading Notes (more detailed) page 1/4

Source: Internet
Author: User

Page 3Overview

Description and execution: Python and Ruby are highly interactive;

○ Compilation and execution: Pascal, C, fast.

N local execution, such as C, C ++;

N virtual machine execution, such as Java, C #.

 

○ Dynamic languages, such as JavaScript and Ruby;

○ Static languages, such as C ++ and Java.

 

Language

○ Syntax

Keywords

Description

○ Pragmatic

 

Matz 1993

Ruby ['ru: bi] (Ruby)

 

 

Ruby draws on perl regular expressions, python's simplicity and readability, smalltalk's pure object-oriented syntax and single inheritance, LISP's infinitely nested syntax, Java's thread...

 

Page 14Getting started

XP

Cmd

C: \ Documents ents and Settings \ Administrator> ruby-v

Ruby 1.8.6 (patchlevel 111) [i386-mswin32]

 

Method 1:RubyCommand

C: \ Documents ents and Settings \ Administrator> ruby-e 'print "hello world "'

Hello world

 

Ruby runs ruby;

-E: Execute the subsequent ruby script program;

Print display;

Hello, world.

 

Method 2:IrbInteractive execution

C: \ Documents ents and Settings \ Administrator> irb

Irb (main): 001: 0> print "hello world"

Hello world => nil

Irb (main): 002: 0> exit

 

Exit: exit the irb interactive environment.

 

Method 3: CreateRubyScript

C: \ Documents ents and Settings \ Administrator> copy con helloworld. rb

Print "hello world"

Print "3*7 =", 3*7

^ Z

Copied 1Files.

 

C: \ Documents ents and Settings \ Administrator> helloworld. rb

Hello world3 * 7 = 21

 

Page 17IDE

○ Eclipse + RDT (Ruby Development Tools)

○ FreeRIDE + SciTE

1. Edit Ruby code in SciTE;

2. Save. RbFile;

3. In the menu bar, choose tools> RUN or F5;

Page 18Syntax

1.Note

Single line comment # multi-line comment = begin = end

Ruby embedded document (Rdoc) annotation, using the ri command to generate documents from the source file.

Rdoc is embedded in ruby code and can be converted to html documents.

Ri command to view the function description and class description. Function Description and class description should be placed in = begin and = end. Note: "= begin" must be written at the beginning of the line. That is to say, the first six characters in this line are "= begin". spaces are not allowed before.

2.Branch

The statement ends with a semicolon.

Multiple statements in one row are separated by semicolons. The last statement can be omitted. A line break indicates that a row ends.

The statement is too long. You can use the '\' symbol at the end of the line to represent the continued row.

3.Delimiter

Name

Symbol

Purpose

Semicolon

;

Used to separate multiple statements in a row

Parentheses

()

Raise the priority; include the parameter list when defining a method

Space

 

Delimiter; In the case where () can be omitted, replace ()

Comma

,

Separate multiple parameters

Point

.

Separate an object from its Method

Two colons

::

The domain identifier that separates a module (class) from its constants.

4.Keywords

The keywords in Ruby are as follows:

Module definition:

Module

Exception Handling:

Rescue, ensure

Class Definition:

Class

Object Reference:

Super, self

Method definition:

Def, undef

Block start:

Begin/end

Check type:

Defined?

Embedded Module:

BEGIN, END

Logical value and null value:

True, false, nil

File-related:

_ FILE __,__ LINE __

Logical judgment:

Not, and, or

Method return:

Return

Condition Statement:

If, then, else, elsif, case, when, unless

Alias:

Alias

Loop statement:

For, in, while, until, next, break, do, redo, retry, yield

The BEGIN module is equivalent to a macro in the C language, and the END module is used for some final work. With

Require, include, should cancel the syntax definition of BEGIN and END.

5.Operator

6.Identification name:

Constants, variables, methods, classes, and modules;

Case Sensitive;

The first character indicates the usage of the name.

Local variables, method parameters, and method names

It must start with a lowercase letter or underscore;

Class Name, Module name, and constant

It must start with an uppercase letter.

Global Variables

Prefix with the dollar sign $;

Instance variables

Starting;

Class variable

Start;

The first letter can be followed by any combination of letters, numbers, and underscores;

@ Cannot be followed by numbers directly.

Ruby program code is represented by a 7-bit acⅱ code, which supports eight-bit coding systems such as EUC, SJIS, and UTF-8 through language extensions. Ruby2.0 supports 16-bit Unicode encoding.

7.Class Library

You can directly use:

I) KEYWORDS;

Ii) classes and methods in the require or include class libraries;

Iii) methods inherited from the parent class.

 

Puts

Automatic line feed after each parameter

Print

Do not automatically wrap, use "\ n" for line feed"

Printf

Output by format, not automatically wrap

Gets

Read data from the keyboard or file

 

C: \ Documents ents and Settings \ Administrator> ruby-e 'puts "hello", "world "'

Hello

World

 

C: \ Documents ents and Settings \ Administrator> ruby-e 'printf "number: % f", 100'

No.: 3.200000

 

 

Note: printf parameters are separated by commas.

C: \ Documents ents and Settings \ Administrator> ruby-e 'printf "number: % 4.3f, string % s

", 1.5," hello world "'

Number: 1.500, string hello world

 

8.Data Type

Type

Category

Description and example

Number

Integer

0 octal,

0x hexadecimal, 0b binary

Floating Point Type

String

Single quotes''

 

Double quotation mark ""

 

Array

Starting from 0, each element can be of different types

Example [2.4, "thank you", [a, B, c], 78]

Interval

1. 5

1, 2, 3, 4, 5

1... 5

1, 2, 3, 4

Regular Expression

 

 

9.Value assignment

Values of the exchange variables a and B:

A, B = B,;

Note:

A = (B = 1 + 2) + 3 # a = 6, B = 3

Comma (,), from left to right, assigned to a, B, and c respectively.

X = 0 # x = 0

A, B, c = x, (x + 1), (x + 2) # a = 0, B = 1, c = 2

10.Conditional operation

=

Equal

! =

Not equal

Eql?

Compare the values and types of the two objects.

Equal?

Compare whether two objects have the same address in the memory

<

Less

>

Greater

<=

Less than or equal

> =

Greater than or equal

= ~

Match Regular Expression

!~

Assertion does not match the Regular Expression

===

The object on the right is within the left range.

Puts (0 .. 9) ===3.14 # true puts ('A'... 'F') ==='c' # true

<=>

Compare the sizes of two objects, greater than 1, equal to 0, less than-1.

In Ruby, nil and false are false, while others are true.

 

11.Judgment statement

  1. Single line if statement

If then end

() If

  1. Multi-line if statement

If elsif else end

  1. Unless Condition Statement

 

Unless

 

If not

 

  1. Case branch Condition Statement

 

Case

When

When

Else

End

 

12.Loop statement

Single Row while

(Statement 1; Statement 2; statement ...) While Condition

Multiline while

While Condition

Statement

End

Until

Until jump out of the loop Condition Statement

Until condition = while not (condition)

For... In Loop

For variable in object

Statement 1; Statement 2; statement...

End

 

Break, next & redo & retry

In the circular body, if you encounter:

Break

Jump out of the current layer loop;

Next

Ignore the remaining part of this loop and start the next loop;

Redo

Start the loop again, or start from this time;

Retry

Start the loop body.

 

Times, upto, downto, each, step

Statement

Running result

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

 

C: \ Documents ents and Settings \ Administrator> irb

Irb (main): 001: 0> 1. upto (9) {| I | print I if I <7}

123456 => 1

Irb (main): 002: 0> 9. downto (1) {| I | print I if I <7}

654321 => 9

Irb (main): 003: 0> (1 .. 9). each {| I | print I if I <7}

123456 => 1 .. 9

Irb (main): 004: 0> 0. step (11,3) {| I | print I}

0369 => 0

 

13. Exception

Java

Try... Catch... Finally... Throw

Ruby

Begin/end... Rescue... Ensure... Raise

Retry can be used in rescue. You can only use rescue or ensure. when both are used, rescue must be before ensure.

14. threads

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.