Ruby nooks & crannies
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Ruby nooks & crannies

on

  • 484 views

Some random Ruby features (tak from IPRUG, 4 December 2012)

Some random Ruby features (tak from IPRUG, 4 December 2012)

Statistics

Views

Total Views
484
Views on SlideShare
482
Embed Views
2

Actions

Likes
2
Downloads
0
Comments
0

1 Embed 2

http://www.hanrss.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Ruby nooks & crannies Presentation Transcript

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