LoginSignup
12
7

More than 5 years have passed since last update.

tap のブロックの中で break すると戻り値を返せる

Last updated at Posted at 2015-01-18

つまり、こう。

hoge.a=nil

hoge.a.tap{|x| break "nil !" unless x }

#=> "nil !"

知らなかった。

たとえば Ruby on Rails などを利用する場合、

if @session.current_user.profile 
  "#{@session.current_user.profile.name} さん"
else
  "ログインしていません"
end

的なことをよくやることになりがち。

そんなイライラが貯まっているあなたには、もしかしたら使い道があるかも… いや、読みづらいかな。ナシかな。

(例が rails 前提ではないのかというご指摘をいただき、それもそうかと修正しました。 @riocampos さん、ありがとうございます)

12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7