Uvod U Programski Jezik Ruby D400
Total Page:16
File Type:pdf, Size:1020Kb
Uvod u programski jezik Ruby D400 priruĉnik za polaznike © 2008 Srce Ovu inačicu priručnika izradio je autorski tim Srca u sastavu: Autor: Hrvoje Marjanović Recenzent: Viktor Matić Urednik: Vladimir Braus Lektorica: Milvia Gulešić Machata Sveučilište u Zagrebu Sveučilišni računski centar Josipa Marohnića 5, 10000 Zagreb [email protected] ISBN: 978-953-7138-52-3 (meki uvez) ISBN: 978-953-7138-53-0 (PDF) Verzija priručnika D400-20141205 Ovo djelo dano je na korištenje pod licencom Creative Commons Imenovanje-Nekomercijalno-Dijeli pod istim uvjetima 4.0 meĎunarodna. Licenca je dostupna na stranici: http://creativecommons.org/licenses/by-nc-sa/4.0/. Sadržaj Uvod .................................................................................................................................... 3 1. Općenito o programskom jeziku Ruby ...................................................................... 4 1.1. Prednosti i nedostaci programskog jezika Ruby ................................................................................... 4 1.2. Priprema radnog okruženja ................................................................................................................... 6 1.3. Pokretanje koda .................................................................................................................................... 7 1.4. Dokumentacija ....................................................................................................................................... 8 1.5. Vježba: Instalacija programskog jezika Ruby ....................................................................................... 8 1.6. Pitanja za ponavljanje ........................................................................................................................... 8 2. Osnove programskog jezika Ruby ............................................................................. 9 2.1. Ispis ....................................................................................................................................................... 9 2.2. Struktura programskog jezika Ruby ...................................................................................................... 9 2.3. Varijable ................................................................................................................................................ 9 2.4. Tipovi podataka ................................................................................................................................... 10 2.5. Operatori ............................................................................................................................................. 14 2.6. Kontrolne strukture i petlje .................................................................................................................. 15 2.7. Sistemske naredbe .............................................................................................................................. 18 2.8. Konstante ............................................................................................................................................ 19 2.9. Vježba: Tipovi podataka, operatori, kontrolne strukture, ulaz/izlaz, sistemske komande ................... 19 2.10. Pitanja za ponavljanje ....................................................................................................................... 20 3. Izrazi ............................................................................................................................ 21 3.1. Vrijednost izraza .................................................................................................................................. 21 3.2. Pridruživanje vrijednosti varijablama i usporedba ............................................................................... 21 3.3. Višestruko pridruživanje vrijednosti varijablama ................................................................................. 21 3.4. Zamjena sadržaja varijabli .................................................................................................................. 22 3.5. Vježba: Izrazi ....................................................................................................................................... 23 3.6. Pitanja za ponavljanje ......................................................................................................................... 23 4. Klase i objekti ............................................................................................................. 24 4.1. Klase.................................................................................................................................................... 24 4.2. Metode ................................................................................................................................................ 24 4.3. Objekti ................................................................................................................................................. 26 4.4. Atributi i varijable ................................................................................................................................. 26 4.5. Inicijalizacija objekata .......................................................................................................................... 28 4.6. Sve je objekt ........................................................................................................................................ 29 4.7. Vježba: Klase, metode, objekti, atributi i varijable ............................................................................... 31 4.8. Pitanja za ponavljanje: ........................................................................................................................ 31 5. Rad s datotekama ...................................................................................................... 33 5.1. Otvaranje datoteka .............................................................................................................................. 33 5.2. Čitanje i pisanje ................................................................................................................................... 33 5.3. Rad s direktorijima ............................................................................................................................... 34 5.4. Upravljanje imenima datoteka ............................................................................................................. 35 5.5. Program i osnovni direktorij ................................................................................................................. 36 5.6. Pisanje programa za različite operacijske sustave ............................................................................. 37 5.7. Brisanje i kopiranje datoteka ............................................................................................................... 37 5.8. Vježba: Rad s datotekama .................................................................................................................. 39 5.9. Pitanja za ponavljanje ......................................................................................................................... 39 6. Više o klasama i objektima ....................................................................................... 40 6.1. Vrste metoda ....................................................................................................................................... 40 6.2. NasljeĎivanje ....................................................................................................................................... 42 6.3. Ulančavanje metoda............................................................................................................................ 42 6.4. Blokovi i iteratori .................................................................................................................................. 42 6.5. Vježba: Klase i nasljeĎivanje ............................................................................................................... 44 6.6. Pitanja za ponavljanje ......................................................................................................................... 44 7. Upravljanje greškama ................................................................................................ 45 7.1. Klasa Exception ................................................................................................................................... 45 7.2. Upravljanje greškama.......................................................................................................................... 45 7.3. Catch i throw ....................................................................................................................................... 47 7.4. Vježba: Upravljanje greškama ............................................................................................................ 48 7.5. Pitanja za ponavljanje ......................................................................................................................... 48 8. Moduli ......................................................................................................................... 49 8.1. Korištenje više datoteka ...................................................................................................................... 49 8.2. Definiranje modula .............................................................................................................................