Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Ruby

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Introduction to Ruby

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

Avatar for Janko Marohnić

Janko Marohnić

April 01, 2014
Tweet

More Decks by Janko Marohnić

Other Decks in Programming

Transcript

  1. public class HelloWorld { public static void main(String[] args) {

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

    System.Console.Write(“Hello world!”); } } C C++ Java C#
  3. C int array[] = {1, 2, 3, 4, 5, 6},

    sum = 0; ! for (int idx = 0; idx < 6; idx++) { sum += array[idx]; }
  4. 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
  5. Ruby def factorial(n) result = 1 for i in [1..n]

    result *= i end result end ! f = factorial(3) print "3! = #{f}" 3! = 6
  6. C++ – hibrid string(“Hello”).size(); //=> 5 ! "Hello".size(); //=> ?

    // error: member reference base type 'const char [6]' is not a structure or union
  7. C++ class Converter { double degree_to_farenheit(int degree) { // ...

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

    } }; Converter converter; converter.DegreeToFarenheit(30);
  9. 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… { // ... } }
  10. 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… { // ... } }
  11. 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
  12. “String”.empty?() #=> false Konvencije imenovanja “String”.empty? #=> false ! post.delete

    if user.moderator? str = “String” str.upcase! str #=> “STRING”
  13. 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
  14. 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
  15. class Shirt def end ! def end ! def end

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

    ”, “ ”] define_method(size) do # ... end end end Metaprogramiranje s m l xl
  17. 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 * π
  18. 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
  19. 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
  20. 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
  21. #include <iostream> #include <limits.h> using namespace std; ! int main()

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

    print max_integer print max_integer + 1 4611686018427387903.class #=> Fixnum 4611686018427387904.class #=> Bignum
  23. 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
  24. “String”.empty? #=> false “String”.include?(“ri”) #=> true ! “String”.downcase #=> “string”

    “String”.reverse #=> “gnirtS” ! “%d %s” % [var1, var2] “#{var1} #{var2}”
  25. [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
  26. hash = { “Pero Perić” => “Matematika”, “Ana Anić” =>

    “Biologija”, } ! hash[“Pero Perić”] #=> “Matematika” hash[“Ana Anić”] #=> “Biologija”
  27. {“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”}
  28. (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”
  29. 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;
  30. 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
  31. { 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
  32. 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
  33. 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
  34. [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
  35. module MyModule def my_module_method # ... end end ! MyModule.new

    #=> Error class MyClass include MyModule end ! instance = MyClass.new instance.my_module_method
  36. 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
  37. 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| ... }
  38. [1, 2, 3].class #=> Array Array.superclass #=> Object ! “A

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