Let’s Talk About Ruby

449 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
449
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Let’s Talk About Ruby

  1. 1. LET’S TALK ABOUT ruby
  2. 2. Ian Bishop@ianbishop
  3. 3. Ruby is
  4. 4. Ruby issimple in appearance,
  5. 5. Ruby issimple in appearance,but is very complex inside,
  6. 6. Ruby issimple in appearance,but is very complex inside,just like our human body
  7. 7. Ruby issimple in appearance,but is very complex inside,just like our human body - Yukihiro “matz” Matsumoto
  8. 8. philosophy
  9. 9. principle ofleast surprise
  10. 10. principle of least surprisearray.lengthstring.length()collection.size()
  11. 11. principle of least surprisearray.lengthstring.length()collection.size()array.lengthstring.lengthcollection.length
  12. 12. features
  13. 13. everything is an object5.times do print “Everyone loves ruby!”end
  14. 14. everything is an object5.times
  15. 15. everything is a message/abc/ === “abc” “abc” === /abc/=> true => false
  16. 16. everything is a message/abc/ === “abc”=> true
  17. 17. everything is a message/abc//abc/
  18. 18. everything is a message/abc//abc/.send
  19. 19. everything is a message/abc/ ===/abc/.send(:===
  20. 20. everything is a message/abc/ === “abc”/abc/.send(:===, “abc”)
  21. 21. everything is a message/abc/ === “abc”=> true/abc/.send(:===, “abc”)=> true
  22. 22. everything is a message/abc/ === “abc” “abc” === /abc/=> true => false
  23. 23. everything is a messagecase “HELLO”when /^[a-z]*$/ “lowercase”when /^[A-Z]*$/ “uppercase”end=> “uppercase”
  24. 24. everything is a messagecase “HELLO”when “hello” “lowercase”when “HELLO” “uppercase”end=> “uppercase”
  25. 25. everything is a message/abc/ === “abc” “abc” === /abc/=> true => false
  26. 26. dynamic runtimea = [1,2,3]a.first=> 1a.secondNoMethodError: Undefined method‘second’ for [1, 2, 3]:Array
  27. 27. dynamic runtimeclass Array def second if self.length > 1 return self[1] end nil endend
  28. 28. dynamic runtimea = [1,2,3]a.first=> 1a.second
  29. 29. dynamic runtimea = [1,2,3]a.first=> 1a.second=> 2
  30. 30. DEALING WITH data
  31. 31. using collectionsa = [1,2,3]
  32. 32. using collectionsa = [1,2,3,“meow”]
  33. 33. using collectionsa.each do |x| puts xend123meow
  34. 34. using collectionsa.each
  35. 35. using collectionsa.each do |x|end
  36. 36. using collectionsa.each do |x| puts xend
  37. 37. using collectionsa.each do |x| puts xend123meow
  38. 38. .eachIterate over elements in a collection
  39. 39. .mapIterate over elements in a collection,returning a new collection ofelements of the same size
  40. 40. using mapa = [1,2,3]
  41. 41. using mapa = [1,2,3]a.map
  42. 42. using mapa = [1,2,3]a.map do |x|end
  43. 43. using mapa = [1,2,3]a.map do |x| 2 * xend
  44. 44. using mapa = [1,2,3]a.map do |x| return 2 * xend
  45. 45. using mapa = [1,2,3]a.map do |x| 2 * xend
  46. 46. using mapa = [1,2,3]a.map do |x| 2 * xend=> [2,4,6]
  47. 47. .mapIterate over elements in a collection,returning a new collection ofelements of the same size
  48. 48. .selectIterate over elements in a collection,returning elements which match aspecified criteria
  49. 49. using selecta = [1,2,3]
  50. 50. using selecta = [1,2,3]a.select
  51. 51. using selecta = [1,2,3]a.select do |x|end
  52. 52. using selecta = [1,2,3]a.select do |x| x.odd?end
  53. 53. using selecta = [1,2,3]a.select do |x| x.odd?end=> [1,3]
  54. 54. .selectIterate over elements in a collection,returning elements which match aspecified criteria
  55. 55. .reduceCombines all elements in a collectionusing a binary operation, returningan accumulator value.
  56. 56. using reducea = [1,2,3]
  57. 57. using reducea = (1..100)
  58. 58. using reducea = (1..100)a.reduce
  59. 59. using reducea = (1..100)a.reduce do |sum, x|end
  60. 60. using reducea = (1..100)a.reduce do |sum, x| sum + xend
  61. 61. using reducea = (1..100)a.reduce do |sum, x| sum + xend=> 5050
  62. 62. .reduceCombines all elements in a collectionusing a binary operation, returningan accumulator value.
  63. 63. .reduceCombines all elements in a collectionusing a binary operation, returningan accumulator value.
  64. 64. using reduce (again)a = (1..100)a.reduce
  65. 65. using reduce (again)a = (1..100)a.reduce(:*)
  66. 66. using reduce (again)a = (1..100)a.reduce(:*)=>933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000…
  67. 67. SOLVING HARDERPROBLEMS
  68. 68. generating poker hands
  69. 69. building a deck of cardssuits = %w(S C H D)
  70. 70. building a deck of cardssuits = %w(S C H D)=> [“S”, “C”, “H”, “D”]
  71. 71. building a deck of cardssuits = %w(S C H D)=> [“S”, “C”, “H”, “D”]“S C H D”.split /s+/=> [“S”, “C”, “H”, “D”]
  72. 72. building a deck of cardssuits = %w(S C H D)faces =
  73. 73. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a
  74. 74. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)
  75. 75. [1, 2, 3][“a”, “b”, “c”]
  76. 76. [1, 2, 3] x[“a”, “b”, “c”]
  77. 77. cross product[1, “a”], [1, “b”], [1, “c”],[2, “a”], [2, “b”], [2, “c”],[3, “a”], [3, “b”], [3, “c”]
  78. 78. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)
  79. 79. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)=> [[“S”, 2], [“S”, 3], …, [“S”, “A”],[“C”, 1], …, [“C”, “A”], …]
  80. 80. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)=> [“S2”, “S3”, …, “SA”, “C1”, …, “C2”,…]
  81. 81. join(sep=$,) -> strReturns a string created by converting each elementof the array to a string, seperated by sep. [ “a”, “b”, “c”].join => “abc” [ “a”, “b”, “c”].join(“-”) => “a-b-c”
  82. 82. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)deck = deck.map do |pair| pair.join end
  83. 83. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)deck.map! do |pair| pair.joinend
  84. 84. building a deck of cards .map!
  85. 85. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)deck.map! do |pair| pair.joinend
  86. 86. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces)deck.map! { |pair| pair.join }
  87. 87. building a deck of cardssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces).map(&:join)
  88. 88. generating poker handssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces).map(&:join)
  89. 89. sample(n) -> new_aryChoose n random elements from the array.The elements are chosen by using randomand unique indices in order to ensure that anelement doesn’t repeat itself unless the arrayalready contained duplicate elements.
  90. 90. generating poker handssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces).map(&:join)deck.sample(5)
  91. 91. generating poker handssuits = %w(S C H D)faces = (2..10).to_a + %w(J Q K A)deck = suits.product(faces).map(&:join)deck.sample(5)=> [“C2”, “D5”, “S7”, “D8”, “C8”]
  92. 92. LET’S TALK ABOUT MAP REDUCE
  93. 93. LET’S TALK ABOUT © MAP REDUCE
  94. 94. Count of URL Access FrequencyThe map function processes logs of web pagerequests and outputs <URL, 1>. The reducefunction adds together all values for the sameURL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  95. 95. counting url access frequencylog
  96. 96. counting url access frequencylog=> [“example.com”, “google.com”, “userevents.com”, “unb.ca”, “frederictonug.net”, ..]
  97. 97. Count of URL Access FrequencyThe map function processes logs of web pagerequests and outputs <URL, 1>. The reducefunction adds together all values for the sameURL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  98. 98. generate count pairscount_pairs = log.map do |url| [url, 1]end
  99. 99. Count of URL Access FrequencyThe map function processes logs of web pagerequests and outputs <URL, 1>. The reducefunction adds together all values for the same URLand emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  100. 100. BUT FIRST
  101. 101. Collection of key-valuepairs.Similar to an array, exceptindexing is done via uniquekeys.
  102. 102. brief introduction to hashesmy_hash = { :abc => 5, “def” => 9 }
  103. 103. brief introduction to hashesmy_hash = { :abc => 5, “def” => 9 }my_hash[:abc]
  104. 104. brief introduction to hashesmy_hash = { :abc => 5, “def” => 9 }my_hash[:abc]=> 5
  105. 105. brief introduction to hashesmy_hash = { :abc => 5, “def” => 9 }my_hash[:abc]=> 5my_hash[“def”] = 14
  106. 106. brief introduction to hashesmy_hash = { :abc => 5, “def” => 9 }my_hash[:abc]=> 5my_hash[“def”] = 14=> { :abc => 5, “def” => 14 }
  107. 107. Count of URL Access FrequencyThe map function processes logs of web pagerequests and outputs <URL, 1>. The reducefunction adds together all values for the same URLand emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  108. 108. combining count pairscount_pairs.reduce
  109. 109. combining count pairscount_pairs.reduce({})
  110. 110. combining count pairscount_pairs.reduce({}) do |hash, pair|end
  111. 111. combining count pairscount_pairs.reduce({}) do |hash, pair| url, count = pairend
  112. 112. combining count pairscount_pairs.reduce({}) do |hash, pair| url, count = pair if hash.has_key? url hash[url] += countend
  113. 113. combining count pairscount_pairs.reduce({}) do |hash, pair| url, count = pair if hash.has_key? url hash[url] += count else hash[url] = count endend
  114. 114. combining count pairscount_pairs.reduce({}) do |hash, pair| url, count = pair if hash.has_key? url hash[url] += count else hash[url] = count end hashend
  115. 115. counting url access frequencylog = [“example.com”, “google.com”,“example.com”, “unb.ca”]
  116. 116. counting url access frequencylog = [“example.com”, “google.com”,“example.com”, “unb.ca”]=> { “example.com” => 2, “google.com” => 1, “unb.ca” => 1 }

×