ruby on-rails

  • View
    93

  • Download
    3

Embed Size (px)

Text of ruby on-rails

  • CC VN HIN I CA CNG NGH PHN MM

    Ch : Tng quan v ngn ng Ruby

    Ging vin: TS. Trng Anh Hong

    Thnh vin:

    Nh Tun Anh

    o c Thnh

    m Thi Nhin

    H Ni 01/10/2012

  • Mc lcChng 1: Ngn ng lp trnh Ruby .............................................................................................. 4

    1. Lch s hnh thnh.....................................................................................................................4

    2. Gii thiu v ngn ng ruby .....................................................................................................4

    3. Xem mi th nh hng i tng .......................................................................................... 4

    4. Ruby l mt ngn ng mm do v linh hot ...........................................................................5

    Chng 2: Ngn ng Ruby............................................................................................................. 6

    1. Bin trong ruby .........................................................................................................................6

    2. Cc dng d liu trong ruby......................................................................................................6

    2.1. S .......................................................................................................................................6

    2.2. K t ..................................................................................................................................6

    3. Mng trong ruby........................................................................................................................7

    4. Cu trc iu khin trong ruby .................................................................................................7

    5. Vng lp trong ruby ..................................................................................................................9

    5.1. While..................................................................................................................................9

    5.2. Util .....................................................................................................................................9

    5.3. For......................................................................................................................................9

    5.4. Cu trc lit k...................................................................................................................9

    5.5. Hash .................................................................................................................................10

    5.6. C mt vn trong y l Blocks..................................................................................11

    6. Lp v modules.......................................................................................................................12

    7. Tha k trong Ruby ................................................................................................................14

    Chng 3: Framework Rails ......................................................................................................... 16

    1. Th no l framework .............................................................................................................16

    2. M hnh MVC.........................................................................................................................16

    2.1. Khi qut v m hnh MVC............................................................................................. 16

    2.2. Cc thnh phn ca m hnh MVC..................................................................................16

    2.3. u v nhc im ca m hnh MVC ...........................................................................17

    3. Ruby on Rails.......................................................................................................................... 18

    3.1. Ngun gc ca Rails........................................................................................................18

    3.2. M hnh MVC vi Rails ..................................................................................................18

    3.3. T ng sinh code trong Rails.........................................................................................19

    3.4. Vng i pht trin mt ng dng trong Rails ................................................................ 19

  • 3.5. So snh Ruby on Rails vi Java v PHP .........................................................................20

    Chng 4: Ci t v pht trin mi trng Rails ........................................................................ 22

    1. Cc mi trng c th pht trin Ruby on Rails ................................................................ 22

    2. Cch ci t mi trng pht trin Ruby on Rails..................................................................22

    3. Ci t cng c h tr code.....................................................................................................22

    Chng 5: Nhng vn c th ca Rails.................................................................................... 24

    1. B cng c pht trin .........................................................................................................24

    2. c im c bn vi c s d liu ca Ruby on Rails ........................................................... 25

    2.1. Gii thiu Active Record.................................................................................................26

    2.2. nh gi Active Record vi cc h csdl khc .................................................................26

    2.3. Thao tc s dng Active Record .....................................................................................26

    Chng cui: Tng kt ................................................................................................................. 28

    Ph Lc ......................................................................................................................................... 28

    Ti liu tham kho........................................................................................................................ 28

  • Chng 1: Ngn ng lp trnh Ruby1. Lch s hnh thnh

    Ruby l mt ngn ng lp trnh hng i tng, c kh nng reflection. C php b nh hngt Ada v Perl vi cc c tnh hng i tng ca Smalltalk, v cng chia s mt s c tnhvi Python, Lisp, Dylan v CLU.Ruby c to ra bi Yukihiro Matz Matsumoto t 24 thng 2, 1993 v a ra bn chnh thcvo nm 1995.

    Ruby chu nhiu nh hng t Perl, v khi hon tt ngn ng ny, anh a vi mtngi bn rng nn t tn th no nghe cho n ging mt th qu no (Perl lc u cngc t tn l Pearl - ngc trai). V bn ca anh gi ci tn Ruby. Sau ny Matz cng btng khi pht hin ra Pearl l vin qu tng trng cho nhng ngi sinh thng 6, cn Rubyth tng trng cho nhng ngi sinh thng 7. Anh cho rng ci tn Ruby nh th l ph hp vRuby k tha v pht trin nhiu c tnh t Perl.

    Thng 9 nm 2005, phin bn n nh mi nht l 1.8.3. Ruby 1.9 (vi mt s thay i)cng ang trong qu trnh pht trin.

    2. Gii thiu v ngn ng ruby

    Thay v nh ngha v Ruby ti xin trch nguyn vn li gii thiu v Ruby ca cng ngRuby nh sau: Ruby l mt ngn lp trnh m ngun m, linh hot, vi mt s ni bt v sn gin d dng v hu ch. N c c php tao nh v t nhin d c v d dng vit.V theo nh tc gi ca ngn ng Ruby, anh Yukihiro matz Matsumoto ngi Nht, th tcgi l mt ngi rt a thch cc ngn ng nh: Perl, Smalltalk, Eiffel, Ada, v Lisp. Tc gi c gng kt hp mt cch thn trng cc ngn ng ny vi nhau t to ra Ruby. C lchnh v vy m Ruby l mt ngn ng vi cc cu lnh v c php kh quen thuc khi chc.Mt vi c im nhn thy ngn ng Ruby l:

    Ruby c c php ngn nhng c th lm c nhiu vic. Ruby l ngn ng rt hng i tng.

    3. Xem mi th nh hng i tng

    Vi Ruby, mi th u l mt i tng. Mi bit thng tin hay code u c th c a racho chnh chng vic s hu cc thuc tnh hay hnh ng. Trong lp trnh hng itng th cc thuc tnh gi l cc bin thc th (instance variables) v cc hnh ng gil cc phng thc. Ruby l mt hng i tng tinh khit, iu ny c th thy bngmt bit ca code m thc hin mt hnh ng vi mt con s. S 5 cng c coi l mt i

  • tng v c phng thc lm times. on code di s in ra 5 ln dng We *love* Ruby:5.times{ print "We *love* Ruby" }

    a s cc ngn ng khc, con s v mt s kiu d liu nguyn thy khc th khng c coil hng i tng. Nhng Ruby theo s nh hng ln ca ngn ng Smalltalk bng vica ra cc phng thc v thc th bin ti tt c cc kiu ca n. iu ny to nn s thngnht v d dng khi s dng Ruby vi tt c cc i tng.

    4. Ruby l mt ngn ng mm do v linh hot

    Ruby c coi l mt ngn ng mm do v linh hot, t khi n cho php ngi dng t dosa i c phn ct li ca n. Cc phn ct li ca Ruby c th c ri i hoc nh nghali nh mun. Cc phn hin hu c th c thm vo. Ruby c gng khng hn ch ccnh lp trnh.

  • Chng 2: Ngn ng RubyTrong chng ny s ni qua tng th nhng vn c bn vi Ruby. Cc bn s hiu nhngci c bn nht v Ruby. V cc bn c th thao tc c bn vi nhng vn ca ngn ngRuby.

    1. Bin trong ruby

    Bin ca ruby khng cn khai bo kiu. Khi gn gi tr cho bin n s t ng nhn kiu, nhncc gi tr cho bin.V d:

    User =userPuts user => user

    Khai bo bin ca ruby lm cho ngn ng tr ln linh hot hn.Bin ton cc ca ruby: khi thm @ trc bin th n tr thnh bin ton cc s dng khi tcng trong chng trnh.Cn khi khng c @ th n l bin a phng ch c s dng trong mt phn no cachng trnh.Mi bin ca ruby l mt i tng. Nh vo vn ny, bn c th tc ng n tng vn ca bin: nh chuyn kiu, ghp cc bin vi nhau.Nh c tnh cht trn cc bin trong ruby c th tng tc vi nhau mt cch d dng. V d:

    @u =Ngon ngu{#@u} lap trinh=> Ngon ngu lap trinh

    Cng c th o ngc chui hoc vit hoa

    2. Cc dng d liu trong ruby

    Ruby l mt ngn ng hng i tng. y cc thnh phn d liu t s, k t, mng uc xy dng trn c s cc lp.

    2.1. S

    Cu trc phn cp d liu dng s trong Ruby: Cc dang d liu kiu s c xy dng t lpNumeric. Kiu s nguyn: Integer c hai dng l Fixnum