quattro_4 scribble

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

RubyTapas #110 - #114

110 Catch And Throw

begin
  parser.parse(html) unless html.nil?
rescue DoneException
  # we are done
end

catch(:done) do
  parser.parse(html) unless html.nil?
end

+

throw :done

perfectly normal early returns

大域脱出catch, throw知らなかった お気楽 Ruby プログラミング入門

111 Symbol Placeholder

★★★

NoMethodError for nil は分かりにくいが

credentials = options.fetch(:credentials) { :credentials_not_set }
credentials.fetch(:user)
#=> undefined method `fetch' for :credentials_not_set:Symbol (NoMethodError)

symbol値がエラーに表示されることで分かりやすい

標準的っぽくないけど、分かりやすいこのやり方はなんか好きだな

112 Special Case

★★

if current_user

が多いコードをきれいにする

def current_user
  # return User OR GuestUser

各メソッドをUser/GuestUserどちらでも持つようにする

def name
def authenticated?
def has_role?(role)
def cart

113 p

★★★

puts str.inspect
puts html.inspect

string, symbolなどinspectでclassの区別がつく

これは次と同等

p str
p html

一行でも書ける

p str, html, ...

putsはnilを返す、pは結果を返す

p(phrase.reverse) == phrase

のように == と使える

細かいpの使い方を実は知らなかった

すごい役に立ちそう

114 Null Object

なんか動画のオープニング映像が、BGMが映画みたいな

class NullLogger
  def debug(*); end

Nullの発音て「ナル」「ナゥ」って感じ

あまり頭に入ってこなかった