More Related Content Similar to Ruby nooks & crannies (20) More from Kerry Buckley (20) Ruby nooks & crannies6. % 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}"
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"]
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"
48. Substring match
str = "foo bar"
str.match /o./
=> #<MatchData "oo">
str.match /x/
=> nil
str.match “oo”
49. Substring match
str = "foo bar"
str.match /o./
=> #<MatchData "oo">
str.match /x/
=> nil
str.match “oo”
=> #<MatchData "oo">
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"]
67. Blocks & procs
%w{foo bar}.map {|a| a.reverse }
=> ["oof", "rab"]
{foo bar}.map &:reverse
=> ["oof", "rab"]
69. Blocks & procs
Class Symbol
def to_proc
Proc.new do |*args|
args.shift.__send__(self, *args)
end
end
end
74. Blocks & procs
flip = ->(a){ a.reverse }
flip.call("GURPI")
=> "IPRUG"
flip.("GURPI")
=> "IPRUG"
75. Blocks & procs
flip = ->(a){ a.reverse }
flip.call("GURPI")
=> "IPRUG"
flip.("GURPI")
=> "IPRUG"
flip["GURPI"]
=> "IPRUG"
105. Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
=> [2, 3, 4]
106. Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
=> [2, 3, 4]
c
107. Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
=> [2, 3, 4]
c
=> 5
108. Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
=> [2, 3, 4]
c
=> 5
d
109. Multiple assignment
a, *b, c, d = 1, 2, 3, 4, 5, 6
b
=> [2, 3, 4]
c
=> 5
d
=> 6
130. 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"]
134. 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"]
135. 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"]
136. Multiple assignment
people = {
fred: [30, "blue", :male],
freda: [21, "brown", :female]
}
people.map {|name, (age, *_)|
"#{name} is #{age}"
}
=> ["fred is 40", "freda is 21"]
197. Mapping hashes
numbers = {one: 1, two: 2,
three: 3}
squares = {}
numbers.each do |k, v|
squares[k] = v**2
end
squares
=> {:one=>1, :two=>4, :three=>9}
200. Mapping hashes
numbers = {one: 1, two: 2,
three: 3}
numbers.map {|k, v| [k, v**2] }
=> [[:one, 1], [:two, 4],
[:three, 9]]
203. Mapping hashes
numbers = {one: 1, two: 2,
three: 3}
numbers.map {|k, v|
[k, v**2] }.flatten
=> [:one, 1, :two, 4, :three, 9]
206. Mapping hashes
numbers = {one: 1, two: 2,
three: 3}
Hash[*numbers.map {|k, v|
[k, v**2] }.flatten]
=> {:one=>1, :two=>4, :three=>9}
209. Mapping hashes
numbers = {one: 1, two: 2,
three: 3}
Hash[*numbers.flat_map {|k, v|
[k, v**2] }]
=> {:one=>1, :two=>4, :three=>9}
218. Struct & OpenStruct
Person = Struct.new :name, :age
me = Person.new "Kerry", 43
=> #<struct Person name="Kerry",
age=43>
me.age
219. Struct & OpenStruct
Person = Struct.new :name, :age
me = Person.new "Kerry", 43
=> #<struct Person name="Kerry",
age=43>
me.age
=> 43
220. Struct & OpenStruct
Person = Struct.new :name, :age
me = Person.new "Kerry", 43
=> #<struct Person name="Kerry",
age=43>
me.age
=> 43
me.age = 30
221. Struct & OpenStruct
Person = Struct.new :name, :age
me = Person.new "Kerry", 43
=> #<struct Person name="Kerry",
age=43>
me.age
=> 43
me.age = 30
=> 30
225. Struct & OpenStruct
Person = Struct.new :name do
def greet
puts "Hello, I'm #{name}"
end
end
Person.new("Kerry").greet
Hello, I'm Kerry
229. Struct & OpenStruct
me = OpenStruct.new(
name: "Kerry", age: 43)
=> #<OpenStruct name="Kerry",
age=43>
me.age
230. Struct & OpenStruct
me = OpenStruct.new(
name: "Kerry", age: 43)
=> #<OpenStruct name="Kerry",
age=43>
me.age
=> 43
231. Struct & OpenStruct
me = OpenStruct.new(
name: "Kerry", age: 43)
=> #<OpenStruct name="Kerry",
age=43>
me.age
=> 43
me.age = 30
232. Struct & OpenStruct
me = OpenStruct.new(
name: "Kerry", age: 43)
=> #<OpenStruct name="Kerry",
age=43>
me.age
=> 43
me.age = 30
=> 30
247. Constructors
class Colour
def self.new_with_hex(*args, &block)
instance = allocate
instance.init_with_hex(*args, &block)
instance
end
248. Constructors
class Colour
def self.new_with_hex(*args, &block)
instance = allocate
instance.init_with_hex(*args, &block)
instance
end
def init_with_hex(name)
...
end
end