Let’s Talk About Ruby

Post on 08-Aug-2015

280 views 5 download

Tags:

Transcript of Let’s Talk About Ruby

LET’S TALK ABOUT

ruby

Ian Bishop @ianbishop

Ruby is

Ruby is

simple in appearance,

Ruby is

simple in appearance,

but is very complex inside,

Ruby is

simple in appearance,

but is very complex inside,

just like our human body

Ruby is

simple in appearance,

but is very complex inside,

just like our human body - Yukihiro “matz” Matsumoto

philosophy

principle of

least surprise

principle of least surprise

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

principle of least surprise

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

features

everything is an object

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

everything is an object

5.times

everything is a message

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

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

everything is a message

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

everything is a message

/abc/

/abc/

everything is a message

/abc/

/abc/.send

everything is a message

/abc/ ===

/abc/.send(:===

everything is a message

/abc/ === “abc”

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

everything is a message

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

everything is a message

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

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

everything is a message

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

=> “uppercase”

everything is a message

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

=> “uppercase”

everything is a message

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

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

dynamic runtime

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

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

dynamic runtime

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

dynamic runtime

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

dynamic runtime

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

DEALING WITH

data

using collections

a = [1,2,3]

using collections

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

using collections

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

using collections

a.each

using collections

a.each do |x|

end

using collections

a.each do |x| puts x

end

using collections

a.each do |x| puts x

end 1 2 3 meow

.each

Iterate over elements in a collection

.map

Iterate over elements in a collection,

returning a new collection of

elements of the same size

using map

a = [1,2,3]

using map

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

using map

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

end

using map

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

2 * x end

using map

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

return 2 * x end

using map

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

2 * x end

using map

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

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

.map

Iterate over elements in a collection,

returning a new collection of

elements of the same size

.select

Iterate over elements in a collection,

returning elements which match a

specified criteria

using select

a = [1,2,3]

using select

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

using select

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

end

using select

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

x.odd? end

using select

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

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

.select

Iterate over elements in a collection,

returning elements which match a

specified criteria

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

using reduce

a = [1,2,3]

using reduce

a = (1..100)

using reduce

a = (1..100) a.reduce

using reduce

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

end

using reduce

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

sum + x end

using reduce

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

sum + x end => 5050

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

.reduce

Combines all elements in a collection

using a binary operation, returning

an accumulator value.

using reduce (again)

a = (1..100) a.reduce

using reduce (again)

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

using reduce (again)

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

SOLVING

HARDER

PROBLEMS

generating poker hands

building a deck of cards

suits = %w(S C H D)

building a deck of cards

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

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

building a deck of cards

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

building a deck of cards

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

building a deck of cards

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

[1, 2, 3]

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

[1, 2, 3]

x

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

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

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

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

cross product

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)

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

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

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”

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

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

building a deck of cards

.map!

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

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 }

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)

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)

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.

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)

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

LET’S TALK ABOUT

MAP REDUCE

LET’S TALK ABOUT

MAP REDUCE ©

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)

counting url access frequency

log

counting url access frequency

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

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)

generate count pairs

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

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)

BUT FIRST

Collection of key-value

pairs.

Similar to an array, except

indexing is done via unique

keys.

brief introduction to hashes

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

brief introduction to hashes

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

brief introduction to hashes

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

brief introduction to hashes

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

brief introduction to hashes

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

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)

combining count pairs

count_pairs.reduce

combining count pairs

count_pairs.reduce({})

combining count pairs

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

combining count pairs

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

combining count pairs

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

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

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

counting url access frequency

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

counting url access frequency

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