quattro_4 scribble

scribble 落書き (調べた事をただ落書きする)

RubyTapas #125 - #128

RubyTapas News

このダウンローダーを採用 bf4/downloader · GitHub

別の長いscreencast The Making of Cowsays.com | Tekpub

125 And/Or

★★★

and/or と &&/||

user_name = user && user.name
user_name                       # => "Avdi"
user_name = user and user.name
user_name                       # => #<struct name="Avdi">

代入とandを一緒に使ってはいけない
使って来なかったけど危ないのは知らなかった

and/orが使われるシーン

Perlの場合

chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

()が足りなくてエラー

line = $stdin.gets || raise "Can't read from STDIN" # !> possibly useless use of a literal in void context

orを使うとOK

line = $stdin.gets or raise "Can't read from STDIN"

別の例 (notify)

# Error
enable_notifications && notify "Something happened!"
# OK
enable_notifications and notify "Something happened!"
# Avdi prefers original
notify "Something happened!" if enable_notifications

use the English logical operators strictly as control-flow constructs rather than evaluating them for their return value

chunk = chunked_data.read(chunk_head.to_i) and
data << chunk

典型的な or dieみたいな場合以外はあまり使わない方が良い気はしている

126 Queue

Queue FIFO

Array LIFO

push/pop以外の分かりやすい書き方

require 'thread'

q = Queue.new
q.enq 123
q.enq 456
q.deq                           # => 123
q.deq                           # => 456

Queueあまり使う機会無かったから、難しい部分あまり分からなかったが、 実際使うときは仕組み完全理解しなくても、書きながら正しく動く書き方できれば良いと思ってる。

127 Parallel Fibonacci

★★

Programming Elixir の rewrite in Ruby

Scheduler部分

threads = (1..num_threads).map {
  thread_queue = Queue.new
  thread = Thread.new do
    mod.public_send(meth, my_queue, thread_queue)
  end
  {thread: thread, queue: thread_queue}
}

CPUと言語が対応してれば、2スレッド以上で速くなるが、
CPUに応じてスレッドを増やしても速くなくなる

Elixerでparallelism速くなる

MRIは効果無し

Rubinius, jRuby速くなる

実装の細かい部分のアルゴリズムとか書き方は昔ほど興味なくなってる。
自分で何の参照も無しに作れるようになろうとか思わなくなってる。

期待する速さで問題なく動くかどうかに関心がある。
ベンチマークは楽しい。

128 Enumerable Queue

a fully functional enumerable wrapper for a thread-safe queue

難しいことはあまりよくわからない