253
Kerry Buckley, IPRUG 4 December 2012 Ruby Nooks & Crannies

Ruby nooks & crannies

Embed Size (px)

Citation preview

Page 1: Ruby nooks & crannies

Kerry Buckley, IPRUG 4 December 2012

RubyNooks & Crannies

Page 2: Ruby nooks & crannies

% notation

Page 3: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]

Page 4: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

Page 5: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%Q[foo “bar” ‘baz’ #{2 + 2}]

Page 6: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

Page 7: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%q[foo "bar" 'baz' #{2 + 2}]

Page 8: Ruby nooks & crannies

% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"

%q[foo "bar" 'baz' #{2 + 2}]=> "foo \"bar\" 'baz' \#{2 + 2}"

Page 9: Ruby nooks & crannies

% notation

Page 10: Ruby nooks & crannies

% notation%{foo}

Page 11: Ruby nooks & crannies

% notation%{foo}=> "foo"

Page 12: Ruby nooks & crannies

% notation%{foo}=> "foo"

%|bar|

Page 13: Ruby nooks & crannies

% notation%{foo}=> "foo"

%|bar|=> "bar"

Page 14: Ruby nooks & crannies

% notation%{foo}=> "foo"

%|bar|=> "bar"

%$#@_$

Page 15: Ruby nooks & crannies

% notation%{foo}=> "foo"

%|bar|=> "bar"

%$#@_$=> “”

Page 16: Ruby nooks & crannies

% notation

Page 17: Ruby nooks & crannies

% notation%w[foo bar baz]

Page 18: Ruby nooks & crannies

% notation%w[foo bar baz]=> ["foo", "bar", "baz"]

Page 19: Ruby nooks & crannies

% notation%w[foo bar baz]=> ["foo", "bar", "baz"]

%w[foo bar b#{2+2}z]

Page 20: Ruby nooks & crannies

% notation%w[foo bar baz]=> ["foo", "bar", "baz"]

%w[foo bar b#{2+2}z]=> ["foo", "bar", "b\#{2+2}z"]

Page 21: Ruby nooks & crannies

% notation%w[foo bar baz]=> ["foo", "bar", "baz"]

%w[foo bar b#{2+2}z]=> ["foo", "bar", "b\#{2+2}z"]

%W[foo bar b#{2+2}z]

Page 22: Ruby nooks & crannies

% notation%w[foo bar baz]=> ["foo", "bar", "baz"]

%w[foo bar b#{2+2}z]=> ["foo", "bar", "b\#{2+2}z"]

%W[foo bar b#{2+2}z]=> ["foo", "bar", "b4z"]

Page 23: Ruby nooks & crannies

% notation

Page 24: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i

Page 25: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i

Page 26: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i

%s{foo bar}

Page 27: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i

%s{foo bar}=> :"foo bar"

Page 28: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i

%s{foo bar}=> :"foo bar"

%x{head -1 /etc/paths}

Page 29: Ruby nooks & crannies

% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i

%s{foo bar}=> :"foo bar"

%x{head -1 /etc/paths}=> "/usr/bin\n"

Page 30: Ruby nooks & crannies

*

Page 31: Ruby nooks & crannies

*"hello " * 3

Page 32: Ruby nooks & crannies

*"hello " * 3=> "hello hello hello "

Page 33: Ruby nooks & crannies

*"hello " * 3=> "hello hello hello "

[1, 2, 3] * 2

Page 34: Ruby nooks & crannies

*"hello " * 3=> "hello hello hello "

[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]

Page 35: Ruby nooks & crannies

*"hello " * 3=> "hello hello hello "

[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]

[1, 2, 3] * " "

Page 36: Ruby nooks & crannies

*"hello " * 3=> "hello hello hello "

[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]

[1, 2, 3] * " "=> "1 2 3"

Page 37: Ruby nooks & crannies

Substring match

Page 38: Ruby nooks & crannies

Substring matchstr = "foo bar"

Page 39: Ruby nooks & crannies

Substring matchstr = "foo bar"

str =~ /o./

Page 40: Ruby nooks & crannies

Substring matchstr = "foo bar"

str =~ /o./=> 1

Page 41: Ruby nooks & crannies

Substring matchstr = "foo bar"

str =~ /o./=> 1

str =~ /xx/

Page 42: Ruby nooks & crannies

Substring matchstr = "foo bar"

str =~ /o./=> 1

str =~ /xx/=> nil

Page 43: Ruby nooks & crannies

Substring match

Page 44: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./

Page 45: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./=> #<MatchData "oo">

Page 46: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./=> #<MatchData "oo">

str.match /x/

Page 47: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./=> #<MatchData "oo">

str.match /x/=> nil

Page 48: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./=> #<MatchData "oo">

str.match /x/=> nil

str.match “oo”

Page 49: Ruby nooks & crannies

Substring matchstr = "foo bar"

str.match /o./=> #<MatchData "oo">

str.match /x/=> nil

str.match “oo”=> #<MatchData "oo">

Page 50: Ruby nooks & crannies

Substring match

Page 51: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]

Page 52: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]=> "oo"

Page 53: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]=> "oo"

str[/x/]

Page 54: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]=> "oo"

str[/x/]=> nil

Page 55: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]=> "oo"

str[/x/]=> nil

str[“oo”]

Page 56: Ruby nooks & crannies

Substring matchstr = "foo bar"

str[/o./]=> "oo"

str[/x/]=> nil

str[“oo”]=> "oo"

Page 57: Ruby nooks & crannies

Blocks & procs

Page 58: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }

Page 59: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

Page 60: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

flip = proc {|a| a.reverse }

Page 61: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

flip = proc {|a| a.reverse }

{foo bar}.map &flip

Page 62: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

flip = proc {|a| a.reverse }

{foo bar}.map &flip=> ["oof", "rab"]

Page 63: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

flip = ->(a){ a.reverse }

{foo bar}.map &flip=> ["oof", "rab"]

Page 64: Ruby nooks & crannies

Blocks & procs

Page 65: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

Page 66: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

{foo bar}.map &:reverse

Page 67: Ruby nooks & crannies

Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]

{foo bar}.map &:reverse=> ["oof", "rab"]

Page 68: Ruby nooks & crannies

Blocks & procs

Page 69: Ruby nooks & crannies

Blocks & procsClass Symbol def to_proc Proc.new do |*args| args.shift.__send__(self, *args) end endend

Page 70: Ruby nooks & crannies

Blocks & procs

Page 71: Ruby nooks & crannies

Blocks & procsflip = ->(a){ a.reverse }

Page 72: Ruby nooks & crannies

Blocks & procsflip = ->(a){ a.reverse }

flip.call("GURPI")

Page 73: Ruby nooks & crannies

Blocks & procsflip = ->(a){ a.reverse }

flip.call("GURPI")=> "IPRUG"

Page 74: Ruby nooks & crannies

Blocks & procsflip = ->(a){ a.reverse }

flip.call("GURPI")=> "IPRUG"

flip.("GURPI")=> "IPRUG"

Page 75: Ruby nooks & crannies

Blocks & procsflip = ->(a){ a.reverse }

flip.call("GURPI")=> "IPRUG"

flip.("GURPI")=> "IPRUG"

flip["GURPI"]=> "IPRUG"

Page 76: Ruby nooks & crannies

tap

Page 77: Ruby nooks & crannies

tap(1..5).map{|a| a**2} .select(&:odd?) .inject(&:+)=> 35

Page 78: Ruby nooks & crannies

tap

Page 79: Ruby nooks & crannies

tap(1..5).map{|a| a**2} .select(&:odd?) .tap{|a| p a} .inject(&:+)

Page 80: Ruby nooks & crannies

tap(1..5).map{|a| a**2} .select(&:odd?) .tap{|a| p a} .inject(&:+)[1, 9, 25]=> 35

Page 81: Ruby nooks & crannies

tap

Page 82: Ruby nooks & crannies

tapdef create_foo Foo.new.tap do|foo| foo.bar = 42 foo.baz = 69 endend

Page 83: Ruby nooks & crannies

Multiple assignment

Page 84: Ruby nooks & crannies

Multiple assignment

a, b = 1, 2

Page 85: Ruby nooks & crannies

Multiple assignment

a, b = 1, 2

a=> 1

b=> 2

Page 86: Ruby nooks & crannies

Multiple assignment

Page 87: Ruby nooks & crannies

Multiple assignment

array = [1, 2]a, b = array

Page 88: Ruby nooks & crannies

Multiple assignment

array = [1, 2]a, b = array

a=> 1

b=> 2

Page 89: Ruby nooks & crannies

Multiple assignment

Page 90: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

Page 91: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

c

Page 92: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

c=> nil

Page 93: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

c=> nil

a, b = 1, 2, 3

Page 94: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

c=> nil

a, b = 1, 2, 3

b

Page 95: Ruby nooks & crannies

Multiple assignment

a, b, c = 1, 2

c=> nil

a, b = 1, 2, 3

b=> 2

Page 96: Ruby nooks & crannies

Multiple assignment

Page 97: Ruby nooks & crannies

Multiple assignment

a, *b = 1, 2, 3, 4

Page 98: Ruby nooks & crannies

Multiple assignment

a, *b = 1, 2, 3, 4

a

Page 99: Ruby nooks & crannies

Multiple assignment

a, *b = 1, 2, 3, 4

a=> 1

Page 100: Ruby nooks & crannies

Multiple assignment

a, *b = 1, 2, 3, 4

a=> 1

b

Page 101: Ruby nooks & crannies

Multiple assignment

a, *b = 1, 2, 3, 4

a=> 1

b=> [2, 3, 4]

Page 102: Ruby nooks & crannies

Multiple assignment

Page 103: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

Page 104: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b

Page 105: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b=> [2, 3, 4]

Page 106: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b=> [2, 3, 4]

c

Page 107: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b=> [2, 3, 4]

c=> 5

Page 108: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b=> [2, 3, 4]

c=> 5

d

Page 109: Ruby nooks & crannies

Multiple assignment

a, *b, c, d = 1, 2, 3, 4, 5, 6

b=> [2, 3, 4]

c=> 5

d=> 6

Page 110: Ruby nooks & crannies

Multiple assignment

Page 111: Ruby nooks & crannies

Multiple assignment

array = [1, 2, 3, 4]a, _, _, b = array

Page 112: Ruby nooks & crannies

Multiple assignment

array = [1, 2, 3, 4]a, _, _, b = array

a=> 1

Page 113: Ruby nooks & crannies

Multiple assignment

array = [1, 2, 3, 4]a, _, _, b = array

a=> 1

b=> 4

Page 114: Ruby nooks & crannies

Multiple assignment

array = [1, 2, 3, 4]a, _, _, b = array

a=> 1

b=> 4

a, *_, b = array

Page 115: Ruby nooks & crannies

Multiple assignment

Page 116: Ruby nooks & crannies

Multiple assignment

array = [[1, 2], [3, 4]]

Page 117: Ruby nooks & crannies

Multiple assignment

array = [[1, 2], [3, 4]]

array.map {|a| a[0] * a[1] }

Page 118: Ruby nooks & crannies

Multiple assignment

array = [[1, 2], [3, 4]]

array.map {|a| a[0] * a[1] }=> [2, 12]

Page 119: Ruby nooks & crannies

Multiple assignment

array = [[1, 2], [3, 4]]

array.map {|a| a[0] * a[1] }=> [2, 12]

array.map {|a, b| a * b }

Page 120: Ruby nooks & crannies

Multiple assignment

array = [[1, 2], [3, 4]]

array.map {|a| a[0] * a[1] }=> [2, 12]

array.map {|a, b| a * b }=> [2, 12]

Page 121: Ruby nooks & crannies

Multiple assignment

Page 122: Ruby nooks & crannies

Multiple assignment

array = [[1, [2, 3]], [3, [4, 5]]]

Page 123: Ruby nooks & crannies

Multiple assignment

array = [[1, [2, 3]], [3, [4, 5]]]

array.map {|a, (b, _)| a * b }

Page 124: Ruby nooks & crannies

Multiple assignment

array = [[1, [2, 3]], [3, [4, 5]]]

array.map {|a, (b, _)| a * b }=> [2, 12]

Page 125: Ruby nooks & crannies

Multiple assignment

Page 126: Ruby nooks & crannies

Multiple assignment

hash = {foo: 1, bar: 2}

Page 127: Ruby nooks & crannies

Multiple assignment

hash = {foo: 1, bar: 2}

hash.map {|a| a }

Page 128: Ruby nooks & crannies

Multiple assignment

hash = {foo: 1, bar: 2}

hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]

Page 129: Ruby nooks & crannies

Multiple assignment

hash = {foo: 1, bar: 2}

hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]

hash.map {|k,v| "#{k} -> #{v}" }

Page 130: Ruby nooks & crannies

Multiple assignment

hash = {foo: 1, bar: 2}

hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]

hash.map {|k,v| "#{k} -> #{v}" }=> ["foo -> 1", "bar -> 2"]

Page 131: Ruby nooks & crannies

Multiple assignment

Page 132: Ruby nooks & crannies

Multiple assignment

people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}

Page 133: Ruby nooks & crannies

Multiple assignment

people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}

people.map {|p| "#{p[0]} is #{p[1][0]}"}

Page 134: Ruby nooks & crannies

Multiple assignment

people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}

people.map {|p| "#{p[0]} is #{p[1][0]}"}=> ["fred is 40", "freda is 21"]

Page 135: Ruby nooks & crannies

Multiple assignment

people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}

people.map {|name, attrs| "#{name} is #{attrs.first}"}=> ["fred is 40", "freda is 21"]

Page 136: Ruby nooks & crannies

Multiple assignment

people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}

people.map {|name, (age, *_)| "#{name} is #{age}"}=> ["fred is 40", "freda is 21"]

Page 137: Ruby nooks & crannies

Type coercion

Page 138: Ruby nooks & crannies

Type coercion123.to_s

Page 139: Ruby nooks & crannies

Type coercion123.to_s=> "123"

Page 140: Ruby nooks & crannies

Type coercion123.to_s=> "123"

:foo.to_s

Page 141: Ruby nooks & crannies

Type coercion123.to_s=> "123"

:foo.to_s=> "foo"

Page 142: Ruby nooks & crannies

Type coercion123.to_s=> "123"

:foo.to_s=> "foo"

[1, 2].to_s

Page 143: Ruby nooks & crannies

Type coercion123.to_s=> "123"

:foo.to_s=> "foo"

[1, 2].to_s=> "[1, 2]"

Page 144: Ruby nooks & crannies

Type coercion

Page 145: Ruby nooks & crannies

Type coercion"123".to_i

Page 146: Ruby nooks & crannies

Type coercion"123".to_i=> 123

Page 147: Ruby nooks & crannies

Type coercion"123".to_i=> 123

1.2.to_i

Page 148: Ruby nooks & crannies

Type coercion"123".to_i=> 123

1.2.to_i=> 1

Page 149: Ruby nooks & crannies

Type coercion"123".to_i=> 123

1.2.to_i=> 1

"foo".to_i

Page 150: Ruby nooks & crannies

Type coercion"123".to_i=> 123

1.2.to_i=> 1

"foo".to_i=> 0

Page 151: Ruby nooks & crannies

Type coercion

Page 152: Ruby nooks & crannies

Type coercion"1.2".to_f

Page 153: Ruby nooks & crannies

Type coercion"1.2".to_f=> 1.2

Page 154: Ruby nooks & crannies

Type coercion"1.2".to_f=> 1.2

1.to_f

Page 155: Ruby nooks & crannies

Type coercion"1.2".to_f=> 1.2

1.to_f=> 1.0

Page 156: Ruby nooks & crannies

Type coercion"1.2".to_f=> 1.2

1.to_f=> 1.0

"foo".to_f

Page 157: Ruby nooks & crannies

Type coercion"1.2".to_f=> 1.2

1.to_f=> 1.0

"foo".to_f=> 0.0

Page 158: Ruby nooks & crannies

Type coercion

Page 159: Ruby nooks & crannies

Type coercion1.2.to_int

Page 160: Ruby nooks & crannies

Type coercion1.2.to_int=> 1

Page 161: Ruby nooks & crannies

Type coercion1.2.to_int=> 1

"123".to_int

Page 162: Ruby nooks & crannies

Type coercion1.2.to_int=> 1

"123".to_intNoMethodError: undefined method `to_int' for "123":String

Page 163: Ruby nooks & crannies

Type coercion1.2.to_int=> 1

"123".to_intNoMethodError: undefined method `to_int' for "123":String

:foo.to_str

Page 164: Ruby nooks & crannies

Type coercion1.2.to_int=> 1

"123".to_intNoMethodError: undefined method `to_int' for "123":String

:foo.to_strNoMethodError: undefined method `to_str' for :foo:Symbol

Page 165: Ruby nooks & crannies

Type coercion

Page 166: Ruby nooks & crannies

Type coercionInteger(123)

Page 167: Ruby nooks & crannies

Type coercionInteger(123)=> 123

Page 168: Ruby nooks & crannies

Type coercionInteger(123)=> 123

Integer("123")

Page 169: Ruby nooks & crannies

Type coercionInteger(123)=> 123

Integer("123")=> 123

Page 170: Ruby nooks & crannies

Type coercionInteger(123)=> 123

Integer("123")=> 123

Integer(1.23)

Page 171: Ruby nooks & crannies

Type coercionInteger(123)=> 123

Integer("123")=> 123

Integer(1.23)=> 1

Page 172: Ruby nooks & crannies

Type coercion

Page 173: Ruby nooks & crannies

Type coercionString(“foo”)

Page 174: Ruby nooks & crannies

Type coercionString(“foo”)=> “foo”

Page 175: Ruby nooks & crannies

Type coercionString(“foo”)=> “foo”

String(123)

Page 176: Ruby nooks & crannies

Type coercionString(“foo”)=> “foo”

String(123)=> “123”

Page 177: Ruby nooks & crannies

Type coercionString(“foo”)=> “foo”

String(123)=> “123”

String(:foo)

Page 178: Ruby nooks & crannies

Type coercionString(“foo”)=> “foo”

String(123)=> “123”

String(:foo)=> “foo”

Page 179: Ruby nooks & crannies

Type coercion

Page 180: Ruby nooks & crannies

Type coercionArray(“foo”)

Page 181: Ruby nooks & crannies

Type coercionArray(“foo”)=> [“foo”]

Page 182: Ruby nooks & crannies

Type coercionArray(“foo”)=> [“foo”]

Array([“foo”, “bar”])

Page 183: Ruby nooks & crannies

Type coercionArray(“foo”)=> [“foo”]

Array([“foo”, “bar”])=> [“foo”, “bar”]

Page 184: Ruby nooks & crannies

Type coercion

Page 185: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

Page 186: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo "foo", "bar"

Page 187: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo "foo", "bar"foobar

Page 188: Ruby nooks & crannies

Type coercion

Page 189: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo "foo"

Page 190: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo "foo"foo

Page 191: Ruby nooks & crannies

Type coercion

Page 192: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo ["foo", "bar"]

Page 193: Ruby nooks & crannies

Type coerciondef echo *args Array(args).each {|a| puts a }end

echo ["foo", "bar"]foobar

Page 194: Ruby nooks & crannies

Mapping hashes

Page 195: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

Page 196: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

squares = {}numbers.each do |k, v| squares[k] = v**2end

Page 197: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

squares = {}numbers.each do |k, v| squares[k] = v**2end

squares=> {:one=>1, :two=>4, :three=>9}

Page 198: Ruby nooks & crannies

Mapping hashes

Page 199: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

numbers.map {|k, v| [k, v**2] }

Page 200: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

numbers.map {|k, v| [k, v**2] }=> [[:one, 1], [:two, 4], [:three, 9]]

Page 201: Ruby nooks & crannies

Mapping hashes

Page 202: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

numbers.map {|k, v| [k, v**2] }.flatten

Page 203: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

numbers.map {|k, v| [k, v**2] }.flatten=> [:one, 1, :two, 4, :three, 9]

Page 204: Ruby nooks & crannies

Mapping hashes

Page 205: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

Hash[*numbers.map {|k, v| [k, v**2] }.flatten]

Page 206: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

Hash[*numbers.map {|k, v| [k, v**2] }.flatten]=> {:one=>1, :two=>4, :three=>9}

Page 207: Ruby nooks & crannies

Mapping hashes

Page 208: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

Hash[*numbers.flat_map {|k, v| [k, v**2] }]

Page 209: Ruby nooks & crannies

Mapping hashesnumbers = {one: 1, two: 2, three: 3}

Hash[*numbers.flat_map {|k, v| [k, v**2] }]=> {:one=>1, :two=>4, :three=>9}

Page 210: Ruby nooks & crannies

Anonymous classes

Page 211: Ruby nooks & crannies

Anonymous classes

foo = Class.new do def hello "hello" endend

Page 212: Ruby nooks & crannies

Anonymous classes

foo = Class.new do def hello "hello" endend

foo.new.hello

Page 213: Ruby nooks & crannies

Anonymous classes

foo = Class.new do def hello "hello" endend

foo.new.hello=> "hello"

Page 214: Ruby nooks & crannies

Struct & OpenStruct

Page 215: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :age

Page 216: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43

Page 217: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>

Page 218: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>

me.age

Page 219: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>

me.age=> 43

Page 220: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>

me.age=> 43

me.age = 30

Page 221: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>

me.age=> 43

me.age = 30=> 30

Page 222: Ruby nooks & crannies

Struct & OpenStruct

Page 223: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend

Page 224: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend

Person.new("Kerry").greet

Page 225: Ruby nooks & crannies

Struct & OpenStruct

Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend

Person.new("Kerry").greetHello, I'm Kerry

Page 226: Ruby nooks & crannies

Struct & OpenStruct

Page 227: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)

Page 228: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>

Page 229: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>

me.age

Page 230: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>

me.age=> 43

Page 231: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>

me.age=> 43

me.age = 30

Page 232: Ruby nooks & crannies

Struct & OpenStruct

me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>

me.age=> 43

me.age = 30=> 30

Page 233: Ruby nooks & crannies

Struct & OpenStruct

Page 234: Ruby nooks & crannies

Struct & OpenStruct

struct.members

Page 235: Ruby nooks & crannies

Struct & OpenStruct

struct.members=> [:name, :age]

Page 236: Ruby nooks & crannies

Struct & OpenStruct

struct.members=> [:name, :age]

struct.values

Page 237: Ruby nooks & crannies

Struct & OpenStruct

struct.members=> [:name, :age]

struct.values=> ["Kerry", 43]

Page 238: Ruby nooks & crannies

Struct & OpenStruct

struct.members=> [:name, :age]

struct.values=> ["Kerry", 43]

struct.each_pair {|k, v| puts "#{k}: #{v}" }

Page 239: Ruby nooks & crannies

Struct & OpenStruct

struct.members=> [:name, :age]

struct.values=> ["Kerry", 43]

struct.each_pair {|k, v| puts "#{k}: #{v}" }name: Kerryage: 43

Page 240: Ruby nooks & crannies

Constructors

Page 241: Ruby nooks & crannies

Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend

Page 242: Ruby nooks & crannies

Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend

Colour.new(255, 0, 0)

Page 243: Ruby nooks & crannies

Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend

Colour.new(255, 0, 0)

Page 244: Ruby nooks & crannies

Constructors

Page 245: Ruby nooks & crannies

Constructorsclass BasicObject def self.new(*args, &block) instance = allocate instance.initialize(*args, &block) instance endend

Page 246: Ruby nooks & crannies

Constructors

Page 247: Ruby nooks & crannies

Constructorsclass Colour def self.new_with_hex(*args, &block) instance = allocate instance.init_with_hex(*args, &block) instance end

Page 248: Ruby nooks & crannies

Constructorsclass Colour def self.new_with_hex(*args, &block) instance = allocate instance.init_with_hex(*args, &block) instance end

def init_with_hex(name) ... endend

Page 249: Ruby nooks & crannies

Constructors

Page 250: Ruby nooks & crannies

ConstructorsColour.new(255, 0, 0)

Page 251: Ruby nooks & crannies

ConstructorsColour.new(255, 0, 0)

Colour.new_with_hex(“ff0000”)

Page 252: Ruby nooks & crannies
Page 253: Ruby nooks & crannies

end