Skip to content

Instantly share code, notes, and snippets.

@carlosantoniodasilva
Created September 27, 2010 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlosantoniodasilva/599566 to your computer and use it in GitHub Desktop.
Save carlosantoniodasilva/599566 to your computer and use it in GitHub Desktop.
## Original
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
if s.match(/\s(asc|ASC)$/)
s.gsub(/\s(asc|ASC)$/, ' DESC')
elsif s.match(/\s(desc|DESC)$/)
s.gsub(/\s(desc|DESC)$/, ' ASC')
else
s + ' DESC'
end
}
end
## Modified
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
if s.match(/\s(asc|ASC)$/)
s.gsub!(/\s(asc|ASC)$/, ' DESC')
elsif s.match(/\s(desc|DESC)$/)
s.gsub!(/\s(desc|DESC)$/, ' ASC')
else
s.concat(' DESC')
end
s
}
end
## Modified - don't create match object
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
if s =~ /\s(asc|ASC)$/
s.gsub!(/\s(asc|ASC)$/, ' DESC')
elsif s =~ /\s(desc|DESC)$/
s.gsub!(/\s(desc|DESC)$/, ' ASC')
else
s.concat(' DESC')
end
s
}
end
## Modified - case?
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
case s
when /\s(asc|ASC)$/
s.gsub!(/\s(asc|ASC)$/, ' DESC')
when /\s(desc|DESC)$/
s.gsub!(/\s(desc|DESC)$/, ' ASC')
else
s.concat(' DESC')
end
s
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment