The Ruby Programming Language:Everything you need to know
David Flanagan & YukihiroMatsumoto
O’Reilly 2008, Publishing Houseof Electronics Industry 2008
1. structure &execution
1.1 lexical structure词法结构
1.1.1 comment
1.1.1.1 单行注释:#
1.1.1.2 embeded document: =begin =end
1.1.1.3 文档化注释:= == ===
1.1.2 literal
1.1.3 punctuation
1.1.4 identifier:alpha, num, _
1.1.4.1 case: sensitive
1.1.4.2 charset: Unicodeet al
1.1.4.3 punctuation
$ global variable
@instance variable
? method thatreturns bool
! change object thatcall it
= assignment
1.1.5 keywords
1.1.5.1 parser
__LINE__, __ENCODING__, __FILE__
BEGIN, END
alias, defined?, ensure, next, redo, rescue, retry,
and, not, or,
begin/end, def/undef,yield
return, break, case, do/until/while,when,if/else/elseif/then/unless, for/in
module, class,self, super, false/true, nil,
1.1.5.2 others
=begin, =end, __END__
1.1.5.3 Kernal, Module, Class, Object
1.1.6 blank
1.2 Syntactic structure句法结构: block& body
1.3 file structure
#!/usr/bin/ruby –w shebang comment
# -*- coding: utf-8 -*- coding comment
require ‘socket’ load library
…
__END__ end of code
… program data goes here
1.4 program encoding
1.5 program execution: BEGIN block -> first line
2. datatypes & objects
2.1 number: Numeric:Integer(Fixnum, Bignum), Float, Complex, BigDecimal, Rational
2.1.1 integer literal
_: thousands separator
0:octal, 0b/0B:binary, 0x/0X: hexa
2.1.2 floating-point literal
2.2 text: String
2.2.1 string literal
‘’
“”
%,%q,%Q
``,%x
here document: <<, <<-label
2.2.2 character literal:?+char
2.2.3 string operator
+
<<
*
<,>,<=,>=
casecmp,downcase,upcase
2.2.4 accessingcharacter & substring
[], length, size
Range
RegExp
2.2.5 iterating
each_byte, each_char, each_line,Ruby 1.9
2.2.6 string encoding& multibyte character
2.2.6.1 size: length,size, bytesize
2.2.6.2 encoding: encoding, force_encoding, encode, encode!, Encoding.compatible?,valid_encoding?
2.2.6.3 Encoding类: name, to_s, inspect, find, list, default_external, locale_charmap
2.3 array
2.3.1 size: length,size
2.3.2 literal: %w, %W
2.3.3 access: [], +, -,<<, *, |, &
2.4 hash
2.4.1 literal: {“one”=> 1}, {:one => 1}, {one: 1}
2.4.2 eql?, hash, rehash
2.5 range
2.5.1 <=>, include?,member?, cover?
2.6 symbol
2.6.1 %s, intern, to_sym
2.6.2 to string: to_s, id2name
2.7 true, false ,nil:nil?
2.8 object
2.8.1 reference
2.8.2 lifetime: new,initialize
2.8.3 identity: object_id, __id__
2.8.4 type: class, superclass, instance_of?, is_a?/kind_of?, ===, respond_to?
2.8.5 equality: equal?, ==, eql?, ===, =~
2.8.6 order: <=>, between?
2.8.7 convertion:
2.8.7.1 显式: to_s, to_i, to_f,to_a, inspect
2.8.7.2 隐式: to_str, to_int, to_hash, to_ary
2.8.7.3 try_convert
2.8.7.4 coerce
2.8.8 copy: clone, dup,initialize_copy
2.8.9 marshal编组: Marshal.dump保存对象状态,Marshal.load恢复
2.8.10 freeze: freeze, frozen?
2.8.11 taint污染: taint, tainted?, untaint, $SAFE
3. expression &operator
3.1 literal & keyword literal
3.2 variable reference
3.3 constant reference
3.4 method invocation
3.5 assignment
3.6 operator
operator | arity | associativity | modified? | operation |
! ~ + | 1 | R | Y |
|
** | 2 | R | Y |
|
- | 1 | R | Y |
|
* / % | 2 | L | Y |
|
+ - | 2 | L | Y |
|
<< >> | 2 | L | Y |
|
& | 2 | L | Y |
|
| ^ | 2 | L | Y |
|
< <= > >= | 2 | L | Y |
|
== === != =~ !~ ó | 2 | N | Y |
|
&& | 2 | L | N |
|
|| | 2 | L | N |
|
.. … | 2 | N | N |
|
?: | 2 | R | N |
|
rescue | 2 | L | N |
|
= **= *= /= %= += -= <<= >>= &&= &= ||= |= ^= | 2 | R | N |
|
defined? | 1 | N | N |
|
not | 1 | R | N |
|
and or | 2 | L | N |
|
if unless while until | 2 | N | N |
|
4. statement &control structure
4.1 conditional
4.1.1 if/else/elsif
4.1.2 if modifier
4.1.3 unless &unless modifier
4.1.4 case/when/else
4.1.5 ?:
4.2 loop
4.2.1 while & until& modifier
4.2.2 for/in
4.3 iterator & enumerable object
4.3.1 numeric iterator:Integer.upto, downto, times
4.3.2 enumerable object
Array, Hash, Range.each
Enumerable: each_with_index,collect, select, reject, inject
4.3.3 custom: yield
4.3.4 enumerator: to_enum, enum_for, with_index
4.3.5 externaliterator: next, rewind
4.3.6 concurrent: 创建copy
4.4 block
4.5 alter control flow: return, break, next, redo, retry, throw/catch
4.6 exception: raise, rescue, ensure
4.7 thread & fiber & continution线程,纤程,连续体: Thread, Fiber, Continuation/calcc
5. method & Proc & lambda & closure
5.1 definition: def/undef,undef_method
5.2 method name:
5.3 method & parentheses
5.4 method argument
5.4.1 parameterdefault: =
5.4.2 variable-length:*
5.4.3 mapping argument toparameter
5.4.4 hash for namedargument
5.4.5 block argument:&
5.5 Pro & Lambda
5.5.1 creating proc
5.5.1.1 method invoke:&
5.5.1.2 Proc.new
5.5.1.3 Kernel.lambda
5.5.1.4 Kernel.proc: =Proc.newRuby 1.9
5.5.1.5 lambda literal:->(parameter list; local variables){code}, () is optional
5.5.2 invoke: Proc.call,[], .()
5.5.3 arity: Proc.arity
5.5.4 equality: ==,clone/dup时相等
5.6 closure: Proc.binding
5.7 method object: Object.method, Method.call, Method.arity, Method.to_proc, Method.name, owner, receiver
5.7.1 unbound methodobject: UnboundMethod.bind, unbind, name, owner
5.8 functional programming
6. class &module
6.1 definition
6.1.1 create: classPoint end
6.1.2 instantiate: Point.new
6.1.3 initialize:initialize
6.1.4 to_s
6.1.5 accessor & attribute: attr, attr_reader, attr_accessor
6.1.6 operator: +, [],==
6.1.7 mutable: add!
6.1.8 class method: sef.
6.1.9 constant
6.1.10 class variable:@@
6.1.11 instancevariable: @
6.2 method visibility: pubic, protected, private; public_send,send, instance_eval
6.3 subclass & inheritance: <
6.3.1 inheriting method
6.3.2 overriding method
6.4 creation & initialization
6.4.1 new, allocate, initialize
6.4.2 factory method
6.4.3 dup, clone, initialize_copy
6.4.4 marshal_dump, marshal_load
6.4.5 singleton
6.5 module
6.5.1 namespace
6.5.2 mixin: include
6.5.3 includablenamespace module: module_function
6.6 load & require module
6.6.1 load path:$LOAD_PATH, $:
6.6.2 execute loadedcode
6.6.3 autoload module: autoload
6.7 singleton method & Eigenclass:<<
6.8 method lookup
6.9 constant lookup
7. reflecton & metaprogramming
7.1 type & class& module: .class, .superclass, .instance_of?, .is_a?, .kind_of?, ===, .respond_to?
7.1.1 ancestry &module: ancestors, include?, included_modules, Object.extend, Module.nesting
7.1.2 define class& module: Class, Module
7.2 evaluating string & block: Kernel.eval
7.2.1 Bindings & eval: Kernel.binding
7.2.2 instance_eval & class_eval
7.2.3 instance_exec & class_exec
7.3 variable & constant
7.4 method
7.4.1 test & list: methods, public_methods, protected_methods,private_methods, singleton_methods;instance_methods, public_instance_methods,protected_instance_methods, private_instance_methods;method_defined?, public_method_defined?,protected_method_defined?, private_method_defined?
7.4.2 obtain method:method, instance_method
7.4.3 invoke: method,call, send, public_method
7.4.4 define, undefined,alias: define_method, define_singleton_method,alias, alias_method; remove_method,undef_method
7.4.5 handle undefinedmethod: method_missing
7.4.6 visibility: class_eval, private_class_method,public_class_method
7.5 hook: inherited, included, extended, method_added,singleton_method_added, method_moved,method_undefined, singleton_method_moved,singleton_method_undefined
7.6 tracing: __FILE__, __LINE__, Exception.backtrace,Kernel.caller
7.7 ObjectSpace &GC: ObjectSpace.each_object, _id2ref, define_finalizer, undefined_finalizer,garbarge_collect, GC.start,disable, enable
7.8 custom control structure
7.8.1 delay &repeat: after, every
7.8.2 thread safety: Mutex.sychronize
7.9 missing method & costant: const_missing, method_missing
7.10 dynamicallycreating method: attr_reader, attr_accessor,class_eval, define_method
7.11 alias chaining:alias, def; alias_method, define_method, class_eval
7.12 domain-specific languange
8. ruby platform
8.1
9.