Janko Marohnić
April 01, 2014
170

# Introduction to Ruby

Teaches basics of Ruby, and is best suited for students coming from C-like languages.

April 01, 2014

## Transcript

C++ Java C#
3. ### #include <iostream> ! int main() { cout << "Hello world!";

} C C++ Java C#
4. ### public class HelloWorld { public static void main(String[] args) {

System.out.print(“Hello world!”); } } C C++ Java C#
5. ### public class HelloWorld { public static void Main(String[] args) {

System.Console.Write(“Hello world!”); } } C C++ Java C#

9. ### C int array[] = {1, 2, 3, 4, 5, 6},

sum = 0; ! for (int idx = 0; idx < 6; idx++) { sum += array[idx]; }

12. ### C/C++ #include <stdio.h> #include <stdlib.h> ! int factorial(n) { int

result = 1, i; for (i = 1; i <= n; i++) result *= i; return result; } ! int main() { int f = factorial(3); printf("3! = %d\n", f); return 0; } 0010111001011100011101 0100011110001101000110 0101110100111000000110 1000000101001000001111 1110111100001010000011 1110000110100110111000 1001101010111110100110 1110000010100001001111 0001010110000001111100 1101111001011101011100 1100101001011110001110 1000010111110100110000 0101110100001011111111 1011001000110111000111 1111001010110100011000 3! = 6
13. ### Ruby def factorial(n) result = 1 for i in [1..n]

result *= i end result end ! f = factorial(3) print "3! = #{f}" 3! = 6

15. ### C++ – hibrid string(“Hello”).size(); //=> 5 ! "Hello".size(); //=> ?

// error: member reference base type 'const char [6]' is not a structure or union

17. ### Ruby • Viši programski jezik • Interpretirani • Objektno-orjentiran •

Dinamički • Garbage collector
18. ### C/C++ int* array = (int*)malloc(...); ! // Akcije... ! free(array);

class MyClass { ~MyClass() { ... } };

21. ### Nema primitivnih tipova "A string".class #=> String 1234598.class #=> Integer

3.14.class #=> Float [1, 2, 3].class #=> Array

23. ### C++ class Converter { double degree_to_farenheit(int degree) { // ...

} }; Converter converter; converter.degree_to_farenheit(30);
24. ### C# class Converter { double DegreeToFarenheit(int degree) { // ...

} }; Converter converter; converter.DegreeToFarenheit(30);
25. ### C# Converter converter = new Converter(); Error: class Converter is

abstract, it cannot be instantiated public abstract partial sealed class Converter { protected override static virtual double D… { // ... } }
26. ### C# class CurrencyConverter : Converter { } Error: class Converter

is sealed, it cannot be inherited from public abstract partial sealed class Converter { protected override static virtual double D… { // ... } }
27. ### Ruby class Converter def degree_to_farenheit(degree) # ... end end converter

= Converter.new converter.degree_to_farenheit(30) class Converter def degree_to_farenheit(degree) return degree * 1.8 + 32 end end class Converter def degree_to_farenheit(degree) degree * 1.8 + 32 end end
28. ### “String”.empty?() #=> false Konvencije imenovanja “String”.empty? #=> false ! post.delete

if user.moderator? str = “String” str.upcase! str #=> “STRING”
29. ### class Array def first self[0] end end Proširenje klasa [1,

2, 3].first #=> 1
30. ### 28 29 30 31 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 1 Pon Uto Sri Čet Pet Sub Ned 2.days.ago
31. ### 28 29 30 31 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 1 Pon Uto Sri Čet Pet Sub Ned 1.week.from_now
32. ### class Shirt def end ! def end ! def end

! def end end s m l xl Metaprogramiranje
33. ### class Shirt for size in [“ ”, “ ”, “

”, “ ”] define_method(size) do # ... end end end Metaprogramiranje s m l xl

36. ### 1000000000 1_000_000_000 ! 5.even? #=> false 5.odd? #=> true 2

** 3 #=> 8 ! 3.52.round #=> 4 3.52.floor #=> 3 3.2.ceil #=> 4 ! c = Complex(1,1) #=> (1+1i) c.conjugate #=> (1-1i) c.real? #=> false c.angle #=> 1/4 * π
37. ### Cijeli brojevi . . . . . 0 1 1

0 0 1 0 0 1 0 1 1 1 2 3 4 5 6 59 60 61 62 63 64 • Reprezentirani s 64 bita 0 1 7 58 264-1 0 ➞ 264 brojeva . . . . . 0 1 1 0 0 1 0 0 1 0 1 1 2 3 4 5 6 59 60 61 62 63 0 1 7 58 1
38. ### Cijeli brojevi . . . . . 0 1 1

0 0 1 0 0 1 0 1 1 1 2 3 4 5 6 59 60 61 62 63 64 • Reprezentirani s 64 bita 0 1 7 58 ➞ 264 brojeva . . . . . 0 1 1 0 0 1 0 0 1 0 1 1 2 3 4 5 6 59 60 61 62 63 0 1 7 58 1 263-1 -263 +/- 264-1 0
39. ### Cijeli brojevi . . . . . 0 1 1

0 0 1 0 0 1 0 1 1 1 2 3 4 5 6 59 60 61 62 63 64 • Reprezentirani s 64 bita 0 1 7 58 ➞ 264 brojeva . . . . . 0 1 1 0 0 1 0 0 1 0 1 1 2 3 4 5 6 59 60 61 62 63 0 1 7 58 1 262-1 -262 +/- 263-1 -263 264-1 0
40. ### #include <iostream> #include <limits.h> using namespace std; ! int main()

{ cout << LLONG_MAX << endl; cout << LLONG_MAX + 1 << endl; } 9223372036854775807 -9223372036854775808
41. ### 4611686018427387903 4611686018427387904 max_integer = 2 ** 62 - 1 !

print max_integer print max_integer + 1 4611686018427387903.class #=> Fixnum 4611686018427387904.class #=> Bignum
42. ### 1 / 0 # ZeroDivisionError: divided by 0 ! 1.0

/ 0.0 # => Infinity infinity = 1.0 / 0.0 ! infinity + 1 #=> Infinity infinity > 2 ** 100 #=> true ! infinity + infinity #=> Infinity infinity - infinity #=> NaN

44. ### “String”.empty? #=> false “String”.include?(“ri”) #=> true ! “String”.downcase #=> “string”

“String”.reverse #=> “gnirtS” ! “%d %s” % [var1, var2] “#{var1} #{var2}”

46. ### [1, 2, 3] [“a”, “b”, “c”] ! [1, 2, 3].any?

#=> true [1, 2, 3].include?(10) #=> false ! [1, 2, 3].reverse #=> [3, 2, 1] [3, 1, 2].sort #=> [1, 2, 3] ! [1, 2] + [3, 4] #=> [1, 2, 3, 4] ! for number in [1, 2, 3] # ... end

48. ### hash = { “Pero Perić” => “Matematika”, “Ana Anić” =>

“Biologija”, } ! hash[“Pero Perić”] #=> “Matematika” hash[“Ana Anić”] #=> “Biologija”
49. ### {“a” => “b”}.has_key?(“c”) #=> false {“a” => “b”}.has_value?(“b”) #=> true

! {“a” => “b”}.invert #=> {“b” => “a”} {“a” => “b”}.merge(“c” => “d”) #=> {“a” => “b”, “c” => “d”}

51. ### (1..10) (1..10).cover?(7) #=> true (1..10).cover?(11) #=> false ! (“a”..”z”) (“a”..”z”).cover?(“d”)

#=> true ! (xmas..easter).cover?(new_year) #=> true [“a”, “b”, “c”, “d”][1..2] #=> [“b”, “c”] “Some string”[2..6] #=> “me st”
52. ### if (percentage >= 90) 5; else if (percentage >= 75

&& percentage < 90) 4; else if (percentage >= 60 && percentage < 75) 3; else if (percentage >= 50 && percentage < 60) 2; else 1;
53. ### case percentage when 90..100 then 5 when 75..89 then 4

when 60..74 then 3 when 50..59 then 2 else 1 end

55. ### { print “Hello world” } ! 5.times { print “Hello

world” } ! { print “Hello world” } ! 5.times { print “Hello world” } { print “Hello world” } ! 5.times { print “Hello world” } ! do print “Hello world” end ! 5.times do print “Hello world” end
56. ### class Integer def times ! ! ! end end class

Integer def times(&block) ! ! ! end end class Integer def times(&block) for n in [1..self] ! end end end class Integer def times(&block) for n in [1..self] block.call end end end
57. ### class Integer def times(&block) for n in [1..self] block.call end

end end class Integer def times(&block) for n in [1..self] block.call(n) end end end 5.times do print “Hello world” print “Number of calls: #{…}” end 5.times do |n| print “Hello world” print “Number of calls: #{…}” end 5.times do |n| print “Hello world” print “Number of calls: #{n}” end
58. ### [1, 2, 3, 4, 5, 6, 7, 8] .select {

|n| n.even? } #=> [2, 4, 6, 8] .map { |n| n + 1 } #=> [3, 5, 7, 9] .reject { |n| n > 7 } #=> [3, 5, 7] .find { |n| n > 3 } #=> 5

60. ### module MyModule def my_module_method # ... end end ! MyModule.new

#=> Error class MyClass include MyModule end ! instance = MyClass.new instance.my_module_method
61. ### class Person ! ! ! ! ! end class Person

include Comparable ! ! ! ! end class Person include Comparable ! def <=>(other) self.surname <=> other.surname end end person1 <= person2 person1 < person2 person1 >= person2 person1 > person2 person1 == person2 person1 != person2
62. ### class Set ! ! ! ! ! end class Set

include Enumerable ! ! ! ! end class Set include Enumerable ! def each(&block) collection.each(&block) end end people = Set.new(array) ! people.any? people.find { |person| ... } people.include?(...) people.select { |person| ... }

64. ### [1, 2, 3].class #=> Array Array.superclass #=> Object ! “A

string”.methods #=> [:empty?, :length, :upcase, …] ! 1.23.respond_to?(:round) #=> true ! String.included_modules #=> [Comparable, …]