Reverse a string each two characters with Ruby

吃可爱长大的小学妹 提交于 2019-12-01 23:34:21

I'm not sure if this is the best way to do it:

"0123456789abcdef".scan(/../).reverse.join == "efcdab8967452301"
"0123456789abcdef".gsub(/../, &:reverse).reverse
# => "efcdab8967452301"

You can try:

"0123456789abcdef".chars.each_slice(2).to_a.reverse.join

I like all the solutions, especially @sawa's, but now, six hours later, its a challenge to come up with something different. In the interest of diversity, I offer a way that uses pairwise substitution. It's destructive, so I start by making a copy of the string.

Code

str = "0123456789abcdef"
s = str.dup
(0...s.size/2).step(2) { |i| s[i,2], s[-i-2,2] = s[-i-2,2], s[i,2] }
  s #=> "efcdab8967452301"

Explanation

Letting

enum = (0...s.size/2).step(2) #=> #<Enumerator: 0...8:step(2)>

we can see what is enumerated:

enum.to_a                     #=> [0, 2, 4, 6]

The string

s #=> "0123456789abcdef"

is as follows after each call to the block:

i = 0: "ef23456789abcd01" 
i = 2: "efcd456789ab2301" 
i = 4: "efcdab6789452301" 
i = 6: "efcdab8967452301" 
"0123456789abcdef".split('').each_slice(2).to_a.reverse.flatten.join('')

=> "efcdab8967452301"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!