This isn't quite as readable as @sethcall's answer, but it should be be fairly readable if you know some Ruby idioms:
def get_let(response)
responses = [response.css('A'), response.css('B')]
responses.detect { |response| !response.empty? } || ''
end
detect returns the first result for which the block doesn't return false. This has the advantage of avoiding conditionals, if that's something you're going for. If you want to do without the || in the above answer, you could do this:
def get_let(response)
responses = [response.css('A'), response.css('B')]
responses.detect(-> { '' }) { |response| !response.empty? }
end
I don't find that second solution to be nearly as intuitive as the first solution, though. It would be terrific if you could just specify an empty string as an argument. However, the argument for detect and its alias, find, must be nil or something that responds to the call method, like a lambda or proc. There's really no reason to pass in nil, since that's the default value.
If you knew for sure that the response.css method would not return an array with nil or false values in it, you could try this solution:
def get_let(response)
responses = [response.css('A'), response.css('B')]
responses.detect(&:any?) || ''
end
See the Ruby docs to read more about how detect works. Here are the docs on any?.