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

Le mani su Ruby

weLaika
September 30, 2013

Le mani su Ruby

Il primo appuntamento del Torino Coding Society. Si parte con una introduzione a Ruby, al linguaggio e alla sua filosofia

weLaika

September 30, 2013
Tweet

More Decks by weLaika

Other Decks in Programming

Transcript

  1. RUBY W H A T I S A programming language,

    written by a Japanese guy in 1995.
  2. Why? “For me, the purpose of life is, at least

    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.”
  3. How? Ruby takes its origins from Perl, Smalltalk and Lisp.

    Matz tried to make the language natural, not simple. “Ruby is simple in appearance, but is very complex inside, just like our human body.”
  4. Hello Torino! def hello(name = "Torino") puts "Hello #{name}!" end

    hello # Hello Torino! hello "Matz" # Hello Matz!
  5. Strings string = "ciao" puts string # ciao puts string.reverse

    # oaic puts string.capitalize # Ciao puts string.upcase # CIAO
  6. Strings string = "ciao" puts string # ciao puts string.reverse

    # oaic puts string.capitalize # Ciao puts string.upcase # CIAO
  7. Strings string = "ciao" puts string # ciao puts string.reverse

    # oaic puts string.capitalize # Ciao puts string.upcase # CIAO
  8. Strings string = "ciao" puts string # ciao puts string.reverse

    # oaic puts string.capitalize # Ciao puts string.upcase # CIAO
  9. Strings string = "ciao" puts string # ciao puts string.reverse

    # oaic puts string.capitalize # Ciao puts string.upcase # CIAO
  10. Arrays array.push "one" array << "two" array += ["three", "four"]

    p array # ["one", "two", "three", "four"] array = []
  11. Hashes hash["one"] = 1 hash["two"] = 2 hash["three"] = 3

    p hash # {"one"=>1, "two"=>2, "three"=>3} hash = {}
  12. Ranges numbers = 1..10 p numbers.to_a # [1, 2, 3,

    4, 5, 6, 7, 8, 9, 10] numbers = 1...10 p numbers.to_a # [1, 2, 3, 4, 5, 6, 7, 8, 9] characters = 'a'..'f' p characters.to_a # ["a", "b", "c", "d", "e", "f"]
  13. Ranges numbers = 1..10 p numbers.to_a # [1, 2, 3,

    4, 5, 6, 7, 8, 9, 10] numbers = 1...10 p numbers.to_a # [1, 2, 3, 4, 5, 6, 7, 8, 9] characters = 'a'..'f' p characters.to_a # ["a", "b", "c", "d", "e", "f"]
  14. Ranges numbers = 1..10 p numbers.to_a # [1, 2, 3,

    4, 5, 6, 7, 8, 9, 10] numbers = 1...10 p numbers.to_a # [1, 2, 3, 4, 5, 6, 7, 8, 9] characters = 'a'..'f' p characters.to_a # ["a", "b", "c", "d", "e", "f"]
  15. Inline Conditions score = 100_000_000 if score > 9000 puts

    "score is over 9000!!" end # score is over 9000!!
  16. Inline Conditions score = 100_000_000 if score > 9000 puts

    "score is over 9000!!" end # score is over 9000!! puts "score is over 9000!!" if score > 9000 # score is over 9000!!
  17. Loops in Javascript for (var i=0; i<array.length; i++) { console.log("Adding

    " + array[i] + "..."); } // Adding coffee... // Adding ice... // Adding sugar... var array = ["coffee", "ice", "sugar"];
  18. Loops in Ruby array.each do |element| puts "Adding #{element}..." end

    # Adding coffee... # Adding ice... # Adding sugar... array = ["coffee", "ice", "sugar"]
  19. Loops in Javascript for (key in person) { console.log(key +

    " is " + person[key]); } // first_name is John // last_name is Doe // age is 25 var person = { first_name: "John", last_name: "Doe", age: 25 };
  20. Loops in Ruby person.each do |key, value| puts "#{key} is

    #{value}" end # first_name is John # last_name is Doe # age is 25 person = { first_name: "John", last_name: "Doe", age: 25 }
  21. Is this the real life, or is this just fantasy?

    puts "nil" if nil puts "false" if false puts "true" if true puts "0" if 0 puts "1" if 1
  22. Is this the real life, or is this just fantasy?

    puts "nil" if nil puts "false" if false puts "true" if true puts "0" if 0 puts "1" if 1 # # # true # 0 # 1
  23. Let’s say we want to add a yahoo method to

    every string in our application. Open Classes
  24. Open Classes class String def yahoo "#{self} YAHOOOOO" end end

    puts "Ruby".yahoo # Ruby YAHOOOOO puts "Ruby".yahoo.yahoo # Ruby YAHOOOOO YAHOOOOO
  25. You can shoot yourself in your foot. Ruby gives you

    the freedom to do that. Ruby thinks you are clever enough to not shoot yourself in the foot. From Great Power..
  26. Closures in Ruby, aka Blocks posts = ["first", "second", "third"]

    posts.each do |post| puts post.upcase end # FIRST # SECOND # THIRD
  27. Closures in Ruby, aka Blocks posts = ["first", "second", "third"]

    posts.each do |post| puts post.capitalize * 2 end # FirstFirst # SecondSecond # ThirdThird
  28. Closures in Ruby, aka Blocks numbers = 1..100 numbers.select do

    |num| num.odd? end.reduce do |total, num| total + num end # 2500
  29. Closures in Ruby, aka Blocks numbers = 1..100 numbers.select do

    |num| num.even? end.reduce(1) do |total, num| total * num end # 3424322470251197624824643289520818597511 8675053719198827915654463488000000000000
  30. Blocks are very flexible and dynamic. A lot of Ruby

    libraries use blocks for this reason. Ruby on Rails use them too! Blocks are good