Last active
November 24, 2015 23:16
-
-
Save ken1flan/63dd8e0577b5ca075a53 to your computer and use it in GitHub Desktop.
文字列の猫語化
This file contains 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 kittenize | |
result = self | |
DICTIONARY.each do |word| | |
result.gsub!(word[0], word[1]) | |
end | |
end | |
def kittenize! | |
DICTIONARY.each do |word| | |
self.gsub(word[0], word[1]) | |
end | |
end | |
private | |
DICTIONARY = [ | |
[/([^。])(。+)/, '\1にゃ\2'], | |
[/な/, "にゃ"], | |
[/ナ/, "ニャ"], | |
[/ま/, "みゃ"], | |
[/マ/, "ミャ"], | |
[/よう/, "にゃう"], | |
[/ヨウ/, "ニャウ"], | |
] | |
end | |
str = "てすと1。。" | |
puts str | |
result = str.kittenize! | |
puts str | |
puts result | |
str = "てすと2。。" | |
puts str | |
str.kittenize! | |
puts str | |
str = "てすと3。。" | |
puts str | |
str.gsub! /([^。])(。+)/, '\1にゃ\2' | |
puts str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment