Skip to content

Instantly share code, notes, and snippets.

@qrush
Created October 10, 2008 15:51
Show Gist options
  • Save qrush/16081 to your computer and use it in GitHub Desktop.
Save qrush/16081 to your computer and use it in GitHub Desktop.
Rubinius' String#reverse
# Returns a new string with the characters from <i>self</i> in reverse order.
#
# "stressed".reverse #=> "desserts"
def reverse
self.dup.reverse!
end
# Reverses <i>self</i> in place.
def reverse!
return self if @bytes <= 1
self.modify!
i = 0
j = @bytes - 1
while i < j
@data[i], @data[j] = @data[j], @data[i]
i += 1
j -= 1
end
self
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment