Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created January 30, 2013 15:00
Show Gist options
  • Select an option

  • Save ffbit/4673841 to your computer and use it in GitHub Desktop.

Select an option

Save ffbit/4673841 to your computer and use it in GitHub Desktop.
Reverse an array with Ruby
def reverse(a)
half = a.size / 2
(0...half).each do |i|
j = a.size - i - 1
a[i], a[j] = a[j], a[i]
end
a
end
reverse([1, 2])
reverse([1, 2, 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment