Elegant way in Ruby to find first of possible matching hash keys

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)

Ste*_*fan 5

我会使用findand反过来解决它has_key?

[:user_id, :app_id].find { |k| input.has_key?(k) }
Run Code Online (Sandbox Code Playgroud)