119
LET’S TALK ABOUT ruby

Let’s Talk About Ruby

Embed Size (px)

Citation preview

Page 1: Let’s Talk About Ruby

LET’S TALK ABOUT

ruby

Page 2: Let’s Talk About Ruby

Ian Bishop @ianbishop

Page 3: Let’s Talk About Ruby

Ruby is

Page 4: Let’s Talk About Ruby

Ruby is

simple in appearance,

Page 5: Let’s Talk About Ruby

Ruby is

simple in appearance,

but is very complex inside,

Page 6: Let’s Talk About Ruby

Ruby is

simple in appearance,

but is very complex inside,

just like our human body

Page 7: Let’s Talk About Ruby

Ruby is

simple in appearance,

but is very complex inside,

just like our human body - Yukihiro “matz” Matsumoto

Page 8: Let’s Talk About Ruby

philosophy

Page 9: Let’s Talk About Ruby

principle of

least surprise

Page 10: Let’s Talk About Ruby

principle of least surprise

array.length string.length() collection.size()

Page 11: Let’s Talk About Ruby

principle of least surprise

array.length string.length() collection.size() array.length string.length collection.length

Page 12: Let’s Talk About Ruby

features

Page 13: Let’s Talk About Ruby

everything is an object

5.times do print “Everyone loves ruby!” end

Page 14: Let’s Talk About Ruby

everything is an object

5.times

Page 15: Let’s Talk About Ruby

everything is a message

/abc/ === “abc” => true

“abc” === /abc/ => false

Page 16: Let’s Talk About Ruby

everything is a message

/abc/ === “abc” => true

Page 17: Let’s Talk About Ruby

everything is a message

/abc/

/abc/

Page 18: Let’s Talk About Ruby

everything is a message

/abc/

/abc/.send

Page 19: Let’s Talk About Ruby

everything is a message

/abc/ ===

/abc/.send(:===

Page 20: Let’s Talk About Ruby

everything is a message

/abc/ === “abc”

/abc/.send(:===, “abc”)

Page 21: Let’s Talk About Ruby

everything is a message

/abc/ === “abc” => true /abc/.send(:===, “abc”) => true

Page 22: Let’s Talk About Ruby

everything is a message

/abc/ === “abc” => true

“abc” === /abc/ => false

Page 23: Let’s Talk About Ruby

everything is a message

case “HELLO” when /^[a-z]*$/ “lowercase” when /^[A-Z]*$/ “uppercase” end

=> “uppercase”

Page 24: Let’s Talk About Ruby

everything is a message

case “HELLO” when “hello” “lowercase” when “HELLO” “uppercase” end

=> “uppercase”

Page 25: Let’s Talk About Ruby

everything is a message

/abc/ === “abc” => true

“abc” === /abc/ => false

Page 26: Let’s Talk About Ruby

dynamic runtime

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

NoMethodError: Undefined method ‘second’ for [1, 2, 3]:Array

Page 27: Let’s Talk About Ruby

dynamic runtime

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

Page 28: Let’s Talk About Ruby

dynamic runtime

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

Page 29: Let’s Talk About Ruby

dynamic runtime

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

Page 30: Let’s Talk About Ruby
Page 31: Let’s Talk About Ruby

DEALING WITH

data

Page 32: Let’s Talk About Ruby
Page 33: Let’s Talk About Ruby
Page 34: Let’s Talk About Ruby

using collections

a = [1,2,3]

Page 35: Let’s Talk About Ruby

using collections

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

Page 36: Let’s Talk About Ruby

using collections

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

Page 37: Let’s Talk About Ruby

using collections

a.each

Page 38: Let’s Talk About Ruby

using collections

a.each do |x|

end

Page 39: Let’s Talk About Ruby

using collections

a.each do |x| puts x

end

Page 40: Let’s Talk About Ruby

using collections

a.each do |x| puts x

end 1 2 3 meow

Page 41: Let’s Talk About Ruby

.each

Iterate over elements in a collection

Page 42: Let’s Talk About Ruby

.map

Iterate over elements in a collection,

returning a new collection of

elements of the same size

Page 43: Let’s Talk About Ruby

using map

a = [1,2,3]

Page 44: Let’s Talk About Ruby

using map

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

Page 45: Let’s Talk About Ruby

using map

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

end

Page 46: Let’s Talk About Ruby

using map

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

2 * x end

Page 47: Let’s Talk About Ruby

using map

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

return 2 * x end

Page 48: Let’s Talk About Ruby

using map

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

2 * x end

Page 49: Let’s Talk About Ruby

using map

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

2 * x end => [2,4,6]

Page 50: Let’s Talk About Ruby

.map

Iterate over elements in a collection,

returning a new collection of

elements of the same size

Page 51: Let’s Talk About Ruby

.select

Iterate over elements in a collection,

returning elements which match a

specified criteria

Page 52: Let’s Talk About Ruby

using select

a = [1,2,3]

Page 53: Let’s Talk About Ruby

using select

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

Page 54: Let’s Talk About Ruby

using select

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

end

Page 55: Let’s Talk About Ruby

using select

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

x.odd? end

Page 56: Let’s Talk About Ruby

using select

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

x.odd? end => [1,3]

Page 57: Let’s Talk About Ruby

.select

Iterate over elements in a collection,

returning elements which match a

specified criteria

Page 58: Let’s Talk About Ruby

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

Page 59: Let’s Talk About Ruby

using reduce

a = [1,2,3]

Page 60: Let’s Talk About Ruby

using reduce

a = (1..100)

Page 61: Let’s Talk About Ruby

using reduce

a = (1..100) a.reduce

Page 62: Let’s Talk About Ruby

using reduce

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

end

Page 63: Let’s Talk About Ruby

using reduce

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

sum + x end

Page 64: Let’s Talk About Ruby

using reduce

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

sum + x end => 5050

Page 65: Let’s Talk About Ruby

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

Page 66: Let’s Talk About Ruby

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

Page 67: Let’s Talk About Ruby

using reduce (again)

a = (1..100) a.reduce

Page 68: Let’s Talk About Ruby

using reduce (again)

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

Page 69: Let’s Talk About Ruby

using reduce (again)

a = (1..100) a.reduce(:*) =>933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000…

Page 70: Let’s Talk About Ruby

SOLVING

HARDER

PROBLEMS

Page 71: Let’s Talk About Ruby

generating poker hands

Page 72: Let’s Talk About Ruby

building a deck of cards

suits = %w(S C H D)

Page 73: Let’s Talk About Ruby

building a deck of cards

suits = %w(S C H D) => [“S”, “C”, “H”, “D”]

Page 74: Let’s Talk About Ruby

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

Page 75: Let’s Talk About Ruby

building a deck of cards

suits = %w(S C H D) faces =

Page 76: Let’s Talk About Ruby

building a deck of cards

suits = %w(S C H D) faces = (2..10).to_a

Page 77: Let’s Talk About Ruby

building a deck of cards

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

Page 78: Let’s Talk About Ruby

[1, 2, 3]

[“a”, “b”, “c”]

Page 79: Let’s Talk About Ruby

[1, 2, 3]

x

[“a”, “b”, “c”]

Page 80: Let’s Talk About Ruby

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

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

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

cross product

Page 81: Let’s Talk About Ruby

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)

Page 82: Let’s Talk About Ruby

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

Page 83: Let’s Talk About Ruby

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

Page 84: Let’s Talk About Ruby

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”

Page 85: Let’s Talk About Ruby

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

Page 86: Let’s Talk About Ruby

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

Page 87: Let’s Talk About Ruby

building a deck of cards

.map!

Page 88: Let’s Talk About Ruby

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

Page 89: Let’s Talk About Ruby

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 }

Page 90: Let’s Talk About Ruby

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)

Page 91: Let’s Talk About Ruby

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)

Page 92: Let’s Talk About Ruby

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.

Page 93: Let’s Talk About Ruby

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)

Page 94: Let’s Talk About Ruby

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

Page 95: Let’s Talk About Ruby

LET’S TALK ABOUT

MAP REDUCE

Page 96: Let’s Talk About Ruby

LET’S TALK ABOUT

MAP REDUCE ©

Page 97: Let’s Talk About Ruby

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)

Page 98: Let’s Talk About Ruby

counting url access frequency

log

Page 99: Let’s Talk About Ruby

counting url access frequency

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

Page 100: Let’s Talk About Ruby

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)

Page 101: Let’s Talk About Ruby

generate count pairs

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

Page 102: Let’s Talk About Ruby

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)

Page 103: Let’s Talk About Ruby

BUT FIRST

Page 104: Let’s Talk About Ruby

Collection of key-value

pairs.

Similar to an array, except

indexing is done via unique

keys.

Page 105: Let’s Talk About Ruby

brief introduction to hashes

my_hash = { :abc => 5, “def” => 9 }

Page 106: Let’s Talk About Ruby

brief introduction to hashes

my_hash = { :abc => 5, “def” => 9 } my_hash[:abc]

Page 107: Let’s Talk About Ruby

brief introduction to hashes

my_hash = { :abc => 5, “def” => 9 } my_hash[:abc] => 5

Page 108: Let’s Talk About Ruby

brief introduction to hashes

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

Page 109: Let’s Talk About Ruby

brief introduction to hashes

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

Page 110: Let’s Talk About Ruby

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)

Page 111: Let’s Talk About Ruby

combining count pairs

count_pairs.reduce

Page 112: Let’s Talk About Ruby

combining count pairs

count_pairs.reduce({})

Page 113: Let’s Talk About Ruby

combining count pairs

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

Page 114: Let’s Talk About Ruby

combining count pairs

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

Page 115: Let’s Talk About Ruby

combining count pairs

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

Page 116: Let’s Talk About Ruby

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

Page 117: Let’s Talk About Ruby

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

Page 118: Let’s Talk About Ruby

counting url access frequency

log = [“example.com”, “google.com”, “example.com”, “unb.ca”]

Page 119: Let’s Talk About Ruby

counting url access frequency

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