大家如何实现 String#reverse ?
我想到了三个方法,虽然第一个其实用到了迭代器中的 reverse_each ....
str = "hello"
p str.chars.reverse_each.to_a.join
str = "hello"
y = []
str.each_char.with_index(1) {|e, i| y << x[-i] }
p y.join
str = "hello"
i = 0; j = str.length - 1
while i < j
str[i], str[j] = str[j], str[i]
i += 1
j -= 1
end
p str