Skip to content

Instantly share code, notes, and snippets.

@upinetree
Created April 8, 2014 15:06
Show Gist options
  • Save upinetree/10139211 to your computer and use it in GitHub Desktop.
Save upinetree/10139211 to your computer and use it in GitHub Desktop.
reverse_number
def reverse_number(num)
(Math.log10(num) + 1).floor.times.map{ |digit| (num/10**digit)%10 }.inject{ |sum, n| sum*10 + n }
end
reverse_number 987654321
# => 123456789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment