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

Ruby nooks & crannies

on

  • 439 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
439
Views on SlideShare
437
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 Ruby nooks & crannies Presentation Transcript

  • 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 #{2 + 2}]
  • % 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}"
  • % 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]=> ["foo", "bar", "b4z"]
  • % 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", "rab"]
  • 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}" }=> ["foo -> 1", "bar -> 2"]
  • 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[1][0]}"}
  • 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"]
  • Multiple assignmentpeople = { fred: [30, "blue", :male], freda: [21, "brown", :female]}people.map {|name, attrs| "#{name} is #{attrs.first}"}=> ["fred is 40", "freda is 21"]
  • Multiple assignmentpeople = { fred: [30, "blue", :male], freda: [21, "brown", :female]}people.map {|name, (age, *_)| "#{name} is #{age}"}=> ["fred is 40", "freda is 21"]
  • 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: undefined method `to_str for :foo:Symbol
  • 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**2endsquares=> {:one=>1, :two=>4, :three=>9}
  • 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], [:three, 9]]
  • 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, :three, 9]
  • 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, :two=>4, :three=>9}
  • 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=>4, :three=>9}
  • 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>me.age
  • Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>me.age=> 43
  • Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>me.age=> 43me.age = 30
  • Struct & OpenStructPerson = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>me.age=> 43me.age = 30=> 30
  • 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").greetHello, Im Kerry
  • 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=> 43me.age = 30
  • Struct & OpenStructme = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>me.age=> 43me.age = 30=> 30
  • 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}" }name: Kerryage: 43
  • 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, &block) instance endend
  • Constructors
  • Constructorsclass Colour def self.new_with_hex(*args, &block) instance = allocate instance.init_with_hex(*args, &block) instance end
  • 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
  • Constructors
  • ConstructorsColour.new(255, 0, 0)
  • ConstructorsColour.new(255, 0, 0)Colour.new_with_hex(“ff0000”)
  • end