RubyTapas #156 - #161
156 Array.new
★★
special guest chef James Edward Gray II (@JEG2)
Ruby Rogues
>> generator = 3.times >> generator.next => 0 >> generator.next => 1 >> generator.map {|i| i ** 2 } => [0, 1, 4] >> generator.map { :whatever } => [:whatever, :whatever, :whatever]
Array.new + blockでも同じ
more natural
Array.new(3) { |i| i ** 2 } Array.new(3) { :whatever } codes = Array.new(200) { SecureRandom.hex(8) }
157 Lockstep Testing
?
"checkpointing" technique
Black box testing is fine for high-level acceptance tests.
But this feels wrong for detailed unit tests.
construct an adapter class we call Condition
#wait , #signal
Fibers, on the other hand, never run concurrently
tiny library to help us do "lockstep" testing with the help of Fibers
avdi/lockstep · GitHub
並列、ロック
長いけどピンと来ない
158 Constant Lookup Scope
★★
OK
module Planets module Jupiter Module.nesting # => [Planets::Jupiter, Planets] end end module Planets::Jupiter Module.nesting # => [Planets::Jupiter] end
NG
module Planets::Jupiter Module.nesting # => end module Planets module Jupiter Module.nesting # => end end # ~> -:1:in `<main>': uninitialized constant Planets (NameError)
shorthand form will only create the module at the tail end of the module nesting
always declare each level of nesting explicitly
Everything you ever wanted to know about constant lookup in Ruby
特に理由が無い限り module Planets::Jupiter
は使わない方が良い
159 Array Set Operations
★★
union operator |
intersection operator &
URI::HTTP.instance_methods - Object.instance_methods
|=
uniqを保証する
avdi_list = %W[milk granola cookies apples] original_list = avdi_list avdi_list |= ["granola"] avdi_list |= ["ice cream"] avdi_list # => ["milk", "granola", "cookies", "apples", "ice cream"] original_list # => ["milk", "granola", "cookies", "apples"]
かっこよくて個人では使いたいけど、知らなくて分からない人多そう
160 Reduce Redux
★
[1,2,3].reduce(0, :+) # => 6 [1,2,3].reduce(:+) # => 6 [].reduce(0, :+) # => 0 [].reduce(:+) # => nil
For this reason, I'm amending what I said in episode 149 to say that while you can omit the seed value to #reduce, in most cases you probably shouldn't.
オペレータだけ渡す書き方は非推奨
helper method to do "deep fetches"
def deep_fetch(data, *keys) keys.reduce(data) {|value, key| value.fetch(key) } end deep_fetch(data, "books", 0, "revisions", 1, "date") # => "2012-06-15"
#reduce has unexpected depth: not only is it great for mathematical reductions, it provides an elegant way to traverse data structures
redux - 戻ってきた
161 Thread Local Variable
?
ShmactiveRecord
さっぱり分からない
PStoreに続く