Skip to content

Instantly share code, notes, and snippets.

@holysugar
Created October 19, 2011 14:15
Show Gist options
  • Save holysugar/1298406 to your computer and use it in GitHub Desktop.
Save holysugar/1298406 to your computer and use it in GitHub Desktop.
order sample?
# "created_at/desc" とか書けるようにしていいなら(delimiter は相談.)
order_strings.map{|o|
name, order = o.split('/', 2)
column_names,
order = 'asc' if order != 'desc'
column_names.include?(name) ? "#{name} #{order}" : nil
}.compact.join(', ')
# あるいは
order_strings.map{|o|
_, name, order = o.match(/\A(.+?)(?:_(desc|asc))?\z/).to_a
next unless column_names.include?(name)
"#{name} #{order}"
}.compact.join(', ')
# あとは map + join じゃなく each_with_object を使うとか……
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment