ruby nooks & crannies
TRANSCRIPT
![Page 1: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/1.jpg)
Kerry Buckley, IPRUG 4 December 2012
RubyNooks & Crannies
![Page 2: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/2.jpg)
% notation
![Page 3: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/3.jpg)
% notation%[foo “bar” ‘baz’ #{2 + 2}]
![Page 4: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/4.jpg)
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"
![Page 5: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/5.jpg)
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo \"bar\" 'baz' 4"
%Q[foo “bar” ‘baz’ #{2 + 2}]
![Page 6: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/6.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/7.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/8.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/9.jpg)
% notation
![Page 10: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/10.jpg)
% notation%{foo}
![Page 11: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/11.jpg)
% notation%{foo}=> "foo"
![Page 12: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/12.jpg)
% notation%{foo}=> "foo"
%|bar|
![Page 13: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/13.jpg)
% notation%{foo}=> "foo"
%|bar|=> "bar"
![Page 14: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/14.jpg)
% notation%{foo}=> "foo"
%|bar|=> "bar"
%$#@_$
![Page 15: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/15.jpg)
% notation%{foo}=> "foo"
%|bar|=> "bar"
%$#@_$=> “”
![Page 16: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/16.jpg)
% notation
![Page 17: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/17.jpg)
% notation%w[foo bar baz]
![Page 18: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/18.jpg)
% notation%w[foo bar baz]=> ["foo", "bar", "baz"]
![Page 19: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/19.jpg)
% notation%w[foo bar baz]=> ["foo", "bar", "baz"]
%w[foo bar b#{2+2}z]
![Page 20: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/20.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/21.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/22.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/23.jpg)
% notation
![Page 24: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/24.jpg)
% notation%r{^foo/bar/\d*$}i
![Page 25: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/25.jpg)
% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i
![Page 26: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/26.jpg)
% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i
%s{foo bar}
![Page 27: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/27.jpg)
% notation%r{^foo/bar/\d*$}i=> /^foo\/bar\/\d*$/i
%s{foo bar}=> :"foo bar"
![Page 28: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/28.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/29.jpg)
% 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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/30.jpg)
*
![Page 31: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/31.jpg)
*"hello " * 3
![Page 32: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/32.jpg)
*"hello " * 3=> "hello hello hello "
![Page 33: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/33.jpg)
*"hello " * 3=> "hello hello hello "
[1, 2, 3] * 2
![Page 34: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/34.jpg)
*"hello " * 3=> "hello hello hello "
[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]
![Page 35: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/35.jpg)
*"hello " * 3=> "hello hello hello "
[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]
[1, 2, 3] * " "
![Page 36: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/36.jpg)
*"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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/37.jpg)
Substring match
![Page 38: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/38.jpg)
Substring matchstr = "foo bar"
![Page 39: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/39.jpg)
Substring matchstr = "foo bar"
str =~ /o./
![Page 40: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/40.jpg)
Substring matchstr = "foo bar"
str =~ /o./=> 1
![Page 41: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/41.jpg)
Substring matchstr = "foo bar"
str =~ /o./=> 1
str =~ /xx/
![Page 42: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/42.jpg)
Substring matchstr = "foo bar"
str =~ /o./=> 1
str =~ /xx/=> nil
![Page 43: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/43.jpg)
Substring match
![Page 44: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/44.jpg)
Substring matchstr = "foo bar"
str.match /o./
![Page 45: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/45.jpg)
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
![Page 46: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/46.jpg)
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
str.match /x/
![Page 47: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/47.jpg)
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
str.match /x/=> nil
![Page 48: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/48.jpg)
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
str.match /x/=> nil
str.match “oo”
![Page 49: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/49.jpg)
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
str.match /x/=> nil
str.match “oo”=> #<MatchData "oo">
![Page 50: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/50.jpg)
Substring match
![Page 51: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/51.jpg)
Substring matchstr = "foo bar"
str[/o./]
![Page 52: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/52.jpg)
Substring matchstr = "foo bar"
str[/o./]=> "oo"
![Page 53: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/53.jpg)
Substring matchstr = "foo bar"
str[/o./]=> "oo"
str[/x/]
![Page 54: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/54.jpg)
Substring matchstr = "foo bar"
str[/o./]=> "oo"
str[/x/]=> nil
![Page 55: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/55.jpg)
Substring matchstr = "foo bar"
str[/o./]=> "oo"
str[/x/]=> nil
str[“oo”]
![Page 56: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/56.jpg)
Substring matchstr = "foo bar"
str[/o./]=> "oo"
str[/x/]=> nil
str[“oo”]=> "oo"
![Page 57: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/57.jpg)
Blocks & procs
![Page 58: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/58.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }
![Page 59: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/59.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
![Page 60: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/60.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
flip = proc {|a| a.reverse }
![Page 61: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/61.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/62.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/63.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/64.jpg)
Blocks & procs
![Page 65: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/65.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
![Page 66: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/66.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
{foo bar}.map &:reverse
![Page 67: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/67.jpg)
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
{foo bar}.map &:reverse=> ["oof", "rab"]
![Page 68: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/68.jpg)
Blocks & procs
![Page 69: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/69.jpg)
Blocks & procsClass Symbol def to_proc Proc.new do |*args| args.shift.__send__(self, *args) end endend
![Page 70: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/70.jpg)
Blocks & procs
![Page 71: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/71.jpg)
Blocks & procsflip = ->(a){ a.reverse }
![Page 72: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/72.jpg)
Blocks & procsflip = ->(a){ a.reverse }
flip.call("GURPI")
![Page 73: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/73.jpg)
Blocks & procsflip = ->(a){ a.reverse }
flip.call("GURPI")=> "IPRUG"
![Page 74: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/74.jpg)
Blocks & procsflip = ->(a){ a.reverse }
flip.call("GURPI")=> "IPRUG"
flip.("GURPI")=> "IPRUG"
![Page 75: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/75.jpg)
Blocks & procsflip = ->(a){ a.reverse }
flip.call("GURPI")=> "IPRUG"
flip.("GURPI")=> "IPRUG"
flip["GURPI"]=> "IPRUG"
![Page 76: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/76.jpg)
tap
![Page 77: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/77.jpg)
tap(1..5).map{|a| a**2} .select(&:odd?) .inject(&:+)=> 35
![Page 78: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/78.jpg)
tap
![Page 79: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/79.jpg)
tap(1..5).map{|a| a**2} .select(&:odd?) .tap{|a| p a} .inject(&:+)
![Page 80: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/80.jpg)
tap(1..5).map{|a| a**2} .select(&:odd?) .tap{|a| p a} .inject(&:+)[1, 9, 25]=> 35
![Page 81: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/81.jpg)
tap
![Page 82: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/82.jpg)
tapdef create_foo Foo.new.tap do|foo| foo.bar = 42 foo.baz = 69 endend
![Page 83: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/83.jpg)
Multiple assignment
![Page 84: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/84.jpg)
Multiple assignment
a, b = 1, 2
![Page 85: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/85.jpg)
Multiple assignment
a, b = 1, 2
a=> 1
b=> 2
![Page 86: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/86.jpg)
Multiple assignment
![Page 87: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/87.jpg)
Multiple assignment
array = [1, 2]a, b = array
![Page 88: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/88.jpg)
Multiple assignment
array = [1, 2]a, b = array
a=> 1
b=> 2
![Page 89: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/89.jpg)
Multiple assignment
![Page 90: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/90.jpg)
Multiple assignment
a, b, c = 1, 2
![Page 91: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/91.jpg)
Multiple assignment
a, b, c = 1, 2
c
![Page 92: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/92.jpg)
Multiple assignment
a, b, c = 1, 2
c=> nil
![Page 93: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/93.jpg)
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
![Page 94: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/94.jpg)
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
b
![Page 95: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/95.jpg)
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
b=> 2
![Page 96: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/96.jpg)
Multiple assignment
![Page 97: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/97.jpg)
Multiple assignment
a, *b = 1, 2, 3, 4
![Page 98: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/98.jpg)
Multiple assignment
a, *b = 1, 2, 3, 4
a
![Page 99: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/99.jpg)
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
![Page 100: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/100.jpg)
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
b
![Page 101: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/101.jpg)
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
b=> [2, 3, 4]
![Page 102: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/102.jpg)
Multiple assignment
![Page 103: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/103.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
![Page 104: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/104.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
![Page 105: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/105.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
![Page 106: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/106.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c
![Page 107: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/107.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c=> 5
![Page 108: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/108.jpg)
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c=> 5
d
![Page 109: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/109.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/110.jpg)
Multiple assignment
![Page 111: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/111.jpg)
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
![Page 112: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/112.jpg)
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
![Page 113: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/113.jpg)
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
b=> 4
![Page 114: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/114.jpg)
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
b=> 4
a, *_, b = array
![Page 115: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/115.jpg)
Multiple assignment
![Page 116: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/116.jpg)
Multiple assignment
array = [[1, 2], [3, 4]]
![Page 117: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/117.jpg)
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }
![Page 118: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/118.jpg)
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }=> [2, 12]
![Page 119: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/119.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/120.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/121.jpg)
Multiple assignment
![Page 122: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/122.jpg)
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
![Page 123: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/123.jpg)
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
array.map {|a, (b, _)| a * b }
![Page 124: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/124.jpg)
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
array.map {|a, (b, _)| a * b }=> [2, 12]
![Page 125: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/125.jpg)
Multiple assignment
![Page 126: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/126.jpg)
Multiple assignment
hash = {foo: 1, bar: 2}
![Page 127: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/127.jpg)
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }
![Page 128: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/128.jpg)
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]
![Page 129: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/129.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/130.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/131.jpg)
Multiple assignment
![Page 132: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/132.jpg)
Multiple assignment
people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}
![Page 133: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/133.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/134.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/135.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/136.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/137.jpg)
Type coercion
![Page 138: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/138.jpg)
Type coercion123.to_s
![Page 139: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/139.jpg)
Type coercion123.to_s=> "123"
![Page 140: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/140.jpg)
Type coercion123.to_s=> "123"
:foo.to_s
![Page 141: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/141.jpg)
Type coercion123.to_s=> "123"
:foo.to_s=> "foo"
![Page 142: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/142.jpg)
Type coercion123.to_s=> "123"
:foo.to_s=> "foo"
[1, 2].to_s
![Page 143: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/143.jpg)
Type coercion123.to_s=> "123"
:foo.to_s=> "foo"
[1, 2].to_s=> "[1, 2]"
![Page 144: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/144.jpg)
Type coercion
![Page 145: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/145.jpg)
Type coercion"123".to_i
![Page 146: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/146.jpg)
Type coercion"123".to_i=> 123
![Page 147: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/147.jpg)
Type coercion"123".to_i=> 123
1.2.to_i
![Page 148: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/148.jpg)
Type coercion"123".to_i=> 123
1.2.to_i=> 1
![Page 149: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/149.jpg)
Type coercion"123".to_i=> 123
1.2.to_i=> 1
"foo".to_i
![Page 150: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/150.jpg)
Type coercion"123".to_i=> 123
1.2.to_i=> 1
"foo".to_i=> 0
![Page 151: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/151.jpg)
Type coercion
![Page 152: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/152.jpg)
Type coercion"1.2".to_f
![Page 153: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/153.jpg)
Type coercion"1.2".to_f=> 1.2
![Page 154: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/154.jpg)
Type coercion"1.2".to_f=> 1.2
1.to_f
![Page 155: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/155.jpg)
Type coercion"1.2".to_f=> 1.2
1.to_f=> 1.0
![Page 156: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/156.jpg)
Type coercion"1.2".to_f=> 1.2
1.to_f=> 1.0
"foo".to_f
![Page 157: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/157.jpg)
Type coercion"1.2".to_f=> 1.2
1.to_f=> 1.0
"foo".to_f=> 0.0
![Page 158: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/158.jpg)
Type coercion
![Page 159: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/159.jpg)
Type coercion1.2.to_int
![Page 160: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/160.jpg)
Type coercion1.2.to_int=> 1
![Page 161: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/161.jpg)
Type coercion1.2.to_int=> 1
"123".to_int
![Page 162: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/162.jpg)
Type coercion1.2.to_int=> 1
"123".to_intNoMethodError: undefined method `to_int' for "123":String
![Page 163: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/163.jpg)
Type coercion1.2.to_int=> 1
"123".to_intNoMethodError: undefined method `to_int' for "123":String
:foo.to_str
![Page 164: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/164.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/165.jpg)
Type coercion
![Page 166: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/166.jpg)
Type coercionInteger(123)
![Page 167: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/167.jpg)
Type coercionInteger(123)=> 123
![Page 168: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/168.jpg)
Type coercionInteger(123)=> 123
Integer("123")
![Page 169: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/169.jpg)
Type coercionInteger(123)=> 123
Integer("123")=> 123
![Page 170: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/170.jpg)
Type coercionInteger(123)=> 123
Integer("123")=> 123
Integer(1.23)
![Page 171: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/171.jpg)
Type coercionInteger(123)=> 123
Integer("123")=> 123
Integer(1.23)=> 1
![Page 172: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/172.jpg)
Type coercion
![Page 173: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/173.jpg)
Type coercionString(“foo”)
![Page 174: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/174.jpg)
Type coercionString(“foo”)=> “foo”
![Page 175: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/175.jpg)
Type coercionString(“foo”)=> “foo”
String(123)
![Page 176: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/176.jpg)
Type coercionString(“foo”)=> “foo”
String(123)=> “123”
![Page 177: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/177.jpg)
Type coercionString(“foo”)=> “foo”
String(123)=> “123”
String(:foo)
![Page 178: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/178.jpg)
Type coercionString(“foo”)=> “foo”
String(123)=> “123”
String(:foo)=> “foo”
![Page 179: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/179.jpg)
Type coercion
![Page 180: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/180.jpg)
Type coercionArray(“foo”)
![Page 181: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/181.jpg)
Type coercionArray(“foo”)=> [“foo”]
![Page 182: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/182.jpg)
Type coercionArray(“foo”)=> [“foo”]
Array([“foo”, “bar”])
![Page 183: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/183.jpg)
Type coercionArray(“foo”)=> [“foo”]
Array([“foo”, “bar”])=> [“foo”, “bar”]
![Page 184: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/184.jpg)
Type coercion
![Page 185: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/185.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
![Page 186: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/186.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo", "bar"
![Page 187: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/187.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo", "bar"foobar
![Page 188: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/188.jpg)
Type coercion
![Page 189: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/189.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo"
![Page 190: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/190.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo"foo
![Page 191: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/191.jpg)
Type coercion
![Page 192: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/192.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo ["foo", "bar"]
![Page 193: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/193.jpg)
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo ["foo", "bar"]foobar
![Page 194: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/194.jpg)
Mapping hashes
![Page 195: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/195.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
![Page 196: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/196.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
squares = {}numbers.each do |k, v| squares[k] = v**2end
![Page 197: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/197.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/198.jpg)
Mapping hashes
![Page 199: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/199.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
numbers.map {|k, v| [k, v**2] }
![Page 200: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/200.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/201.jpg)
Mapping hashes
![Page 202: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/202.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
numbers.map {|k, v| [k, v**2] }.flatten
![Page 203: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/203.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/204.jpg)
Mapping hashes
![Page 205: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/205.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
Hash[*numbers.map {|k, v| [k, v**2] }.flatten]
![Page 206: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/206.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/207.jpg)
Mapping hashes
![Page 208: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/208.jpg)
Mapping hashesnumbers = {one: 1, two: 2, three: 3}
Hash[*numbers.flat_map {|k, v| [k, v**2] }]
![Page 209: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/209.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/210.jpg)
Anonymous classes
![Page 211: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/211.jpg)
Anonymous classes
foo = Class.new do def hello "hello" endend
![Page 212: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/212.jpg)
Anonymous classes
foo = Class.new do def hello "hello" endend
foo.new.hello
![Page 213: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/213.jpg)
Anonymous classes
foo = Class.new do def hello "hello" endend
foo.new.hello=> "hello"
![Page 214: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/214.jpg)
Struct & OpenStruct
![Page 215: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/215.jpg)
Struct & OpenStruct
Person = Struct.new :name, :age
![Page 216: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/216.jpg)
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43
![Page 217: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/217.jpg)
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
![Page 218: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/218.jpg)
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
me.age
![Page 219: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/219.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/220.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/221.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/222.jpg)
Struct & OpenStruct
![Page 223: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/223.jpg)
Struct & OpenStruct
Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend
![Page 224: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/224.jpg)
Struct & OpenStruct
Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend
Person.new("Kerry").greet
![Page 225: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/225.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/226.jpg)
Struct & OpenStruct
![Page 227: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/227.jpg)
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)
![Page 228: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/228.jpg)
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
![Page 229: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/229.jpg)
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age
![Page 230: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/230.jpg)
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age=> 43
![Page 231: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/231.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/232.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/233.jpg)
Struct & OpenStruct
![Page 234: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/234.jpg)
Struct & OpenStruct
struct.members
![Page 235: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/235.jpg)
Struct & OpenStruct
struct.members=> [:name, :age]
![Page 236: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/236.jpg)
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values
![Page 237: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/237.jpg)
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values=> ["Kerry", 43]
![Page 238: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/238.jpg)
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values=> ["Kerry", 43]
struct.each_pair {|k, v| puts "#{k}: #{v}" }
![Page 239: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/239.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/240.jpg)
Constructors
![Page 241: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/241.jpg)
Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend
![Page 242: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/242.jpg)
Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend
Colour.new(255, 0, 0)
![Page 243: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/243.jpg)
Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend
Colour.new(255, 0, 0)
![Page 244: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/244.jpg)
Constructors
![Page 245: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/245.jpg)
Constructorsclass BasicObject def self.new(*args, &block) instance = allocate instance.initialize(*args, &block) instance endend
![Page 246: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/246.jpg)
Constructors
![Page 247: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/247.jpg)
Constructorsclass Colour def self.new_with_hex(*args, &block) instance = allocate instance.init_with_hex(*args, &block) instance end
![Page 248: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/248.jpg)
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](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/249.jpg)
Constructors
![Page 250: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/250.jpg)
ConstructorsColour.new(255, 0, 0)
![Page 251: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/251.jpg)
ConstructorsColour.new(255, 0, 0)
Colour.new_with_hex(“ff0000”)
![Page 252: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/252.jpg)
![Page 253: Ruby nooks & crannies](https://reader031.vdocuments.site/reader031/viewer/2022030403/544f44e7af79593b028b7735/html5/thumbnails/253.jpg)
end