-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
1111 lines (869 loc) · 68 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Ruby Workshop]]></title>
<link href="http://ruby-workshop.github.com/atom.xml" rel="self"/>
<link href="http://ruby-workshop.github.com/"/>
<updated>2012-02-25T14:25:00+01:00</updated>
<id>http://ruby-workshop.github.com/</id>
<author>
<name><![CDATA[Hrvoje Šimić]]></name>
<email><![CDATA[[email protected]]]></email>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[uvod u ruby]]></title>
<link href="http://ruby-workshop.github.com/blog/2012/02/20/uvod-u-ruby/"/>
<updated>2012-02-20T14:00:00+01:00</updated>
<id>http://ruby-workshop.github.com/blog/2012/02/20/uvod-u-ruby</id>
<content type="html"><![CDATA[<blockquote><p>For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.</p><footer><strong>Yukihiro Matsumoto</strong> <cite>The Inventor of Ruby</cite></footer></blockquote>
<p>Kako tebe Ruby može učiniti sretnim?</p>
<p>Hello world u Javi:</p>
<figure class='code'><figcaption><span>HelloWorldApp.java </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">class</span> <span class="nc">HelloWorldApp</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Hello world!"</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Pokretanje:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>javac HelloWorldApp.java
</span><span class='line'>java HelloWorldApp
</span></code></pre></td></tr></table></div></figure>
<p>Hello world u Rubyu:</p>
<figure class='code'><figcaption><span>hello_world.rb </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='rb'><span class='line'><span class="nb">print</span> <span class="s2">"Hello world!"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Pokretanje:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>ruby hello_world.rb
</span></code></pre></td></tr></table></div></figure>
<p>Kako bi na engleskom rekli računalu što želite napraviti?</p>
<p>Ruby je moćan jezik sa predivnom sintaksom. Ruby se želi što više
prilagoditi programeru i govornom jeziku, te mu brzina izvršavanja nije
prioritet.</p>
<p>Filozofija Rubya:</p>
<blockquote><p>Everything is an object.</p></blockquote>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span> <span class="c1">#=> Fixnum</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Integer</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Numeric</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Object</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> BasicObject</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> nil</span>
</span></code></pre></td></tr></table></div></figure>
<p>Čak je i “ništa” objekt:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="kp">nil</span><span class="o">.</span><span class="n">class</span> <span class="c1">#=> NilClass</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Class</span><span class="o">.</span><span class="n">class</span> <span class="c1">#=> Class</span>
</span><span class='line'><span class="no">Class</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Module</span>
</span><span class='line'><span class="no">Class</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Object</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Object</span><span class="o">.</span><span class="n">class</span> <span class="c1">#=> Class</span>
</span><span class='line'><span class="no">Object</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Module</span>
</span><span class='line'><span class="no">Object</span><span class="o">.</span><span class="n">class</span><span class="o">.</span><span class="n">superclass</span><span class="o">.</span><span class="n">superclass</span> <span class="c1">#=> Object</span>
</span><span class='line'>
</span><span class='line'><span class="n">mind</span> <span class="o">=</span> <span class="s2">"blown"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Ruby je jedan od “najobjektnijih” jezika na svijetu.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[zahtjevi za rad]]></title>
<link href="http://ruby-workshop.github.com/blog/2012/02/20/zahtjevi-za-rad/"/>
<updated>2012-02-20T13:59:00+01:00</updated>
<id>http://ruby-workshop.github.com/blog/2012/02/20/zahtjevi-za-rad</id>
<content type="html"><![CDATA[<p>Za ovu radionicu trebat će vam <a href="http://www.github.com">github account</a>
i lokalno instalirana verzija rubya.</p>
<p><strong>Zašto github (osim što je najbolji)?</strong></p>
<p>Zadatke planiram objavljivati u
<a href="https://gist.github.com/">gistovima</a>, najlakšem načinu dijeljenja koda.
Nakon objave zadatak ćete moći rješiti “forkanjem”.</p>
<h2>Instalacija</h2>
<p><strong>Windows</strong></p>
<p>Instalirajte <a href="http://msysgit.googlecode.com/files/Git-1.7.9-preview20120201.exe">Git for Windows</a> i <a href="http://rubyforge.org/frs/download.php/75127/rubyinstaller-1.9.2-p290.exe">Ruby</a>.</p>
<p>Odite na <em>Start > Run > cmd</em> i upišite <code>ruby -v</code>. Ukoliko vam ta komanda
vraća</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>ruby 1.9.2 (x) [y]</span></code></pre></td></tr></table></div></figure>
<p>uspješno ste instalirali ruby :)</p>
<p><strong>Linux/Mac</strong></p>
<p>Instalirajte git: <code>$ sudo apt-get install git</code></p>
<p>Instalirajte RVM (Ruby Version Manager):</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>bash -s stable < <<span class="o">(</span>curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer<span class="o">)</span>
</span><span class='line'><span class="nv">$ </span><span class="nb">source</span> ~/.bash_profile
</span></code></pre></td></tr></table></div></figure>
<p>Nakon toga instalirajte “requirementse” za ruby. Ispišite listu
“requirementsa”:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rvm requirements
</span></code></pre></td></tr></table></div></figure>
<p>Istalirajte ruby</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>rvm install 1.9.2
</span></code></pre></td></tr></table></div></figure>
<p>Ukoliko vam <code>$ ruby -v</code> vraća</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>ruby 1.9.2 <span class="o">(</span>x<span class="o">)</span> <span class="o">[</span>y<span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>uspješno ste instalirali ruby :)</p>
<h2>Problemi sa instalacijom</h2>
<p><em>“Ne mogu instalirati Ruby! Ne dobijem takav izlaz kad upišem ruby -v.”</em>
- <a href="mailto:[email protected]?subject=[ruby-workshop]%20Ne%20mogu%20instalirati%20Ruby!">mail me, baby!</a></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[osnovni tipovi podataka]]></title>
<link href="http://ruby-workshop.github.com/blog/2012/02/20/osnovni-tipovi-podataka/"/>
<updated>2012-02-20T13:58:00+01:00</updated>
<id>http://ruby-workshop.github.com/blog/2012/02/20/osnovni-tipovi-podataka</id>
<content type="html"><![CDATA[<h2>Brojevi</h2>
<p>Počinjemo sa najjednostavnijim tipom podatka - brojem.</p>
<p>Otvorite text editor i upišite</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">puts</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">4</span>
</span></code></pre></td></tr></table></div></figure>
<p>Spremite datoteku kao <code>rezultat.rb</code> i nakon toga pokrenite pozivom <code>ruby
calc.rb</code> iz komandne linije.</p>
<p>Trebali biste vidjeti <code>5</code> kao rezultat.</p>
<p><code>puts</code> - ispisivanje na ekran</p>
<p>Ruby razlikuje dva tipa brojeva - <em>Floatove</em> i <em>Integere</em>.</p>
<ul>
<li>Float - decimalni brojevi</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">1</span><span class="o">.</span><span class="mi">231</span>
</span><span class='line'><span class="mi">0</span><span class="o">.</span><span class="mi">0</span>
</span><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mi">14</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li>Integer - cjelobrojni brojevi</li>
</ul>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">2314</span>
</span><span class='line'><span class="mi">1</span>
</span><span class='line'><span class="mi">9999999999999</span>
</span><span class='line'><span class="o">-</span><span class="mi">205</span>
</span><span class='line'><span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>
<p>Brojevi znaju izvoditi dosta korisnih stvari. Definirana stvar koju
broj zna izvoditi zove se <strong>metoda</strong>.</p>
<p>Primjeri metoda</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">### abs - vraća apsolutnu vrijednost broja</span>
</span><span class='line'>
</span><span class='line'><span class="o">-</span><span class="mi">2</span><span class="o">.</span><span class="n">abs</span> <span class="c1">#=> 2</span>
</span><span class='line'><span class="mi">4</span><span class="o">.</span><span class="n">abs</span> <span class="c1">#=> 4</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### zero? - provjerava je li broj jednak 0</span>
</span><span class='line'>
</span><span class='line'><span class="mi">0</span><span class="o">.</span><span class="n">zero?</span> <span class="c1">#=> true</span>
</span><span class='line'><span class="mi">1</span><span class="o">.</span><span class="n">zero?</span> <span class="c1">#=> false</span>
</span><span class='line'><span class="mi">0</span><span class="o">.</span><span class="mi">0</span><span class="o">.</span><span class="n">zero?</span> <span class="c1">#=> true</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### eql? - provjerava je li broj jednak drugom broju</span>
</span><span class='line'>
</span><span class='line'><span class="mi">5</span><span class="o">.</span><span class="n">eql?</span> <span class="mi">5</span> <span class="c1">#=> true</span>
</span><span class='line'><span class="mi">6</span><span class="o">.</span><span class="n">eql?</span> <span class="mi">4</span> <span class="c1">#=> false</span>
</span><span class='line'><span class="mi">0</span><span class="o">.</span><span class="n">eql?</span> <span class="mi">0</span><span class="o">.</span><span class="mi">0</span> <span class="c1">#=> false (zato jer nisu isti tip)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### round(n) - zaokružuje broj na maksimalno n decimalnih mjesta</span>
</span><span class='line'><span class="c1">### n argument koji šaljemo metodi, nije obavezan. ako ne pošaljemo </span>
</span><span class='line'><span class="c1">### argument podrazumijeva se 0</span>
</span><span class='line'>
</span><span class='line'><span class="o">-</span><span class="mi">2</span><span class="o">.</span><span class="n">round</span> <span class="c1">#=> -2</span>
</span><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mi">14</span><span class="o">.</span><span class="n">round</span> <span class="c1">#=> 3</span>
</span><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mi">14159</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">#=> 3.14</span>
</span><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mo">021</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="c1">#=> 3.021</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Kako prepoznati metodu?</em></p>
<p>Metoda koju pozivate uvijek se nalazi sa desne strane točke, a objekt
kojem govorite da napravi tu metodu sa lijeve. – <strong>objekt.metoda</strong></p>
<p><em>Kako prepoznati argument?</em></p>
<p>Argument je ono što šaljemo sa pozivom metode. Primjer:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">5</span><span class="o">.</span><span class="n">eql?</span> <span class="mi">0</span>
</span></code></pre></td></tr></table></div></figure>
<ul>
<li><strong>5</strong> - objekt</li>
<li><strong>eql?</strong> - metoda</li>
<li><strong>0</strong> - argument</li>
</ul>
<p><em>Zašto negdje koristimo zagrade kad šaljemo argument, a negdje ne?</em></p>
<p>U Rubyu zagrade kod slanja arugmenta nisu obavezne</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mi">14159</span><span class="o">.</span><span class="n">round</span> <span class="mi">2</span> <span class="c1">#=> 3.14</span>
</span><span class='line'><span class="mi">3</span><span class="o">.</span><span class="mi">14159</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">#=> 3.14</span>
</span></code></pre></td></tr></table></div></figure>
<p>U prvom primjeru u ovom poglavlju zbrajali smo brojeve. Koristili smo
<code>+</code> za to. Tako možemo koristiti i sve algebarske operacije (<code>-, +, *,
/</code>).</p>
<p><strong>Sve su ove algebarske operacije metode!</strong> - zato što je Ruby jedan od
najobjektnijih jezika na svijetu</p>
<p>Primjer</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">+</span> <span class="mi">3</span> <span class="c1">#=> 5</span>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">+</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1">#=> 5</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Zašto nismo koristili točku za poziv metode + kada smo zbrajali?</em></p>
<p><code>2 + 3</code> je <strong>sintaktički šećer</strong> ili kratica za pisanje <code>1.+ 3</code> koja je
napravljena da bi nam olakšala život.</p>
<p><strong>Drill</strong></p>
<div><script src='https://gist.github.com/1877085.js?file='></script>
<noscript><pre><code>### Brojčano na ekranu ispišite koliko godina ima sekundi.
### Koliko imate godina ako ste start 8286 dana?</code></pre></noscript></div>
<h2>Stringovi</h2>
<p><em>String</em> je sve što se nalazi unutar navodnika.</p>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"ruby"</span>
</span><span class='line'><span class="s1">'ruby'</span>
</span><span class='line'><span class="s1">''</span>
</span><span class='line'><span class="s2">""</span>
</span><span class='line'><span class="s2">"1"</span>
</span><span class='line'><span class="s1">'48304823048304802384'</span>
</span></code></pre></td></tr></table></div></figure>
<p><em>Kakve nam koristi od stringova?</em></p>
<p>String je tip podatka koji se najčešće koristi. Koristi se za spremanje
podataka o korisniku - korisničkog imena, emaila, lozinke…</p>
<p>Neke od string metoda</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"ruby"</span><span class="o">.</span><span class="n">capitalize</span> <span class="c1">#=> "Ruby"</span>
</span><span class='line'><span class="s2">"mOzIlLa"</span><span class="o">.</span><span class="n">swapcase</span> <span class="c1">#=> "MoZiLlA"</span>
</span><span class='line'><span class="s2">"github"</span><span class="o">.</span><span class="n">upcase</span> <span class="c1">#=> "GITHUB"</span>
</span><span class='line'><span class="s2">"NE DERI SE!"</span><span class="o">.</span><span class="n">downcase</span> <span class="c1">#=> "ne deri se!"</span>
</span><span class='line'>
</span><span class='line'><span class="s2">"mačka"</span><span class="o">.</span><span class="n">reverse</span> <span class="c1">#=> "akčam"</span>
</span><span class='line'>
</span><span class='line'><span class="s2">"mačka "</span> <span class="o">+</span> <span class="s2">"i"</span> <span class="o">+</span> <span class="s2">" pas"</span> <span class="c1">#=> "mačka i pas"</span>
</span><span class='line'>
</span><span class='line'><span class="s2">"mačka"</span><span class="o">.</span><span class="n">length</span> <span class="c1">#=> 5</span>
</span><span class='line'>
</span><span class='line'><span class="s2">"Florijan"</span><span class="o">[</span><span class="mi">2</span><span class="o">]</span> <span class="c1">#=> "o"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Stringovi i brojevi dijele metodu <code>+</code>, ali ne smijemo koristiti druge
brojevne metode na stringovima. (nemaju smisla)</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"tata"</span><span class="o">.</span><span class="n">abs</span> <span class="c1">#=> NoMethodError: undefined method `abs' for "tata":String</span>
</span><span class='line'>
</span><span class='line'><span class="mi">2</span><span class="o">.</span><span class="n">capitalize</span> <span class="c1">#=> NoMethodError: undefined method `capitalize' for 2:Fixnum</span>
</span><span class='line'>
</span><span class='line'><span class="s2">"0"</span><span class="o">.</span><span class="n">zero?</span> <span class="c1">#=> NoMethodError: undefined method `zero?' for "0":String</span>
</span></code></pre></td></tr></table></div></figure>
<p>Zadnji primjer ima smisla, ali i dalje ne smijemo koristiti brojevne
metode na stringu. Zato koristimo <strong>konverziju</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"0"</span><span class="o">.</span><span class="n">to_i</span><span class="o">.</span><span class="n">zero?</span> <span class="c1">#=> true</span>
</span></code></pre></td></tr></table></div></figure>
<p>Ovime smo pretvorili string “0” u broj i onda ispitali je li jednak
nuli.</p>
<p>Koristili smo i <strong>ulančavanje</strong>, to jest ljepljenje metoda jednu na
drugu. Ulančavanje smijemo koristiti bez ograničenja.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"github"</span><span class="o">.</span><span class="n">reverse</span><span class="o">.</span><span class="n">capitalize</span> <span class="c1">#=> "Buhtig"</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Drill</strong></p>
<div><script src='https://gist.github.com/1877184.js?file='></script>
<noscript><pre><code>### Ispišite svoje ime i prezime unatrag
### Ispišite treće slovo svog imena velikim slovom</code></pre></noscript></div>
<h2>Nizovi</h2>
<p>Nizove prepoznajemo po uglatim zagradama.</p>
<p>Primjeri nizova</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">]</span>
</span><span class='line'><span class="o">[</span><span class="mi">414</span><span class="p">,</span><span class="s2">"zebra"</span><span class="p">,</span><span class="mi">2</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>Nizovi posjeduju nekoliko korisnih metoda</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">].</span><span class="n">length</span> <span class="c1">#=> 4</span>
</span><span class='line'>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">].</span><span class="n">shuffle</span> <span class="c1">#=> [4,3,1,2]</span>
</span><span class='line'>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">].</span><span class="n">join</span> <span class="s1">' '</span> <span class="c1">#=> "1 2 3 4"</span>
</span><span class='line'>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">].</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">element</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">element</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> [1,2,3,4]</span>
</span><span class='line'>
</span><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="o">].</span><span class="n">each</span> <span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="nb">puts</span> <span class="n">x</span><span class="p">}</span> <span class="c1">#=> [1,2,3,4]</span>
</span></code></pre></td></tr></table></div></figure>
<p>Zanimljivo je konvertirati drugi tip podatka u niz kako bi dobili metode
niza.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">"Ruby je super"</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sr">/ /</span><span class="p">)</span> <span class="c1">#=> ["Ruby","je","super"]</span>
</span><span class='line'><span class="s2">"Ruby je super"</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sr">//</span><span class="p">)</span> <span class="c1">#=> ["R","u","b","y","j","e","s","u","p","e","r"]</span>
</span><span class='line'><span class="s2">"Ruby je super"</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sr">/ /</span><span class="p">)</span><span class="o">.</span><span class="n">shuffle</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span> <span class="c1">#=> "je super Ruby"</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Let’s drill it baby</strong></p>
<div><script src='https://gist.github.com/1893905.js?file='></script>
<noscript><pre><code>### Napišite program koji će ispisivati string od 10 slova i 5 znamenki i to svaki put u drukčijem rasporedu
### Napišite program koji će ispisati brojeve od 1 do 10, svaki u svom redu</code></pre></noscript></div>
<h2>Intervali</h2>
<p>Interval (range) prepoznajemo po oblim zagradama:</p>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">10</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="mi">0</span><span class="o">.</span><span class="n">.</span><span class="mi">1</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Korisnost: ne trebamo upisivati sve vrijednosti u niz nego koristimo
interval</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">x</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Drill</strong></p>
<div><script src='https://gist.github.com/1893927.js?file='></script>
<noscript><pre><code>### Ispišite sve brojeve od 1 do 100, svaki u svom redu
### Ispišite brojeve od 17 do 32 u jednom redu, odvojite ih zarezom</code></pre></noscript></div>
<h2>Rječnici</h2>
<p>Rječnik (hash) prepoznajemo po vitičastim zagradama. Rječnik je sličan
nizu, samo što svakoj vrijednosti dodjeljuje ključ.</p>
<p>Struktura rječnika:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="p">{</span> <span class="ss">:key</span> <span class="o">=></span> <span class="ss">:value</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">karakteristike</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:linux</span> <span class="o">=></span> <span class="s2">"zgodan"</span><span class="p">,</span> <span class="ss">:windows</span> <span class="o">=></span> <span class="s2">"nezgodan"</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">[</span><span class="ss">:linux</span><span class="o">]</span> <span class="c1">#=> "zgodan"</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">[</span><span class="ss">:windows</span><span class="o">]</span> <span class="c1">#=> "nezgodan"</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">.</span><span class="n">keys</span> <span class="c1">#=> [:linux,:windows]</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">.</span><span class="n">values</span> <span class="c1">#=> ["zgodan","nezgodan"]</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">[</span><span class="ss">:linux</span><span class="o">]</span> <span class="o">=</span> <span class="s2">"simpatičan"</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span> <span class="c1">#=> {:linux => "simpatičan", :windows => "nezgodan"}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Ovdje je <code>karakteristike</code> varijabla, a varijable služe za spremanje
podataka za kasniju upotrebu.</p>
<p>Glavna karakteristika rječnika je da ključ jednoznačno određuje
vrijednost.</p>
<p>U rječnicima se kao ključevi najčešće koriste <strong>simboli</strong>.</p>
<p>Još primjera</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">karakteristike</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:linux</span> <span class="o">=></span> <span class="s2">"zgodan"</span><span class="p">,</span> <span class="ss">:windows</span> <span class="o">=></span> <span class="s2">"nezgodan"</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">karakteristike</span><span class="o">.</span><span class="n">to_a</span> <span class="c1">#=> [[:linux, "zgodan"], [:windows, "nezgodan"]]</span>
</span><span class='line'><span class="n">karakteristike</span><span class="o">.</span><span class="n">to_a</span><span class="o">.</span><span class="n">flatten</span> <span class="c1">#=> [:linux, "zgodan", :windows, "nezgodan"]</span>
</span></code></pre></td></tr></table></div></figure>
<h2>Uvjeti</h2>
<p>Uvjete u rubyu ostvarujemo ključnom riječju <code>if</code>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">if</span> <span class="o"><</span><span class="n">something</span><span class="o">></span> <span class="k">do</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">if</span> <span class="kp">true</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"bingo!"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ispisuje "bingo!"</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="kp">false</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"bingo!"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ne ispisuje ništa</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="mi">3</span> <span class="o">></span> <span class="mi">2</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"3 je veći od 2"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ispisuje "3 je veći od 2"</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="mi">2</span> <span class="o">></span> <span class="mi">3</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"2 je veći od 3"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ne ispisuje ništa</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="mi">2</span> <span class="o">></span> <span class="mi">3</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"2 je veći od 3"</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"2 nije je veći od 3"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ispisuje "2 nije veći od 3"</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="mi">2</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"nema ostatka sa dijeljenjem"</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"ima ostatka sa dijeljenjem"</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>
<p>Negacija</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">if</span> <span class="o">!</span><span class="kp">false</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"it's true!"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ispisuje "it's true!"</span>
</span><span class='line'>
</span><span class='line'><span class="k">unless</span> <span class="kp">false</span>
</span><span class='line'> <span class="nb">puts</span> <span class="s2">"it's true!"</span>
</span><span class='line'><span class="k">end</span> <span class="c1">#=> ispisuje "it's true!"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Perlovski način</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">puts</span> <span class="s2">"it's true"</span> <span class="k">if</span> <span class="kp">false</span> <span class="c1">#=> ne ispisuje ništa</span>
</span><span class='line'>
</span><span class='line'><span class="nb">puts</span> <span class="s2">"it's true"</span> <span class="k">if</span> <span class="kp">true</span> <span class="c1">#=> ispisuje "it's true"</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Let’s drill this shit</strong></p>
<div><script src='https://gist.github.com/1894357.js?file='></script>
<noscript><pre><code>### a) Ispišite sve brojeve od 1 do 1000 koji su djeljivi sa 13 ili 7
### b) Napravite novi rječnik koji će sadržavati dva ključa.
### Ključevi: 15, 7
###
### Pod ključem "15" u ovom hashu moraju biti svi brojevi
### od 1 do 1000 djeljivi sa 15
### Pod ključem "7" u ovom hashu moraju biti svi brojevi
### od 1 do 1000 djeljivi sa 7
### c) Ispišite koliko postoji vrijendosti za svaki ključ iz prethodnog zadatka
</code></pre></noscript></div>
<h2>Konstante</h2>
<p>Ništa pametno - slične varijablama ali spremaju jednu vrijednost koja se
ne bi trebala mijenjati.</p>
<p>Primjeri</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Boje</span> <span class="o">=</span> <span class="o">[</span><span class="s2">"plava"</span><span class="p">,</span><span class="s2">"crvena"</span><span class="p">,</span><span class="s2">"zelena"</span><span class="p">,</span><span class="s2">"žuta"</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="no">Spol</span> <span class="o">=</span> <span class="o">[</span><span class="s2">"muško"</span><span class="p">,</span><span class="s2">"žensko"</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="no">INSTRUMENTI</span> <span class="o">=</span> <span class="o">[</span><span class="s2">"gitara"</span><span class="p">,</span><span class="s2">"bubnjevi"</span><span class="p">,</span><span class="s2">"bas gitara"</span><span class="o">]</span>
</span><span class='line'>
</span><span class='line'><span class="no">IME_PSA</span> <span class="o">=</span> <span class="s2">"Lola"</span>
</span></code></pre></td></tr></table></div></figure>
<p>Upotreba</p>
<p>Ako se podatak kojim slučajem i promjeni znamo da će se promjeniti i na
svim ostalim mjestima, ne moramo tražiti gdje se sve koristi.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[ah taj divan objektni svijet]]></title>
<link href="http://ruby-workshop.github.com/blog/2012/02/20/ah-taj-divan-objektni-svijet/"/>
<updated>2012-02-20T13:57:00+01:00</updated>
<id>http://ruby-workshop.github.com/blog/2012/02/20/ah-taj-divan-objektni-svijet</id>
<content type="html"><![CDATA[<h2>Metode</h2>
<p>Metoda je definicija više naredbi koje se obavljaju jedna iza druge.</p>
<p>Metodu prepoznajemo po ključnoj riječi <code>def</code>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">ispisi_parne</span>
</span><span class='line'> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">i</span> <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span>
</span></code></pre></td></tr></table></div></figure>
<p>Argumente koje šaljemo metodi moramo definirati unutar zagrada.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">ispisi_parne</span><span class="p">(</span><span class="n">limit</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.limit</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">i</span> <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span> <span class="mi">40</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span> <span class="c1">#=> ArgumentError</span>
</span></code></pre></td></tr></table></div></figure>
<p>Pretpostavljene vrijednosti argumenata</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">ispisi_parne</span><span class="p">(</span><span class="n">limit</span> <span class="o">=</span> <span class="mi">10</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="mi">1</span><span class="o">.</span><span class="n">.limit</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">i</span> <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span> <span class="mi">20</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span>
</span></code></pre></td></tr></table></div></figure>
<p>Više argumenata</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">ispisi_parne</span><span class="p">(</span><span class="n">low</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">high</span> <span class="o">=</span> <span class="mi">10</span><span class="p">)</span>
</span><span class='line'> <span class="p">(</span><span class="n">low</span><span class="o">.</span><span class="n">.high</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
</span><span class='line'> <span class="nb">puts</span> <span class="n">i</span> <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span> <span class="mi">5</span>
</span><span class='line'>
</span><span class='line'><span class="n">ispisi_parne</span><span class="p">(</span><span class="kp">nil</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span> <span class="c1">#=> ArgumentError</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>DRILL</strong></p>
<div><script src='https://gist.github.com/1905082.js?file='></script>
<noscript><pre><code>### Napišite metodu koja prima string i vraća broj pojavljivanja slova a u tom stringu
### Napišite metodu koja prima string a ispisuje ga unazad</code></pre></noscript></div>
<p>Ruby je objektan jezik. Što to znači?</p>
<p>Pas zna lajat, mačka zna mijaukat.</p>
<p>Nove objekte u Rubyu radimo pomoću <strong>klasa</strong>. Klasa nije ništa drugo
nego nacrt objekta.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Dog</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">run</span>
</span><span class='line'> <span class="s2">"running like a boss..."</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">come</span>
</span><span class='line'> <span class="s2">"just a second!"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="k">def</span> <span class="nf">bark</span>
</span><span class='line'> <span class="s2">"woof!"</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">pas</span> <span class="o">=</span> <span class="no">Dog</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>
</span><span class='line'><span class="n">pas</span><span class="o">.</span><span class="n">bark</span> <span class="c1">#=> "woof!"</span>
</span><span class='line'>
</span><span class='line'><span class="n">pas</span><span class="o">.</span><span class="n">run</span> <span class="c1">#=> "running like a boss..."</span>
</span><span class='line'>
</span><span class='line'><span class="n">pas</span><span class="o">.</span><span class="n">come</span> <span class="c1">#=> "just a second!"</span>
</span><span class='line'>
</span><span class='line'><span class="n">pas</span><span class="o">.</span><span class="n">class</span> <span class="c1">#=> Dog</span>
</span><span class='line'>
</span><span class='line'><span class="n">pag</span><span class="o">.</span><span class="n">is_a?</span> <span class="no">Dog</span> <span class="c1">#=> true</span>
</span></code></pre></td></tr></table></div></figure>
<p>Zašto koristiti objektno programiranje?</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1">### Lakše je za čitati</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### Ispitivanje parnosti</span>
</span><span class='line'>
</span><span class='line'><span class="mi">17</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="c1">#=> false</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### ili</span>
</span><span class='line'>
</span><span class='line'><span class="mi">17</span><span class="o">.</span><span class="n">even?</span> <span class="c1">#=> false</span>
</span><span class='line'>
</span><span class='line'><span class="mi">17</span><span class="o">.</span><span class="n">odd?</span> <span class="c1">#=> true</span>
</span></code></pre></td></tr></table></div></figure>
<p>Recimo da želimo udvostručiti broj.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">def</span> <span class="nf">udvostruci</span><span class="p">(</span><span class="n">broj</span><span class="p">)</span>
</span><span class='line'> <span class="n">broj</span> <span class="o">*</span> <span class="mi">2</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">udvostruci</span> <span class="mi">4</span> <span class="c1">#=> 8</span>
</span><span class='line'>
</span><span class='line'><span class="n">udvostruci</span> <span class="mi">5</span> <span class="c1">#=> 25</span>
</span><span class='line'>
</span><span class='line'><span class="c1">### Što je ovdje objekt?</span>
</span></code></pre></td></tr></table></div></figure>