$30 off During Our Annual Pro Sale. View Details »

Introduction to Ruby

Introduction to Ruby

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

Janko Marohnić

April 01, 2014
Tweet

More Decks by Janko Marohnić

Other Decks in Programming

Transcript

  1. Ruby

    View Slide

  2. #include
    !
    int main() {
    printf("Hello world!");
    }
    C C++ Java C#

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. print “Hello world!”
    Ruby

    View Slide

  7. Nastanak
    Yukihiro
    Matsumoto
    Smalltalk
    Perl

    View Slide

  8. Ruby
    • Viši programski jezik

    View Slide

  9. C
    int array[] = {1, 2, 3, 4, 5, 6},
    sum = 0;
    !
    for (int idx = 0; idx < 6; idx++) {
    sum += array[idx];
    }

    View Slide

  10. (1..6).inject(:+)
    Ruby

    View Slide

  11. Ruby
    • Viši programski jezik
    • Interpretirani

    View Slide

  12. C/C++
    #include
    #include
    !
    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

    View Slide

  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

    View Slide

  14. Ruby
    • Viši programski jezik
    • Interpretirani
    • Objektno-orijentiran

    View Slide

  15. C++ – hibrid
    string(“Hello”).size(); //=> 5
    !
    "Hello".size(); //=> ?
    // error: member reference base
    type 'const char [6]' is not a
    structure or union

    View Slide

  16. Ruby
    String.new(“Hello”).size #=> 5
    !
    “Hello".size #=> 5
    “Hello” String.new(“Hello”)

    View Slide

  17. Ruby
    • Viši programski jezik
    • Interpretirani
    • Objektno-orjentiran
    • Dinamički
    • Garbage collector

    View Slide

  18. C/C++
    int* array = (int*)malloc(...);
    !
    // Akcije...
    !
    free(array);
    class MyClass {
    ~MyClass() { ... }
    };

    View Slide

  19. Generalno

    View Slide

  20. Object
    String
    Integer
    Float
    Time Array Hash
    Numeric

    View Slide

  21. Nema primitivnih tipova
    "A string".class #=> String
    1234598.class #=> Integer
    3.14.class #=> Float
    [1, 2, 3].class #=> Array

    View Slide

  22. Metode

    View Slide

  23. C++
    class Converter {
    double degree_to_farenheit(int degree) {
    // ...
    }
    };
    Converter converter;
    converter.degree_to_farenheit(30);

    View Slide

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

    View Slide

  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… {
    // ...
    }
    }

    View Slide

  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… {
    // ...
    }
    }

    View Slide

  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

    View Slide

  28. “String”.empty?() #=> false
    Konvencije imenovanja
    “String”.empty? #=> false
    !
    post.delete if user.moderator?
    str = “String”
    str.upcase!
    str #=> “STRING”

    View Slide

  29. class Array
    def first
    self[0]
    end
    end
    Proširenje klasa
    [1, 2, 3].first #=> 1

    View Slide

  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

    View Slide

  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

    View Slide

  32. class Shirt
    def
    end
    !
    def
    end
    !
    def
    end
    !
    def
    end
    end
    s
    m
    l
    xl
    Metaprogramiranje

    View Slide

  33. class Shirt
    for size in [“ ”, “ ”, “ ”, “ ”]
    define_method(size) do
    # ...
    end
    end
    end
    Metaprogramiranje
    s m l xl

    View Slide

  34. Tipovi

    View Slide

  35. Numeric
    Integer Complex Rational
    Float

    View Slide

  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 * π

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  40. #include
    #include
    using namespace std;
    !
    int main() {
    cout << LLONG_MAX << endl;
    cout << LLONG_MAX + 1 << endl;
    }
    9223372036854775807
    -9223372036854775808

    View Slide

  41. 4611686018427387903
    4611686018427387904
    max_integer = 2 ** 62 - 1
    !
    print max_integer
    print max_integer + 1
    4611686018427387903.class #=> Fixnum
    4611686018427387904.class #=> Bignum

    View Slide

  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

    View Slide

  43. String

    View Slide

  44. “String”.empty? #=> false
    “String”.include?(“ri”) #=> true
    !
    “String”.downcase #=> “string”
    “String”.reverse #=> “gnirtS”
    !
    “%d %s” % [var1, var2]
    “#{var1} #{var2}”

    View Slide

  45. Array

    View Slide

  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

    View Slide

  47. Hash

    View Slide

  48. hash = {
    “Pero Perić” => “Matematika”,
    “Ana Anić” => “Biologija”,
    }
    !
    hash[“Pero Perić”] #=> “Matematika”
    hash[“Ana Anić”] #=> “Biologija”

    View Slide

  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”}

    View Slide

  50. Range

    View Slide

  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”

    View Slide

  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;

    View Slide

  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

    View Slide

  54. Blokovi

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  59. Moduli

    View Slide

  60. module MyModule
    def my_module_method
    # ...
    end
    end
    !
    MyModule.new #=> Error
    class MyClass
    include MyModule
    end
    !
    instance = MyClass.new
    instance.my_module_method

    View Slide

  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

    View Slide

  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| ... }

    View Slide

  63. Introspekcija

    View Slide

  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, …]

    View Slide

  65. View Slide