我只是在学习 ruby,想将下面的函数从 php 转换为 ruby
function stringToDigits($str)
{ $str = strtolower($str);
$from = 'abcdefghijklmnopqrstuvwxyz';
$to = '22233344455566677778889999';
return preg_replace('/[^0-9]/', '', strtr($str, $from, $to));
}
对于之前没有正确解释,我深表歉意。这是我的第一个问题......
我在将其转换为红宝石时遇到问题。这就是我所做的
class String
def str_to_digits str
str = str.downcase
from = 'abcdefghijklmnopqrstuvwxyz'
to = '22233344455566677778889999'
self.gsub('/[^0-9]', '')
# here I am having problem to translate the whole string 'str' from 'from' to 'to'
# I dont know how to write it in ruby
end
end