Created
February 15, 2016 12:30
-
-
Save amashigeseiji/abc81b8425292eab75ed to your computer and use it in GitHub Desktop.
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
class String | |
def cabmridge | |
tmp = [] | |
split(' ').each do |word| | |
tmp2 = [] | |
chars = word.chars | |
first = chars.shift | |
last = chars.pop | |
tmp.push tmp2.push(first).concat(chars.shuffle).push(last).join('') | |
end | |
tmp.join(' ') | |
end | |
end | |
p 'この ぶんしょう は いぎりす の ケンブリッジ だいがく の けんきゅう の けっか にんげんは もじ を にんしき する とき その さいしょ と さいご の もじさえ あっていれば じゅんばん は めちゃくちゃ でも ちゃんと よめる という けんきゅう に もとづいて わざと もじの じゅんばん を いれかえて あります'.cabmridge |
コメントいまさら気づきました!
一時変数やだなとは思ってましたが、なるほどです。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
どっちがいいかという話ではないのですが、Ruby だとあんまり一時格納用の変数を使わない書き方が好まれるので、
inject
とメソッドチェーンを使ったこういうのが、同じ手法でも「より Ruby っぽい」コードになると思います!