RubyNooks & CranniesKerry Buckley, IPRUG 4 December 2012
% notation
% notation%[foo “bar” ‘baz’ #{2 + 2}]
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%Q[foo “bar” ‘baz’ #{2 + 2}]
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%q[foo "bar" baz ...
% notation%[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%Q[foo “bar” ‘baz’ #{2 + 2}]=> "foo "bar" baz 4"%q[foo "bar" baz ...
% notation
% notation%{foo}
% notation%{foo}=> "foo"
% notation%{foo}=> "foo"%|bar|
% notation%{foo}=> "foo"%|bar|=> "bar"
% notation%{foo}=> "foo"%|bar|=> "bar"%$#@_$
% notation%{foo}=> "foo"%|bar|=> "bar"%$#@_$=> “”
% notation
% notation%w[foo bar baz]
% notation%w[foo bar baz]=> ["foo", "bar", "baz"]
% notation%w[foo bar baz]=> ["foo", "bar", "baz"]%w[foo bar b#{2+2}z]
% notation%w[foo bar baz]=> ["foo", "bar", "baz"]%w[foo bar b#{2+2}z]=> ["foo", "bar", "b#{2+2}z"]
% 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]
% 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]=> [...
% notation
% notation%r{^foo/bar/d*$}i
% notation%r{^foo/bar/d*$}i=> /^foo/bar/d*$/i
% notation%r{^foo/bar/d*$}i=> /^foo/bar/d*$/i%s{foo bar}
% notation%r{^foo/bar/d*$}i=> /^foo/bar/d*$/i%s{foo bar}=> :"foo bar"
% notation%r{^foo/bar/d*$}i=> /^foo/bar/d*$/i%s{foo bar}=> :"foo bar"%x{head -1 /etc/paths}
% notation%r{^foo/bar/d*$}i=> /^foo/bar/d*$/i%s{foo bar}=> :"foo bar"%x{head -1 /etc/paths}=> "/usr/binn"
*
*"hello " * 3
*"hello " * 3=> "hello hello hello "
*"hello " * 3=> "hello hello hello "[1, 2, 3] * 2
*"hello " * 3=> "hello hello hello "[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3]
*"hello " * 3=> "hello hello hello "[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3][1, 2, 3] * " "
*"hello " * 3=> "hello hello hello "[1, 2, 3] * 2=> [1, 2, 3, 1, 2, 3][1, 2, 3] * " "=> "1 2 3"
Substring match
Substring matchstr = "foo bar"
Substring matchstr = "foo bar"str =~ /o./
Substring matchstr = "foo bar"str =~ /o./=> 1
Substring matchstr = "foo bar"str =~ /o./=> 1str =~ /xx/
Substring matchstr = "foo bar"str =~ /o./=> 1str =~ /xx/=> nil
Substring match
Substring matchstr = "foo bar"str.match /o./
Substring matchstr = "foo bar"str.match /o./=> #<MatchData "oo">
Substring matchstr = "foo bar"str.match /o./=> #<MatchData "oo">str.match /x/
Substring matchstr = "foo bar"str.match /o./=> #<MatchData "oo">str.match /x/=> nil
Substring matchstr = "foo bar"str.match /o./=> #<MatchData "oo">str.match /x/=> nilstr.match “oo”
Substring matchstr = "foo bar"str.match /o./=> #<MatchData "oo">str.match /x/=> nilstr.match “oo”=> #<MatchData "oo">
Substring match
Substring matchstr = "foo bar"str[/o./]
Substring matchstr = "foo bar"str[/o./]=> "oo"
Substring matchstr = "foo bar"str[/o./]=> "oo"str[/x/]
Substring matchstr = "foo bar"str[/o./]=> "oo"str[/x/]=> nil
Substring matchstr = "foo bar"str[/o./]=> "oo"str[/x/]=> nilstr[“oo”]
Substring matchstr = "foo bar"str[/o./]=> "oo"str[/x/]=> nilstr[“oo”]=> "oo"
Blocks & procs
Blocks & procs%w{foo bar}.map {|a| a.reverse }
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]flip = proc {|a| a.reverse }
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]flip = proc {|a| a.reverse }{foo bar}.map &flip
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]flip = proc {|a| a.reverse }{foo bar}.map &flip=> ["oof", "...
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]flip = ->(a){ a.reverse }{foo bar}.map &flip=> ["oof", "rab"]
Blocks & procs
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]{foo bar}.map &:reverse
Blocks & procs%w{foo bar}.map {|a| a.reverse }=> ["oof", "rab"]{foo bar}.map &:reverse=> ["oof", "rab"]
Blocks & procs
Blocks & procsClass Symbol  def to_proc    Proc.new do |*args|      args.shift.__send__(self, *args)    end  endend
Blocks & procs
Blocks & procsflip = ->(a){ a.reverse }
Blocks & procsflip = ->(a){ a.reverse }flip.call("GURPI")
Blocks & procsflip = ->(a){ a.reverse }flip.call("GURPI")=> "IPRUG"
Blocks & procsflip = ->(a){ a.reverse }flip.call("GURPI")=> "IPRUG"flip.("GURPI")=> "IPRUG"
Blocks & procsflip = ->(a){ a.reverse }flip.call("GURPI")=> "IPRUG"flip.("GURPI")=> "IPRUG"flip["GURPI"]=> "IPRUG"
tap
tap(1..5).map{|a| a**2}      .select(&:odd?)      .inject(&:+)=> 35
tap
tap(1..5).map{|a| a**2}      .select(&:odd?)      .tap{|a| p a}      .inject(&:+)
tap(1..5).map{|a| a**2}      .select(&:odd?)      .tap{|a| p a}      .inject(&:+)[1, 9, 25]=> 35
tap
tapdef create_foo  Foo.new.tap do|foo|    foo.bar = 42    foo.baz = 69  endend
Multiple assignment
Multiple assignmenta, b = 1, 2
Multiple assignmenta, b = 1, 2a=> 1b=> 2
Multiple assignment
Multiple assignmentarray = [1, 2]a, b = array
Multiple assignmentarray = [1, 2]a, b = arraya=> 1b=> 2
Multiple assignment
Multiple assignmenta, b, c = 1, 2
Multiple assignmenta, b, c = 1, 2c
Multiple assignmenta, b, c = 1, 2c=> nil
Multiple assignmenta, b, c = 1, 2c=> nila, b = 1, 2, 3
Multiple assignmenta, b, c = 1, 2c=> nila, b = 1, 2, 3b
Multiple assignmenta, b, c = 1, 2c=> nila, b = 1, 2, 3b=> 2
Multiple assignment
Multiple assignmenta, *b = 1, 2, 3, 4
Multiple assignmenta, *b = 1, 2, 3, 4a
Multiple assignmenta, *b = 1, 2, 3, 4a=> 1
Multiple assignmenta, *b = 1, 2, 3, 4a=> 1b
Multiple assignmenta, *b = 1, 2, 3, 4a=> 1b=> [2, 3, 4]
Multiple assignment
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b=> [2, 3, 4]
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b=> [2, 3, 4]c
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b=> [2, 3, 4]c=> 5
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b=> [2, 3, 4]c=> 5d
Multiple assignmenta, *b, c, d = 1, 2, 3, 4, 5, 6b=> [2, 3, 4]c=> 5d=> 6
Multiple assignment
Multiple assignmentarray = [1, 2, 3, 4]a, _, _, b = array
Multiple assignmentarray = [1, 2, 3, 4]a, _, _, b = arraya=> 1
Multiple assignmentarray = [1, 2, 3, 4]a, _, _, b = arraya=> 1b=> 4
Multiple assignmentarray = [1, 2, 3, 4]a, _, _, b = arraya=> 1b=> 4a, *_, b = array
Multiple assignment
Multiple assignmentarray = [[1, 2], [3, 4]]
Multiple assignmentarray = [[1, 2], [3, 4]]array.map {|a| a[0] * a[1] }
Multiple assignmentarray = [[1, 2], [3, 4]]array.map {|a| a[0] * a[1] }=> [2, 12]
Multiple assignmentarray = [[1, 2], [3, 4]]array.map {|a| a[0] * a[1] }=> [2, 12]array.map {|a, b| a * b }
Multiple assignmentarray = [[1, 2], [3, 4]]array.map {|a| a[0] * a[1] }=> [2, 12]array.map {|a, b| a * b }=> [2, 12]
Multiple assignment
Multiple assignmentarray = [[1, [2, 3]],         [3, [4, 5]]]
Multiple assignmentarray = [[1, [2, 3]],         [3, [4, 5]]]array.map {|a, (b, _)| a * b }
Multiple assignmentarray = [[1, [2, 3]],         [3, [4, 5]]]array.map {|a, (b, _)| a * b }=> [2, 12]
Multiple assignment
Multiple assignmenthash = {foo: 1, bar: 2}
Multiple assignmenthash = {foo: 1, bar: 2}hash.map {|a| a }
Multiple assignmenthash = {foo: 1, bar: 2}hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]
Multiple assignmenthash = {foo: 1, bar: 2}hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]hash.map {|k,v| "#{k} -> #{v}" }
Multiple assignmenthash = {foo: 1, bar: 2}hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]hash.map {|k,v| "#{k} -> #{v}" }=> ["f...
Multiple assignment
Multiple assignmentpeople = {  fred: [30, "blue", :male],  freda: [21, "brown", :female]}
Multiple assignmentpeople = {  fred: [30, "blue", :male],  freda: [21, "brown", :female]}people.map {|p|  "#{p[0]} is #{p[...
Multiple assignmentpeople = {  fred: [30, "blue", :male],  freda: [21, "brown", :female]}people.map {|p|  "#{p[0]} is #{p[...
Multiple assignmentpeople = {  fred: [30, "blue", :male],  freda: [21, "brown", :female]}people.map {|name, attrs|  "#{nam...
Multiple assignmentpeople = {  fred: [30, "blue", :male],  freda: [21, "brown", :female]}people.map {|name, (age, *_)|  "#...
Type coercion
Type coercion123.to_s
Type coercion123.to_s=> "123"
Type coercion123.to_s=> "123":foo.to_s
Type coercion123.to_s=> "123":foo.to_s=> "foo"
Type coercion123.to_s=> "123":foo.to_s=> "foo"[1, 2].to_s
Type coercion123.to_s=> "123":foo.to_s=> "foo"[1, 2].to_s=> "[1, 2]"
Type coercion
Type coercion"123".to_i
Type coercion"123".to_i=> 123
Type coercion"123".to_i=> 1231.2.to_i
Type coercion"123".to_i=> 1231.2.to_i=> 1
Type coercion"123".to_i=> 1231.2.to_i=> 1"foo".to_i
Type coercion"123".to_i=> 1231.2.to_i=> 1"foo".to_i=> 0
Type coercion
Type coercion"1.2".to_f
Type coercion"1.2".to_f=> 1.2
Type coercion"1.2".to_f=> 1.21.to_f
Type coercion"1.2".to_f=> 1.21.to_f=> 1.0
Type coercion"1.2".to_f=> 1.21.to_f=> 1.0"foo".to_f
Type coercion"1.2".to_f=> 1.21.to_f=> 1.0"foo".to_f=> 0.0
Type coercion
Type coercion1.2.to_int
Type coercion1.2.to_int=> 1
Type coercion1.2.to_int=> 1"123".to_int
Type coercion1.2.to_int=> 1"123".to_intNoMethodError: undefined method  `to_int for "123":String
Type coercion1.2.to_int=> 1"123".to_intNoMethodError: undefined method  `to_int for "123":String:foo.to_str
Type coercion1.2.to_int=> 1"123".to_intNoMethodError: undefined method  `to_int for "123":String:foo.to_strNoMethodError: ...
Type coercion
Type coercionInteger(123)
Type coercionInteger(123)=> 123
Type coercionInteger(123)=> 123Integer("123")
Type coercionInteger(123)=> 123Integer("123")=> 123
Type coercionInteger(123)=> 123Integer("123")=> 123Integer(1.23)
Type coercionInteger(123)=> 123Integer("123")=> 123Integer(1.23)=> 1
Type coercion
Type coercionString(“foo”)
Type coercionString(“foo”)=> “foo”
Type coercionString(“foo”)=> “foo”String(123)
Type coercionString(“foo”)=> “foo”String(123)=> “123”
Type coercionString(“foo”)=> “foo”String(123)=> “123”String(:foo)
Type coercionString(“foo”)=> “foo”String(123)=> “123”String(:foo)=> “foo”
Type coercion
Type coercionArray(“foo”)
Type coercionArray(“foo”)=> [“foo”]
Type coercionArray(“foo”)=> [“foo”]Array([“foo”, “bar”])
Type coercionArray(“foo”)=> [“foo”]Array([“foo”, “bar”])=> [“foo”, “bar”]
Type coercion
Type coerciondef echo *args  Array(args).each {|a| puts a }end
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho "foo", "bar"
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho "foo", "bar"foobar
Type coercion
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho "foo"
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho "foo"foo
Type coercion
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho ["foo", "bar"]
Type coerciondef echo *args  Array(args).each {|a| puts a }endecho ["foo", "bar"]foobar
Mapping hashes
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}squares = {}numbers.each do |k, v|  squares[k] = v**2end
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}squares = {}numbers.each do |k, v|  squares[k] = v**2endsquare...
Mapping hashes
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}numbers.map {|k, v| [k, v**2] }
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}numbers.map {|k, v| [k, v**2] }=> [[:one, 1], [:two, 4],    [:...
Mapping hashes
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}numbers.map {|k, v|  [k, v**2] }.flatten
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}numbers.map {|k, v|  [k, v**2] }.flatten=> [:one, 1, :two, 4, ...
Mapping hashes
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}Hash[*numbers.map {|k, v|  [k, v**2] }.flatten]
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}Hash[*numbers.map {|k, v|  [k, v**2] }.flatten]=> {:one=>1, :t...
Mapping hashes
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}Hash[*numbers.flat_map {|k, v|  [k, v**2] }]
Mapping hashesnumbers = {one: 1, two: 2,           three: 3}Hash[*numbers.flat_map {|k, v|  [k, v**2] }]=> {:one=>1, :two=...
Anonymous classes
Anonymous classesfoo = Class.new do  def hello    "hello"  endend
Anonymous classesfoo = Class.new do  def hello    "hello"  endendfoo.new.hello
Anonymous classesfoo = Class.new do  def hello    "hello"  endendfoo.new.hello=> "hello"
Struct & OpenStruct
Struct & OpenStructPerson = Struct.new :name, :age
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry",     age=43>
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry",     age=43>m...
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry",     age=43>m...
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry",     age=43>m...
Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry",     age=43>m...
Struct & OpenStruct
Struct & OpenStructPerson = Struct.new :name do  def greet    puts "Hello, Im #{name}"  endend
Struct & OpenStructPerson = Struct.new :name do  def greet    puts "Hello, Im #{name}"  endendPerson.new("Kerry").greet
Struct & OpenStructPerson = Struct.new :name do  def greet    puts "Hello, Im #{name}"  endendPerson.new("Kerry").greetHel...
Struct & OpenStruct
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)=> #<OpenStruct name="Kerry",     age=43>
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)=> #<OpenStruct name="Kerry",     age=43>me.age
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)=> #<OpenStruct name="Kerry",     age=43>me.age=> 43
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)=> #<OpenStruct name="Kerry",     age=43>me.age=> 43m...
Struct & OpenStructme = OpenStruct.new(       name: "Kerry", age: 43)=> #<OpenStruct name="Kerry",     age=43>me.age=> 43m...
Struct & OpenStruct
Struct & OpenStructstruct.members
Struct & OpenStructstruct.members=> [:name, :age]
Struct & OpenStructstruct.members=> [:name, :age]struct.values
Struct & OpenStructstruct.members=> [:name, :age]struct.values=> ["Kerry", 43]
Struct & OpenStructstruct.members=> [:name, :age]struct.values=> ["Kerry", 43]struct.each_pair {|k, v|  puts "#{k}: #{v}" }
Struct & OpenStructstruct.members=> [:name, :age]struct.values=> ["Kerry", 43]struct.each_pair {|k, v|  puts "#{k}: #{v}" ...
Constructors
Constructorsclass Colour  def initialize(r, g, b)    @r, @g, @b = r, g, b  endend
Constructorsclass Colour  def initialize(r, g, b)    @r, @g, @b = r, g, b  endendColour.new(255, 0, 0)
Constructorsclass Colour  def initialize(r, g, b)    @r, @g, @b = r, g, b  endendColour.new(255, 0, 0)
Constructors
Constructorsclass BasicObject  def self.new(*args, &block)    instance = allocate    instance.initialize(*args,           ...
Constructors
Constructorsclass Colour  def self.new_with_hex(*args, &block)    instance = allocate    instance.init_with_hex(*args, &bl...
Constructorsclass Colour  def self.new_with_hex(*args, &block)    instance = allocate    instance.init_with_hex(*args, &bl...
Constructors
ConstructorsColour.new(255, 0, 0)
ConstructorsColour.new(255, 0, 0)Colour.new_with_hex(“ff0000”)
end
Ruby nooks & crannies
Upcoming SlideShare
Loading in...5
×

Ruby nooks & crannies

301

Published on

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

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
301
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \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

    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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×