为了好玩我在ruby创建一个简单的leet(1337)生成器
所以我正在做这样的事情,虽然有效,但看起来效率不高,我相信它只能用一行完成......
def leet
words = words.gsub(/a/, '4')
words = words.gsub(/e/, '3')
words = words.gsub(/i/, '1')
words = words.gsub(/o/, '0')
words = words.gsub(/s/, '5')
words = words.gsub(/t/, '7')
puts words
end
你能在这里给我一个帮助吗? :)谢谢!
答案 0 :(得分:7)
def leet(word)
puts word.gsub(/[aeiost]/,'a'=>'4','e'=>'3','i'=>'1','o'=>'0','s'=>'5','t'=>'7')
end
答案 1 :(得分:5)
def leet s
s.tr 'aeiost', '431057'
end
答案 2 :(得分:4)
更为通用的megas版本:
class Leet
@@map = {
'a' => '4',
'e' => '3',
'i' => '1',
'o' => '0',
's' => '5',
't' => '7'
}
@@re = Regexp.union(@@map.keys)
def self.speak(str)
str.gsub(@@re, @@map)
end
end
puts Leet.speak('leet')
# l337
根据需要调整@@map
,然后离开。