从 PHP 切换到 Rails 需要帮助翻译 URL 缩短器
以前的开发人员使用此网站中的源代码来创建URL 缩短器。我的主要任务是将这段代码翻译成 ruby:
function getIDFromShortenedURL1 ($string, $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$length = strlen($base);
$size = strlen($string) - 1;
$string = str_split($string);
$out = strpos($base, array_pop($string));
foreach($string as $i => $char)
{
$out += strpos($base, $char) * pow($length, $size - $i);
}
return $out;
}
我是 ruby 新手,任何帮助将不胜感激:)
Previous developers used source code from this website to create a URL shortener. I am essentially tasked with translating this piece of code into ruby:
function getIDFromShortenedURL1 ($string, $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$length = strlen($base);
$size = strlen($string) - 1;
$string = str_split($string);
$out = strpos($base, array_pop($string));
foreach($string as $i => $char)
{
$out += strpos($base, $char) * pow($length, $size - $i);
}
return $out;
}
I am new to ruby and any help would be much appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这基本上相当于 PHP 代码的直接移植。
代码和非常基本的测试结果(以确保功能相同)可以在 https:// gist.github.com/941152。
Here's what basically amounts to a direct port of the PHP code.
The code an the results of a very basic test (to make sure the functionality is equal) can be found at https://gist.github.com/941152.