Skip to content

Instantly share code, notes, and snippets.

@youcune
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youcune/21e6b1a81a2bf057292a to your computer and use it in GitHub Desktop.
Save youcune/21e6b1a81a2bf057292a to your computer and use it in GitHub Desktop.
try_chain depends on ActiveSupport
class Object
def try_chain(*args)
args.inject(self) { |_, method| _.try(method) }
end
end
@youcune
Copy link
Author

youcune commented Nov 15, 2014

たとえば omniauth-facebook 使って最終学歴を取りたいとき

request.env['omniauth.auth'].try_chain(:extra, :raw_info, :education, :last, :school, :name)

って書ける。途中どこかでコケたら nil になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment