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

Let's Talk About Ruby

Ian Bishop
October 24, 2012

Let's Talk About Ruby

An introductory talk given about Ruby for the Fredericton Developers User Group on October 24th, 2012.

Ian Bishop

October 24, 2012
Tweet

More Decks by Ian Bishop

Other Decks in Programming

Transcript

  1. Ruby is simple in appearance, but is very complex inside,

    just like our human body - Yukihiro “matz” Matsumoto
  2. everything is a message case “HELLO” when /^[a-z]*$/ “lowercase” when

    /^[A-Z]*$/ “uppercase” end => “uppercase”
  3. everything is a message case “HELLO” when “hello” “lowercase” when

    “HELLO” “uppercase” end => “uppercase”
  4. dynamic runtime a = [1,2,3] a.first => 1 a.second NoMethodError:

    Undefined method ‘second’ for [1, 2, 3]:Array
  5. .map Iterate over elements in a collection, returning a new

    collection of elements of the same size
  6. .map Iterate over elements in a collection, returning a new

    collection of elements of the same size
  7. .reduce Combines all elements in a collection using a binary

    operation, returning an accumulator value.
  8. .reduce Combines all elements in a collection using a binary

    operation, returning an accumulator value.
  9. .reduce Combines all elements in a collection using a binary

    operation, returning an accumulator value.
  10. building a deck of cards suits = %w(S C H

    D) => [“S”, “C”, “H”, “D”]
  11. building a deck of cards suits = %w(S C H

    D) => [“S”, “C”, “H”, “D”] “S C H D”.split /\s+/ => [“S”, “C”, “H”, “D”]
  12. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A)
  13. [1, “a”], [1, “b”], [1, “c”], [2, “a”], [2, “b”],

    [2, “c”], [3, “a”], [3, “b”], [3, “c”] cross product
  14. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces)
  15. building a deck of cards suits = %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”], …]
  16. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces) => [“S2”, “S3”, …, “SA”, “C1”, …, “C2”, …]
  17. join(sep=$,) -> str Returns a string created by converting each

    element of the array to a string, seperated by sep. [ “a”, “b”, “c”].join => “abc” [ “a”, “b”, “c”].join(“-”) => “a-b-c”
  18. building a deck of cards suits = %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
  19. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces) deck.map! do |pair| pair.join end
  20. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces) deck.map! do |pair| pair.join end
  21. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces) deck.map! { |pair| pair.join }
  22. building a deck of cards suits = %w(S C H

    D) faces = (2..10).to_a + %w(J Q K A) deck = suits.product(faces).map(&:join)
  23. generating poker hands suits = %w(S C H D) faces

    = (2..10).to_a + %w(J Q K A) deck = suits.product(faces).map(&:join)
  24. sample(n) -> new_ary Choose n random elements from the array.

    The elements are chosen by using random and unique indices in order to ensure that an element doesn’t repeat itself unless the array already contained duplicate elements.
  25. generating poker hands suits = %w(S C H D) faces

    = (2..10).to_a + %w(J Q K A) deck = suits.product(faces).map(&:join) deck.sample(5)
  26. generating poker hands suits = %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”]
  27. Count of URL Access Frequency The map function processes logs

    of web page requests and outputs <URL, 1>. The reduce function adds together all values for the same URL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  28. Count of URL Access Frequency The map function processes logs

    of web page requests and outputs <URL, 1>. The reduce function adds together all values for the same URL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  29. Count of URL Access Frequency The map function processes logs

    of web page requests and outputs <URL, 1>. The reduce function adds together all values for the same URL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  30. brief introduction to hashes my_hash = { :abc => 5,

    “def” => 9 } my_hash[:abc] => 5
  31. brief introduction to hashes my_hash = { :abc => 5,

    “def” => 9 } my_hash[:abc] => 5 my_hash[“def”] = 14
  32. brief introduction to hashes my_hash = { :abc => 5,

    “def” => 9 } my_hash[:abc] => 5 my_hash[“def”] = 14 => { :abc => 5, “def” => 14 }
  33. Count of URL Access Frequency The map function processes logs

    of web page requests and outputs <URL, 1>. The reduce function adds together all values for the same URL and emits a <URL, total count> pair. from Introduction to Parallel Programming and MapReduce (Google)
  34. combining count pairs count_pairs.reduce({}) do |hash, pair| url, count =

    pair if hash.has_key? url hash[url] += count end
  35. combining count pairs count_pairs.reduce({}) do |hash, pair| url, count =

    pair if hash.has_key? url hash[url] += count else hash[url] = count end end
  36. combining count pairs count_pairs.reduce({}) do |hash, pair| url, count =

    pair if hash.has_key? url hash[url] += count else hash[url] = count end hash end
  37. counting url access frequency log = [“example.com”, “google.com”, “example.com”, “unb.ca”]

    => { “example.com” => 2, “google.com” => 1, “unb.ca” => 1 }