Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby nooks & crannies

821 views

Published on

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

  • Be the first to comment

Ruby nooks & crannies

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

×