Created
August 3, 2012 12:30
-
-
Save jcasimir/3247167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOOKUP_CHAIN = [:params, :user, :session, :http, :default] | |
def self.set_by(inputs) | |
locale = nil | |
LOOKUP_CHAIN.each do |lookup| | |
locale = send("by_#{lookup}", inputs[lookup]) | |
break if locale | |
end | |
return locale | |
end |
I just found out that you can make more than one fork of a Gist. So here’s a Gist fork for the test harness I posted earlier.
This Gist was posted alongside a tweet by the author:
Any ideas to refactor this little ruby collection operation? https://gist.github.com/3247167
Thought about this the other day but just got around to trying it this afternoon. I believe #any?
stops when it reaches a truthy result, so how about this:
def self.set_by(inputs)
locale = nil
LOOKUP_CHAIN.any? { |lookup| locale = send("by_#{lookup}", inputs[lookup]) }
return locale
end
Passes the test harness.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here’s a refactoring:
It produces the same output in the test harness I posted previously.
Oh, and sorry for the 4-space indentation in that previous test-harness code. I forgot to convert from tabs to spaces, and GitHub won’t let me edit that comment.