Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created August 3, 2012 12:30
Show Gist options
  • Save jcasimir/3247167 to your computer and use it in GitHub Desktop.
Save jcasimir/3247167 to your computer and use it in GitHub Desktop.
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
@roryokane
Copy link

This Gist was posted alongside a tweet by the author:

Any ideas to refactor this little ruby collection operation? https://gist.github.com/3247167

@Yardboy
Copy link

Yardboy commented Aug 6, 2012

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