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

download
  • date post

    15-Aug-2014
  • Category

    Documents
  • view

    47
  • download

    19

description

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

transcript

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 . ! srand , .

: .ucxogHoe_4ucJIo = 1234 puts sprintf("%b",ucxogHoe_4ucJIo) # sprintf puts ucxogHoe_4ucJIo.to_s(2) # - " ", # 8 16, 5, 30...

:ucxogHoe_4ucJIo = 1234 puts ucxogHoe_4ucJIo.to_s.reverse

# reverse

N- :ucxogHoe_4ucJIo, N = 1234, 5 puts ucxogHoe_4ucJIo[ N ]

:

ucxogHoe_nepBoe, ucxogHoe_BTopoe = 134, 234 ucxogHoe_nepBoe, ucxogHoe_BTopoe = ucxogHoe_BTopoe, ucxogHoe_nepBoe puts ucxogHoe_nepBoe, ucxogHoe_BTopoe

:gpo6Hoe_4ucJIo = 3.1415926535 puts ( gpo6Hoe_4ucJIo * 100 ).to_i.to_f / 100 puts sprintf( "%.2f", gpo6Hoe_4ucJIo ).to_f

# =)

1. ? 2. ? 3. ? 4. ? (, .) 5. , 1890 . ? ?

, . ( ) .

. :[1,2,3,4,5,6]

, . . , .new Array. :Array.new( 6 ){ |index| index + 1 } #-> [1,2,3,4,5,6]

.new ( 6). , . , . Range () .to_a:

(1..6).to_a

#-> [1,2,3,4,5,6]

, .

to_a , .(1..10).to_a ('a'..'d').to_a #-> #-> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ['a', 'b', 'c', 'd']

, , . . :[ 'a', 'b', 'c', 'd', 'e']

:[ 'a'0, 'b'1, 'c'2, 'd'3, 'e'4 ]

Ruby . , , ? , ![ 'a' , 'b' , 'c' , 'd' , 'e' ]

. . ? , : , .maccuB = [ 'a', 'b', 'c', 'd', 'e'] maccuB[ maccuB.size - 2 ] #-> 'd'

.size, . , maccuB.size , . :maccuB = [ 'a', 'b', 'c', 'd', 'e'] maccuB[ -2 ] #-> 'd'

-2 .

. . , . :maccuB = [ 'a', 'b', 'c', 'd', 'e'] maccuB[ 1..-2 ] #-> ['b', 'c', 'd']

:

maccuB = [ 'a', 'b', 'c', 'd', 'e'] maccuB[ 1...-1 ] #-> ['b', 'c', 'd']

, .

Ruby , . :[[1],[2,3],[4]] # [[1,2],[3,4]] # [["",""],[""," "]] [["",""],[1,[""," "]]] # -

. . . : , .

, . , . ; . Ruby : . .size:

[1,'',3,'',5,6,'',2,5].size

#-> 9

, :maccuB = [1,'',3,'',5,6,'',2,5] maccuB.size #-> 9

, . , ,

.size . , . / , ? ? Ruby .max, .max_by ( 1.9). :['','','',''].max ['','','',''].max_by{ |elem| elem.size } #-> '' #-> ''

.min .min_by :['','','',''].min ['','','',''].min_by{ |elem| elem.size } #-> '' #-> 'y'

? . . , .sort .sort_by ( 1.8).['','','',''].sort #-> ['','','','']

['','','',''].sort_by{ |elem| elem.size } #-> ['','','','']

, . , . ! , . .reverse, . , , . .reverse. , :['','','',''].sort.reverse ['','','',''] ['','','',''].sort_by{ |elem| elem.size }.reverse ['','','',''] #-> #->

.reverse . ; , , . / , +:[1,2,3,4] + [5,6,7] + [8,9] #-> [1,2,3,4,5,6,7,8,9]

, , . . -, , :[ 1, 1, 2, 2, 3, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #-> [3, 3, 3, 5]

, , . , . ( ) , .

: | () & (). :[1,2,3,4,5,5,6] | [0,1,2,3,4,5,7] #-> [1,2,3,4,5,6,0,7]

. :[1,2,3,4,5,5,6,0,1,2,3,4,5,7]

, , , , . . :[1,2,3,4,5,5,6,0,1,2,3,4,5,7]

(). :[1,2,3,4,5,5,6] & [0,2,1,3,5,4,7] #-> [1,2,3,4,5]

, , . .[1,2,3,4,5,5,6] & [0,2,1,3,5,4,7]

. , | & , . . , ; , , =. , . ( ) Ruby .uniq:[1,2,3,4,5,5,6,0,1,2,3,4,5,7].uniq #-> [1,2,3,4,5,6,0,7]

, .

[1,2,3,4,5,5,6,0,1,2,3,4,5,7]

( nepBblu_maccuB + BTopou_maccuB ).uniq. , , . .flatten , . . , , , .max:maccuB = [[1,2],[3,4]] maccuB.flatten.max #-> 4

. .[[1,2],[3,4]]

, . , .[1,2,3,4]

! . (, .). : . . ... , , :maccuB_2D = [[1,2],[3,4]] maccuB_2D.map{ |maccuB| maccuB.max } #-> [2,4]

, ( ):maccuB_2D = [[1,2],[3,4]] maccuB_2D.transpose.map{ |maccuB| maccuB.max } #-> [3,4]

.transpose .

, . , , . , .["", "", "", ""] * 2 #-> ["", "", "", "", "", "", "", ""]

:maccuB = ["", "", "", ""] maccuB + maccuB #-> ["", "", "", "", "", "", "", ""]

, ? ! :maccuB = [1,2,3,4,5] maccuB[ maccuB.size ] = 6 maccuB #-> [1,2,3,4,5,6]

, - . :maccuB = [1,2,3,4,5,6] maccuB[0...-1] #-> [1,2,3,4,5]

, . .push ( ) .pop ( ):maccuB = [1,2,3,4,5] maccuB.push( 6 ) maccuB #-> [1,2,3,4,5,6] maccuB.pop #-> 6 maccuB #-> [1,2,3,4,5]

/ , . , . , [], []= +:maccuB = [1,2,3,4,5] # maccuB = [6] + maccuB maccuB #-> [6,1,2,3,4,5] maccuB[0] #-> 6 # maccuB = maccuB[1..-1] maccuB #-> [1,2,3,4,5]

, :maccuB = [1,2,3,4,5] # maccuB.unshift( 6 ) #-> [6,1,2,3,4,5] # maccuB.shift #-> 6 maccuB #-> [1,2,3,4,5]

.shift (), , , .unshift ( , ). // .shift, .unshift, .pop .push:.unshift( 0 ) .push( 6 ) [1,2,3,4,5] .shift .pop

() , () . . .shift , .pop . .push , .unshift . ,

, , , . , . , , .each ? , , . , , , .. .each mixin Enumerable. , .inject, .each_with_index ..

, (true false). , ? ( ). , : , ?. :maccuB = [1,2,2,3] puts maccuB.methods.grep(/\?$/)

, :maccuB = [1,2,2,3] puts maccuB.methods.grep(/\?$/).sort

? , ? .size .find_all:maccuB = [1,2,3,4,5,6,7] uckomoe = 5 # , maccuB.find_all{ |elem| elem == uckomoe }.size != 0 #-> true # ,

(!=, .find_all .size) ! . .include?. , :

maccuB = [1,2,3,4,5,6,7] uckomoe = 5 # , maccuB.include?( uckomoe ) #-> true # ?

, , . , .include?o3epo = [" ", "", "", "", "", "", "", "", " "] o3epo.include?("") #-> false

, . ? ?, , . , : ?nycTou_maccuB = [] noJIHbIu_maccuB = [1,2,2,3] nycTou_maccuB == [] #-> true noJIHbIu_maccuB == [] #-> false

: ?nycTou_maccuB = [] noJIHbIu_maccuB = [...