SL Unit 2

Download as pdf
Download as pdf
You are on page 1of 12
| a Lo ONT-2 aS nese Ruby : Rulay is a object - oriented reflective, geneval - popese, dyvanite programming dawg vag . “Ruboy was devcloped fo wake Wh act as a Sensthh “ \yatles lug human Proyawiners the widelyirg | conpidiig woulrinety, bk 4s on awhaapreled seriphing Jarguoge which Meer wosk of obs Fmplemeucedtions execute inshructtons ; Bred fredly, withouwe previews ly, compiling a Progreymn. wo wachiue - Sarguage instuckions “Ruby ‘s used to ceale web applications of Belfer sors. - sslbiyie a extend Roky cotth nec ferboes by * vorthtg cde in Ra 4: Once yout shack odehing ty , \oo-level code wrikken ty C, howevey Me possi Eiht \ -d ate endled, ORaby cbiecks m ke + Evething on Ruby RS cin objeck, and abt variables are selerenced Xo objects Most Ruby okies are seprearnted aa C potvlees to an cea Th Memory thot Conlafa the obeectls \ ‘y dara & tur tmplemacketion dekele. i s i, C cote, oD Shave veftrences cae vie. variates 4 ar Value , so when wee pos Rat objects unde you LE doh by passing value. ais. yas one exception For Performance reasons, Ruby Aonplemends Fianums, Symbols, teue, false and, Me yl as so-called Smmedtate vodrer, wort ing wa, some dtake okjecks FLXNUMSCvalue) — > von-se10 tf value fs a TnUM Ang SYMBOL ~Pvalue) — snon-ze10 UY value ts arto ms NUL? (vale) 3 Non-zero % value 7s wil REST (value) —y nowzeo 14 value HY neithe Whig, witht re eWorkirg w% sivas! of a wt a nubl- Gn C, we are wsed Ao coor tig desnivaked! stig’: Ruby shirgs, howeved, age more, genreradl Wary well ‘clude embedded nulla. b yew « Ruby shiny objects ase ackually, veferences to an “a Rstiing: sdrudbare, ANE Reig bucket coda ‘ Vet oo Levath © a porides Feld. We com acces the shrucute via Me Robina macros JNTZNUMC rk) 9 Frere oF Bignamt INT ZELK Gt) a Fremum (faster) CHARLEY Char) A Fanum ale sh Sa- newdCohor ¥) 3 String eas C dokskyges t0 Raley obs ects. ai ——— < | RSTRING LEN (sta) — len of fhe Ra wy RSTRING — PTR (sb) 3 Petites to sting storage D RSTRIN GAEND (sty) a Poiwhee toed of shig h “Working with other _ekjeds » . When valuc’s are net immediiade, hey ake 4 » pokes ko one of the predefined Ralky objet I Huck es, 4 The she cluces for Whe baste built in claiges Oe defined tx ralaysh % ase named RClarnarne: 4 PAnay RB QtUNRClass, RDaka, RFile, RF lec, Ria, 9, i RObseck, RReger p, Rsting & RStruck, OL™ Sykelor Etovsten + Tukebor eenge —Fodlenfacng, C code usith rub hy and shattrg, dota and behavioy bewoeen, Lhe hog a wordlds “Weagptig c, sbuckurey« a Woke Deda Whap- struct (value cla, veid (+ wat). p Uy, VEAC¥Fce€)07, void. tp) a — Whos the Gen © data ‘ype pee, vegisters tty 5 two aptloage colleckton veutnes 2 vekvons co volue a Pawlet fo a genuine ruby object. Value Dada_make. Shuck (Vakue dass, ¢ (Hark lO), voi A eee) (), CAype #) —Nllocedex ond seks do 2¢10 a struclue of CB ceeieccemceeereee ete ee ENUF LO ve type: void # y mt $ ie grdfcoded ype fiwsh © Yhon proceeds ox pata rap - Stuck, C-Aype es Ye tame of dada sgpe Wok you've wrapping, nol a vasiable of Yet ee ioe. Gek-Stouck (value obj, ctype, ctype *)_ Reus Ae ovtefrad peter. Daka-Gel-Strucl fsa qyesate Weappes around He wero Pata PTRC) wkidy evalluckes he potter. : Obfeck, Creakion me: - Creaking on, okgeck of claw CPPlag er tn our lash, Raley progiarn ¢ N cd = CDPlayes nreto We Implementation of new in clas is Simple, “A allocakes memos for new objeck © then calls! the objeckls ‘Wikialtve method to wwittalize hak +00 MEMO. ) + So, woe need yo Time evck can allocation fane and can entealtee methed. » Mocakion Funchgoncktons = The allocation function ts responsible for creedking | he The memeny “used ky object. Lue “Sf Ake object ‘naplementirg deawt we amy dota oer Han Rub ‘nstavtee variables, neu You don't wed to vworite an alocat ton. function — Rubys Adoutk ablocutor sill usek gush fhe. | Bok th your clare voraps a © shack el) imeed-tc athorate space jor Wed struck fy 7 Mu allocation tunckion. odes Whe ablocabfon fume" qs pared the class of te pol? dbject bebg alected. 4 2 We allecadGon fume’ cveccteg aun ewgly , unin ‘ale|-* ~ted objec , & we nl ko Kill w specific ree + Clonina, dejecks bl All ruby obbjecls can be copied. Msg one of | wo Me\hods cup and clone. Both produce ante inskance of tutr receiver's claw by calling the | * adlocakion funckion | “Then Ahoy copy across amy ‘nstance variables | from tue original. Clee thon goes ta WA further! ! and copfes dnt original's stn fags . Aten CORINA Abe object's ‘instance vaniables, Ye Awdherpreker Tavolees the newd obgells intlealtie COPY meted, pawing Awa velerence lo \he origind okkecl. wo Xoh tne oxiginall object code checless aos oa Ayre of daha and ~ Has a free func’ lh same addict, as cur dvee find. yOu Wa deter claws and cqagtenny, ection nice iq concn, we weed. do allocate memory ino an j “teuton Yok won't be - object shorage | cu lave o& ant bilmap for a boolean bi pie : Sage, oF ak i of . (ele shkucly! als i elk Raley doesn't use Atecy. | L ~ qo work covsectly With the gpeboae colleclor age | As shold Use the following MeN allecation vouktwes Thee voukines do a (Mle Lik more work Yron Yee shovdard matter func! ante y : Lape * AON Co-Lape, ») w —Mocaked w c-Agpe objecls, where Ape ts | \ the eral name of Ma © type « wot a varias | ee le of that Aype- rthey ' 4 Aye #ALLOC Ce-Ayge) | nu ~Mlocader a c-Aype & cals the resulk toa 28 Powder of Wed {ype taltie. » REALLOC-N (van CAyper n) iin ~Reallocates, Mw CAyes 9 ONsiqus the result | to vag 7& prinber do & Vatiable of Age yp type ¥ ALLOCK Ni Ce type a) —Mocale, memory for Wn objecks on Ye stack — thes vne of CAype | img tik Le eatin Il ~odically freed chen he fume’ tect | evokes ALLOCA nt xehouns, uy | = 11D, 2 < PUES Tye Sytent Sve Raby we sely \ew on Mhe Aype Cor clare), at on chiect and more. on IMs capabilites. This «se! 2 oe Knoon ax Duck Typivg- abe sence, Ducle Typing means an objec Nype ws (oe © defined by had th can do, ne by ubat tt te jae Duck Aygo yefers to dhe Vendeneg of Ral io © be Lew foncemed ith the clay of an object a b More Concerned woth whet methods can be called| WW on Hh & chet opetations can, be pesdouned on | ot ss “ha an Ruby, we would use ¥espond_to 1 os oath : Simply ws an okeck fog methed and know “ the an exception ull be xaised 4{ VA ts used ‘mappropriately Mf an obleck uailks MWe oa Auck ard Aabke Lite a duccle, Yon Ve Ruby ‘Indes preter & Veppy fo Wear, wh os ER wee a duck, <4 Br edkeck. cWhelher he object. Adfanes the Aowste mel >> lA Sing! Spend to } G ost) => dive >> Excephton, MEW. vespord_fol (tog) = false (8 >> a. respond? Close) . ae on => false SY gan \ WA Hee qT above eramplg Isddype eb Buck typig. Ua) gk oO okleck quacte hike a Aude Cor ads Like « Be 4 sting), (ust ae ahead €& teak Wh as a duck Comm 7 fang. Wheneses possttle you shoud tread okje cls vccod to voelhods (rey, define rather than the camer from shich toy trhettd o¢ He modules bs, dhe indud e. & Leddy Ruby do Olney o to @)onbeltire Ruby to ha lovguaae® ; Ke hem yor Fes foot ack Some Ruby code, 74 caf cdlle,| vkdy sermind Yeu of othe Prog armed \erequccen Mm we have used. hs This ts becom of the sya ts fandiliad to wees “ at Perl, Padker, and Java ede, wo ed «Lond Cat Ruby © © have We Waalkuy ssumlbiokte vel ‘fonship, p Silat thet ay Wane te uke, 5 . Shiugs ge in doulle- quotes, reac + Shh are mutable a Sovilatltes | wih Cth! as with Cat, in Reuky mel, public, pavate and protected do Sather sobs. Encepons work Mia sre Keyword vank have (ano cent. wy | sVova 8s moduses t's tested And Ws fast. ak mawnier , Yhough the been charged) to protect the Qdingy fcow fava Ad ruby , excep Your code Se som ko ahyvine down considietably. we can also expect i not Ab dake lew Ke to knock together quick Protot athe Re. “pate Swilartler: Ac wilh Gave’, Sn Rub in Memory, fs wromaged fox you via a qesbege ~e colechos. et Objecks are skiovalay Ayped. oe “Thoe ae public, private & prclected! melheds, a Bele. | Ake Pad docs ate Awesome, For thee Peers who wl \ovg, fox elegans, objeck ovented (ecdluves built-in closu from ue beefing Rutey can hep. Qs Swslaidies Ae wily Pest an Rul i _ Porerdheses ate often op\ional code Sheings voor barteably the same. ewte wes PHP: ‘ SPHP ts Kefaticaly ny widespread use for eh | appltcadtons, Buc you won Ao use Ruby on aol at lox Aust wank a lanuoge Yakis: move Laglored S | Sos geneva use, Ruby %s voorlh a leek. oa Siwflacea: As tn PHP, in Ruby ot ee call JRuby % dapawtiallly Ayped, Ske Te PHP, & you ; Aon'k need to wooeg about having to declare Bs He Vastables. i a he ee, a Some vastaloles sel with 4, Mike ‘in PHP (Ld Lay not all) 3 ‘ny Baie ou t OM. Paton as ancthes vey nice gered purpose progre xe larquat: salasties: As wrth Pathon, An Ruby, : - shit Yrerals can Span amuliple Liner like Python wage gushed sbings. tiga me ly od Analy There good suppord fov funckionall proguarnnding wth Kash- claws Func\ions, anonymous funckfons anc closures. OQUrbedig o Bike Seeders San addekton +0 extending Ruby by odding c code, rr can also Aun the problem avound~ 2 ented Rude Asel§ willie your exppticect ion, We can *wherack Ruby by C APL ov by oraluatig Skyingg cornet we eal Ateaet ly eabuakig, cbtigs of Ruby coe Ae ee clalere she Ruy eodecpreks, we weed to call uly. aan) Xo Pek up Command ine aagumenhs ¥ by Rady, Ruby intl Stack to sefup He wuby stack, and yuby-inik 4o Gdtaltee the oF “Arlaps ees Asddh. The cath Xo “vuby “inc boefag” end jadds any Mrechorfes to be searched (os Whecrsieg Thés bind of hands-off wranipulatfon of Rub Proqrarns from wily C code Ys €asy, bul 44 has \uoo waios drawbacks. Pra, as evden. “we rave to Weep sto ving Aevys na alckals 7 extrackivg te values trom thoxe alobals out to use whem, — Second, Wl are not Asiy eu seq evyo checking, wich ust dekinndely Whe us - lake, So, We second vay to trlerack Lith Ruby code %s to Use due C APE. Dcond, we veil wlerack kay cvaluaking c APL | “This ves us much fing qratned| combel ¢ takso leks us handle esrors, ble can do {his hy inihetb.lea ag the Swherpeley as Nomal. Then, vetho, than evaluating ctrngs, ‘nslead Invoke specific metheds In yours Ruby | code. Athen “hese methabs velusn your C code gets covtvo | back, Le vor ast pe woke —4 c vor caaill 4, “ewibelled xg \ a ae wd abe oe as aderpreker. qhis Junc she we calhe before ony, other wl related functions, ed rath “nih eagle) — Aelkaltees Whe Qs vathalde , mecersand CY your code \cods cou Wlreng rocdules. word wha options Grd asge, cha ean) b — Ges We aby ankerpreler Ye commande copious. Voth vuloy- serfph char % nana) | —scds Me vame of the vulby scfiph fo raw. —leads We queen frle &vdo Ye iwcer preler , a VARA subytunt): ~ Runs we indevpreter ved swhy—frolkret ) — vshuds down ‘he iwdeapreler.

You might also like