Created
October 10, 2008 15:51
-
-
Save qrush/16081 to your computer and use it in GitHub Desktop.
Rubinius' String#reverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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