2017-08-25 13 views
-3

単語ごとに、単語の最初の文字を単語の最後に移動しようとしています。例えば、各単語の最初の文字を末尾に追加する

  • "this is an example" ---->"hist si na xamplee"です。

私は例えば、一つの単語でこれを達成、

x = "hello" 
x << x.split("").shift # => "elloh" 
+2

あなたのコードは '' elloh "'ではなく '' helloh''を返します。 – Stefan

+0

「時」は「時計」になりますか? –

答えて

4
'this is an example'.split.map { |word| word.chars.rotate.join }.join(' ') 
#=> "hist si na xamplee" 

参考文献:

6
"this is an example".gsub(/(\S)(\S+)/, '\2\1') 
# => "hist si na xamplee" 
0
"It was the best of times, it was the worst of times". 
    gsub(/[\p{Alpha}']+/) { |s| s[1..-1]<<s[0]} 
    #=> "tI asw het estb fo imest, ti asw het orstw fo imest" 
関連する問題