Учебник языка Ruby

download Учебник языка Ruby

If you can't read please download the document

  • date post

    15-Aug-2014
  • Category

    Documents

  • view

    131
  • download

    19

Embed Size (px)

description

http://ru.wikibooks.org/wiki/Ruby

Transcript of Учебник языка Ruby

Ruby . , , . , . . , , , , . , , , . . , , , , . 1993 (24 ) ( ), , , . Ruby. : , . . Ruby . -, : -, .

1 Ruby 2

2.1 2.2

2.2.1 fxri:

2.2.2

2.3 2.4 2.5

2.5.1

3

3.1 3.2

3.2.1 3.2.2

3.3 3.4 3.5 3.6 3.7

4

4.1 4.2 4.3 4.4

4.4.1 4.4.2

4.5 4.6 4.7 4.8

5

5.1 5.2 5.3

5.4

5.4.1 5.4.2 / 5.4.3 5.4.4 5.4.5 / 5.4.6 ( ) 5.4.7 5.4.8 5.4.9 5.4.10 5.4.11 5.4.12 5.4.13 // 5.4.14 ,

5.5

5.5.1 ? 5.5.2 ?

5.6

5.6.1 5.6.2 5.6.3 // 5.6.4

5.7

5.7.1 ? 5.7.2 ?

5.8 5.9

5.9.1

5.9.1.1

5.9.2

5.9.2.1

6

6.1 ? 6.2

6.2.1 6.2.2 6.2.3 6.2.4 6.2.5

6.3

6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 /

6.4

6.4.1 ? 6.4.2 ? 6.4.3 ?

6.5 6.6 6.7

7

7.1

7.1.1 ?

7.2

7.2.1 7.2.2 7.2.3 7.2.4 7.2.5 - 7.2.6 ! 7.2.7 7.2.8

7.2.8.1 7.2.8.2 7.2.8.3 7.2.8.4 7.2.8.5 7.2.8.6

7.2.9 7.2.10 7.2.11 7.2.12

7.3

7.3.1 7.3.2

7.4

8

8.1

8.1.1 8.1.2 8.1.3 8.1.4 8.1.5 ""

8.2

8.2.1 8.2.2 8.2.3 8.2.4 8.2.5

8.3 ,

8.3.1 .size 8.3.2

8.4

8.4.1 //? 8.4.2 -

8.5 ? 8.6 ? 8.7 ? 8.8 ?

9

9.1 ? 9.2 2- ? 9.3 ?

9.3.1

9.4 9.5 ? 9.6 9.7

9.7.1

10

10.1 IO 10.2 10.3

10.4 10.5

11

11.1 ?

11.1.1 11.1.2

11.2 ?

11.2.1 11.2.2 ERB 11.2.3 ERB- 11.2.4 ERB- ERB-

11.3 , 11.4 ? 11.5 11.6 11.7 - 11.8 HTML-

11.8.1 11.8.2

11.9 You have got mail 11.10 -

11.10.1

12 13

13.1 13.2 13.3

Ruby :

()

:

.

- :

. Class , , , , . . - ( ) , ; , Unix (, ),

:

:

( .exe- Windows)

, . , . . / . Ruby , . , . Ruby, . TecToBa9_nporpamma.rb ( .rb), Unix-e :%

ruby TecToBa9_nporpamma.rb

Windows . STDIN.getc

Windows .rbw, Windows- rubyw, . ruby . rubyw STDIN.getc

. :puts " !"

npuBeT_mup.rb . : !

.

, . irb, . %

irb

. :% irb(main):001:0> % => [1, 2, 3, 4]

[1,2,3,4]

Ruby , . , , :% irb(main):002:0> "text".methods % => ["respond_to?", "%", "index", "select", ...]

:% irb(main):002:0> % => 70

14 * 5

exit. , () , Ruby ( , ). irb , , . fxri: Windows fxri. , ri irb. fx FXRuby. ri Ruby Informer ( ). fxri : c . . irb, .

, . .

, ( ). Ruby #. , . :puts 2+2 puts "!" # #

#->. :puts 2+2 puts "" #-> 4 #->

Ruby : print, printf, p, puts, .display . :

puts. . , , puts .to_s ; p. , - . , puts, . ( ) , .inspect.

:puts [1,2,3,4] p [1,2,3,4] puts "npuBeT!" p "npuBeT!" puts 5 p 5 #-> 1\n2\n3\n4 # \n - #-> [1,2,3,4] #-> npuBeT! #-> "npuBeT!" #-> 5 #-> 5

, . , p,

, puts.

, p "\323\367\350\362\345 \320\363\341\350!"

:$KCODE = "utf-8"

utf-8 SciTE. , ( )

, . Ruby :

; , .

:maccuB nepemeHHa9 gpyra9_nepemeHHa9 HenpaBuJIbHa9_nepemeHHa9 3JIemeHT _3JIemeHT ___

# # # #

. . ,

, , . , , UTF-8, -KU (=), :maccuB = [1,2,3,4] nepemeHHa9 = maccuB + [1,2] gpyra9_nepemeHHa9 = nepemeHHa9 - maccuB

, =, , . nepemeHHa9 [1, 2, 3, 4, 1, 2], maccuB + [1, 2]. (+) : .

-:nogpyra = "" k_HAM_B_rocTu_ugeT = nogpyra puts nogpyra #-> "", k_HAM_B_rocTu_ugeT[0] = "" # ( ) - puts nogpyra #-> "" # ,

, , , . () - . , , , , , ( ). nogpyra "", :nogpyra = "" k_HAM_B_rocTu_ugeT = nogpyra.clone k_HAM_B_rocTu_ugeT[0] = "" # . : puts nogpyra #-> ""

.dup. , . , ( ) :#... k_HAM_B_rocTu_ugeT = ""; # , p nogpyra #-> ""

Ruby . : Fixnum ( , 231), Bignum ( , 231), Float ( ), Array (), String (), Hash ( ). , , . pWrapPolygonVertices8;5;(-25,0);(-25,21554);(21600,21554);(21600,0);(-25,0)wzTooltip"

"posh2fBehindDocument1fIsButton1fLayoutInCell1

Ruby :5 # # # # # #

-12 4.5 076 0b010 0x89

.

|| "" or && "" and ! "" not ^ " " xor () . Ruby true ( ) false ( ). . .

== != < > = , , =< "--, "

- , . .maccuB[0] #=> " maccuB"

, . , , .

. Ruby . :

. . . ( + []). ( .inspect .to_s ). , ( .gsub, .match, .scan, .split).

. Ruby , . Perl 6 , ;

Ruby . .

" () ' ( ). :" " ' ' # #

, , , .

. : , => - . , .{ "" => " ", 807 => " !"}

, :"npeBeg" => "MegBeg"

:puts puts puts puts puts maccuB[""] #-> maccuB["807"] #-> nil maccuB[807] #-> ! maccuB[1] #-> nil maccuB["npeBeg"]#-> MegBeg

.

, ( Range). : , ( .. ...). ( ) , , .succ. :'a'..'z' 'a'...'z' 1..100 1...100

# , 'a'..'y' # , 1..99

, .succ ( ), , . : , , .

, .succ . , ( .succ)

, . , - . , Object.str = "A " str.class #-> String str.class.superclass #-> Object

. , , . , . . .

: ( Fixnum Bignum) ( Float). , , , .

Ruby , . . 231 ( ), , . , , , . Fixnum 231 ( ), Bignum. Bignum 231, Fixnum. ( + ). , ( ): 0 , 0x , 0b . , . , . , :# Fixnum 123_456 # -567 # 0xbad #

0377 # -0b101010 # 0b0101_0101 # # Bignum 123_456_789_123_456 -123_456_789_123_456 07777777777777777777 # # #

, (Fixnum) (Bignum) .

, . (). : 0.1234e2 1234e-2 12.34.# Float -12.34 # 0.12342 # 12.34 1234-2 # 12.34

, . mathn ( ).

, . Numeric (). , , . : Integer (), Float () Complex (). Rational (), . Integer : Fixnum ( ) Bignum ( ). , 231 , .

Fixnum Bignum 231 . , , Bignum Fixnum. , mathn. Complex (). Complex () ,

Integer (Fixnum Bignum), Float ( ). mathn, Rational ().

Rational 1, Integer (Fixnum Bignum).

Ruby , : (+), (-), (*), (/), (%), (**).6 6 6 6 6 6 + 4 - 4 * 4 / 4 % 4 ** 4 #-> #-> #-> #-> #-> #-> 10 2 24 1 2 1296

, ( ). , . :2 + 2 * 2 (2 + 2) * 2 #-> 6 #-> 8

, . :1/3 2/3 3/3 #-> 0 #-> 0 #-> 1

, , , . Ruby , . , . , , .

6.0 + 4 6 - 4.0 6.0 * 4.0 6.0 / 4 6.0 % 4 6 ** 4.0

#-> #-> #-> #-> #-> #->

10.0 2.0 24.0 1.5 ( , ) 2.0 1296.0

. Ruby. try ruby!. & | ^ > ~

. .6 & 4 6 | 4 6 ^ 4 6 > 4 ~4 #-> #-> #-> #-> #-> #-> 4 6 2 96 0 ( ) -5 ( )

, , . , . :ogHo_4ucJIo += gpyroe_4ucJIo

. :

ogHo_4ucJIo = ogHo_4ucJIo + gpyroe_4ucJIo

, + , .cTpoka = "" cTpoka += ", " cTpoka *= 3 cTpoka #-> ", , , " maccuB = [1,2,3] maccuB += [4,5] maccuB #-> [1,2,3,4,5]

+, += . , .

to_f to_i to_s to_a

Ruby to_. , (f Float , i Integer , s String , a Array ). :7.to_f 7.9.to_i 7.to_s 7.to_a #-> #-> #-> #-> 7.0 7 "7" [7]

. :rand(100) rand # 86 #-> 0.599794231588021

, rand 0 99 ( 1

100). , rand 0.0 1.0 . :

( 100), ( 0...N, N ); , 0.0 1.0.

. srand. ( ). , . : , .srand 123 Array.new(5){ rand(100) }

#-> [69, 71, 28, 42, 22]

, . 123 . ! sr