php - 等同于 PHP 的 .= 运算符的 ruby​​?

标签 php ruby

我想在一个循环中连接一个字符串,就像这样,但是在 Ruby 中:

$fizz = file_get_contents($_SERVER['argv'][1]);
$output = "";
for($i=1;$i<=$fizz;$i++) {
    if($i % 3 == 0 && !($i % 5 ==0)) {
        $output .= "fizz\n";
    }
    else if($i % 5 == 0 && !($i % 3 ==0)) {
        $output .= "buzz\n";
    }
    else if($i % 3 == 0 && $i % 5 == 0) {
        $output .= "FizzBuzz\n";
    }
}
print $output;
?>

我也有兴趣找出“.=”类型运算符的名称,以便我可以通过 Google 更好地搜索这些东西。

使用 Ruby 1.8.6。

最佳答案

您正在寻找 Ruby 的 String#<< .

因此,您的代码可能如下所示:

variable << "fizz\n"

这是 documentation对于 <<(又名 concat)方法。

关于php - 等同于 PHP 的 .= 运算符的 ruby​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6191986/

相关文章:

PHP - 创建一个文件夹并将图像保存到该文件夹​​?然后显示那张照片?

php - 制作类似程序的测验

ruby SSH : interactive command "su": How further after successfull logged in?

ruby - 从 Chef CookBook 检查 Python 版本并根据结果安装包

PHP从没有扩展名的url中查找图像的扩展名

php - notepad++ 中多行 php echo 语句的语法高亮显示

ruby - 二维数组,推到一个数组显示在所有数组中?

ruby - 正则表达式如何从括号中提取单词

ruby-on-rails - rails 5 : Add record to join table without model (has_and_belongs_to_many)

php - MySQL查询以特定顺序返回结果而不按顺序返回