如何将这一行从PHP转换为Ruby?
问题描述:
I have trouble with converting the following line from PHP to Ruby:
strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), trim($str)));
Is it possible to accomplish this in one line just like in here?
将以下行从PHP转换为Ruby时遇到问题: p>
strtolower(preg_replace(array('/ [^ a-zA-Z0-9 - ] /','/ [ - ] + /','/ ^ - | - $ /'),array('', ' - ',''),trim($ str)));
code> pre>
是否可以像在这里一样在一行中完成此操作? p >
div>
答
str.strip.gsub(/[^a-zA-Z0-9 -]/, '').gsub(/[ -]+/, '-').gsub(/^-|-$/, '').downcase
could be further simplified to:
str.strip.gsub(/[ -]+/, '-').gsub(/[^a-z \d-]|^-|-$/i, '').downcase