Slide 1

Slide 1 text

LET’S TALK ABOUT ruby

Slide 2

Slide 2 text

Ian Bishop @ianbishop

Slide 3

Slide 3 text

Ruby is

Slide 4

Slide 4 text

Ruby is simple in appearance,

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Ruby is simple in appearance, but is very complex inside, just like our human body

Slide 7

Slide 7 text

Ruby is simple in appearance, but is very complex inside, just like our human body - Yukihiro “matz” Matsumoto

Slide 8

Slide 8 text

philosophy

Slide 9

Slide 9 text

principle of least surprise

Slide 10

Slide 10 text

principle of least surprise array.length string.length() collection.size()

Slide 11

Slide 11 text

principle of least surprise array.length string.length() collection.size() array.length string.length collection.length

Slide 12

Slide 12 text

features

Slide 13

Slide 13 text

everything is an object 5.times do print “Everyone loves ruby!” end

Slide 14

Slide 14 text

everything is an object 5.times

Slide 15

Slide 15 text

everything is a message /abc/ === “abc” => true “abc” === /abc/ => false

Slide 16

Slide 16 text

everything is a message /abc/ === “abc” => true

Slide 17

Slide 17 text

everything is a message /abc/ /abc/

Slide 18

Slide 18 text

everything is a message /abc/ /abc/.send

Slide 19

Slide 19 text

everything is a message /abc/ === /abc/.send(:===

Slide 20

Slide 20 text

everything is a message /abc/ === “abc” /abc/.send(:===, “abc”)

Slide 21

Slide 21 text

everything is a message /abc/ === “abc” => true /abc/.send(:===, “abc”) => true

Slide 22

Slide 22 text

everything is a message /abc/ === “abc” => true “abc” === /abc/ => false

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

everything is a message case “HELLO” when “hello” “lowercase” when “HELLO” “uppercase” end => “uppercase”

Slide 25

Slide 25 text

everything is a message /abc/ === “abc” => true “abc” === /abc/ => false

Slide 26

Slide 26 text

dynamic runtime a = [1,2,3] a.first => 1 a.second NoMethodError: Undefined method ‘second’ for [1, 2, 3]:Array

Slide 27

Slide 27 text

dynamic runtime class Array def second if self.length > 1 return self[1] end nil end end

Slide 28

Slide 28 text

dynamic runtime a = [1,2,3] a.first => 1 a.second

Slide 29

Slide 29 text

dynamic runtime a = [1,2,3] a.first => 1 a.second => 2

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

DEALING WITH data

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

using collections a = [1,2,3]

Slide 35

Slide 35 text

using collections a = [1,2,3,“meow”]

Slide 36

Slide 36 text

using collections a.each do |x| puts x end 1 2 3 meow

Slide 37

Slide 37 text

using collections a.each

Slide 38

Slide 38 text

using collections a.each do |x| end

Slide 39

Slide 39 text

using collections a.each do |x| puts x end

Slide 40

Slide 40 text

using collections a.each do |x| puts x end 1 2 3 meow

Slide 41

Slide 41 text

.each Iterate over elements in a collection

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

using map a = [1,2,3]

Slide 44

Slide 44 text

using map a = [1,2,3] a.map

Slide 45

Slide 45 text

using map a = [1,2,3] a.map do |x| end

Slide 46

Slide 46 text

using map a = [1,2,3] a.map do |x| 2 * x end

Slide 47

Slide 47 text

using map a = [1,2,3] a.map do |x| return 2 * x end

Slide 48

Slide 48 text

using map a = [1,2,3] a.map do |x| 2 * x end

Slide 49

Slide 49 text

using map a = [1,2,3] a.map do |x| 2 * x end => [2,4,6]

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

.select Iterate over elements in a collection, returning elements which match a specified criteria

Slide 52

Slide 52 text

using select a = [1,2,3]

Slide 53

Slide 53 text

using select a = [1,2,3] a.select

Slide 54

Slide 54 text

using select a = [1,2,3] a.select do |x| end

Slide 55

Slide 55 text

using select a = [1,2,3] a.select do |x| x.odd? end

Slide 56

Slide 56 text

using select a = [1,2,3] a.select do |x| x.odd? end => [1,3]

Slide 57

Slide 57 text

.select Iterate over elements in a collection, returning elements which match a specified criteria

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

using reduce a = [1,2,3]

Slide 60

Slide 60 text

using reduce a = (1..100)

Slide 61

Slide 61 text

using reduce a = (1..100) a.reduce

Slide 62

Slide 62 text

using reduce a = (1..100) a.reduce do |sum, x| end

Slide 63

Slide 63 text

using reduce a = (1..100) a.reduce do |sum, x| sum + x end

Slide 64

Slide 64 text

using reduce a = (1..100) a.reduce do |sum, x| sum + x end => 5050

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

using reduce (again) a = (1..100) a.reduce

Slide 68

Slide 68 text

using reduce (again) a = (1..100) a.reduce(:*)

Slide 69

Slide 69 text

using reduce (again) a = (1..100) a.reduce(:*) =>9332621544394415268169923885626670049 071596826438162146859296389521759999322 991560894146397615651828625369792082722 37582511852109168640000000000000…

Slide 70

Slide 70 text

SOLVING HARDER PROBLEMS

Slide 71

Slide 71 text

generating poker hands

Slide 72

Slide 72 text

building a deck of cards suits = %w(S C H D)

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

building a deck of cards suits = %w(S C H D) faces =

Slide 76

Slide 76 text

building a deck of cards suits = %w(S C H D) faces = (2..10).to_a

Slide 77

Slide 77 text

building a deck of cards suits = %w(S C H D) faces = (2..10).to_a + %w(J Q K A)

Slide 78

Slide 78 text

[1, 2, 3] [“a”, “b”, “c”]

Slide 79

Slide 79 text

[1, 2, 3] x [“a”, “b”, “c”]

Slide 80

Slide 80 text

[1, “a”], [1, “b”], [1, “c”], [2, “a”], [2, “b”], [2, “c”], [3, “a”], [3, “b”], [3, “c”] cross product

Slide 81

Slide 81 text

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)

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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”

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

building a deck of cards .map!

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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 }

Slide 90

Slide 90 text

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)

Slide 91

Slide 91 text

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)

Slide 92

Slide 92 text

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.

Slide 93

Slide 93 text

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)

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

LET’S TALK ABOUT MAP REDUCE

Slide 96

Slide 96 text

LET’S TALK ABOUT MAP REDUCE ©

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

counting url access frequency log

Slide 99

Slide 99 text

counting url access frequency log => [“example.com”, “google.com”, “userevents.com”, “unb.ca”, “frederictonug.net”, ..]

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

generate count pairs count_pairs = log.map do |url| [url, 1] end

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

BUT FIRST

Slide 104

Slide 104 text

Collection of key-value pairs. Similar to an array, except indexing is done via unique keys.

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

combining count pairs count_pairs.reduce

Slide 112

Slide 112 text

combining count pairs count_pairs.reduce({})

Slide 113

Slide 113 text

combining count pairs count_pairs.reduce({}) do |hash, pair| end

Slide 114

Slide 114 text

combining count pairs count_pairs.reduce({}) do |hash, pair| url, count = pair end

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

counting url access frequency log = [“example.com”, “google.com”, “example.com”, “unb.ca”]

Slide 119

Slide 119 text

counting url access frequency log = [“example.com”, “google.com”, “example.com”, “unb.ca”] => { “example.com” => 2, “google.com” => 1, “unb.ca” => 1 }