Ben*_*Ben 2 ruby ruby-on-rails-4 ruby-2.2
Lets say I have user input that can be either this:
input = { user_id: 5, ... }
Run Code Online (Sandbox Code Playgroud)
or this:
input = { app_id: 5, ... }
Run Code Online (Sandbox Code Playgroud)
And I want to return either :user_id or :app_id depending on which is provided. I can do this:
(input.keys & [:user_id, :app_id]).first
Run Code Online (Sandbox Code Playgroud)
Is there a more elegant, more rubyish, idiomatic way of doing this?
Is this better or worse than above?:
input.slice(:user_id, :app_id).keys.first
Run Code Online (Sandbox Code Playgroud)
(Answers don't need to be strictly from Ruby 2.2 stdlib, Rails methods welcome as well)
| 归档时间: |
|
| 查看次数: |
2857 次 |
| 最近记录: |