Upload
kerry-buckley
View
766
Download
0
Embed Size (px)
Citation preview
Kerry Buckley, IPRUG 4 December 2012
RubyNooks & Crannies
% 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/bin\n"
*
*"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./=> 1
str =~ /xx/
Substring matchstr = "foo bar"
str =~ /o./=> 1
str =~ /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/=> nil
str.match “oo”
Substring matchstr = "foo bar"
str.match /o./=> #<MatchData "oo">
str.match /x/=> nil
str.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/]=> nil
str[“oo”]
Substring matchstr = "foo bar"
str[/o./]=> "oo"
str[/x/]=> nil
str[“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 assignment
a, b = 1, 2
Multiple assignment
a, b = 1, 2
a=> 1
b=> 2
Multiple assignment
Multiple assignment
array = [1, 2]a, b = array
Multiple assignment
array = [1, 2]a, b = array
a=> 1
b=> 2
Multiple assignment
Multiple assignment
a, b, c = 1, 2
Multiple assignment
a, b, c = 1, 2
c
Multiple assignment
a, b, c = 1, 2
c=> nil
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
b
Multiple assignment
a, b, c = 1, 2
c=> nil
a, b = 1, 2, 3
b=> 2
Multiple assignment
Multiple assignment
a, *b = 1, 2, 3, 4
Multiple assignment
a, *b = 1, 2, 3, 4
a
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
b
Multiple assignment
a, *b = 1, 2, 3, 4
a=> 1
b=> [2, 3, 4]
Multiple assignment
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c=> 5
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c=> 5
d
Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b=> [2, 3, 4]
c=> 5
d=> 6
Multiple assignment
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
b=> 4
Multiple assignment
array = [1, 2, 3, 4]a, _, _, b = array
a=> 1
b=> 4
a, *_, b = array
Multiple assignment
Multiple assignment
array = [[1, 2], [3, 4]]
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }=> [2, 12]
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }=> [2, 12]
array.map {|a, b| a * b }
Multiple assignment
array = [[1, 2], [3, 4]]
array.map {|a| a[0] * a[1] }=> [2, 12]
array.map {|a, b| a * b }=> [2, 12]
Multiple assignment
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
array.map {|a, (b, _)| a * b }
Multiple assignment
array = [[1, [2, 3]], [3, [4, 5]]]
array.map {|a, (b, _)| a * b }=> [2, 12]
Multiple assignment
Multiple assignment
hash = {foo: 1, bar: 2}
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]
hash.map {|k,v| "#{k} -> #{v}" }
Multiple assignment
hash = {foo: 1, bar: 2}
hash.map {|a| a }=> [[:foo, 1], [:bar, 2]]
hash.map {|k,v| "#{k} -> #{v}" }=> ["foo -> 1", "bar -> 2"]
Multiple assignment
Multiple assignment
people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}
Multiple assignment
people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}
people.map {|p| "#{p[0]} is #{p[1][0]}"}
Multiple assignment
people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}
people.map {|p| "#{p[0]} is #{p[1][0]}"}=> ["fred is 40", "freda is 21"]
Multiple assignment
people = { fred: [30, "blue", :male], freda: [21, "brown", :female]}
people.map {|name, attrs| "#{name} is #{attrs.first}"}=> ["fred is 40", "freda is 21"]
Multiple assignment
people = { 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=> 123
1.2.to_i
Type coercion"123".to_i=> 123
1.2.to_i=> 1
Type coercion"123".to_i=> 123
1.2.to_i=> 1
"foo".to_i
Type coercion"123".to_i=> 123
1.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.2
1.to_f
Type coercion"1.2".to_f=> 1.2
1.to_f=> 1.0
Type coercion"1.2".to_f=> 1.2
1.to_f=> 1.0
"foo".to_f
Type coercion"1.2".to_f=> 1.2
1.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)=> 123
Integer("123")
Type coercionInteger(123)=> 123
Integer("123")=> 123
Type coercionInteger(123)=> 123
Integer("123")=> 123
Integer(1.23)
Type coercionInteger(123)=> 123
Integer("123")=> 123
Integer(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 }end
echo "foo", "bar"
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo", "bar"foobar
Type coercion
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo"
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo "foo"foo
Type coercion
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo ["foo", "bar"]
Type coerciondef echo *args Array(args).each {|a| puts a }end
echo ["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**2end
squares=> {: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 classes
foo = Class.new do def hello "hello" endend
Anonymous classes
foo = Class.new do def hello "hello" endend
foo.new.hello
Anonymous classes
foo = Class.new do def hello "hello" endend
foo.new.hello=> "hello"
Struct & OpenStruct
Struct & OpenStruct
Person = Struct.new :name, :age
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
me.age
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
me.age=> 43
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
me.age=> 43
me.age = 30
Struct & OpenStruct
Person = Struct.new :name, :ageme = Person.new "Kerry", 43=> #<struct Person name="Kerry", age=43>
me.age=> 43
me.age = 30=> 30
Struct & OpenStruct
Struct & OpenStruct
Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend
Struct & OpenStruct
Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend
Person.new("Kerry").greet
Struct & OpenStruct
Person = Struct.new :name do def greet puts "Hello, I'm #{name}" endend
Person.new("Kerry").greetHello, I'm Kerry
Struct & OpenStruct
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age=> 43
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age=> 43
me.age = 30
Struct & OpenStruct
me = OpenStruct.new( name: "Kerry", age: 43)=> #<OpenStruct name="Kerry", age=43>
me.age=> 43
me.age = 30=> 30
Struct & OpenStruct
Struct & OpenStruct
struct.members
Struct & OpenStruct
struct.members=> [:name, :age]
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values=> ["Kerry", 43]
Struct & OpenStruct
struct.members=> [:name, :age]
struct.values=> ["Kerry", 43]
struct.each_pair {|k, v| puts "#{k}: #{v}" }
Struct & OpenStruct
struct.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 endend
Colour.new(255, 0, 0)
Constructorsclass Colour def initialize(r, g, b) @r, @g, @b = r, g, b endend
Colour.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