2

puts #{a}を実行すると と同じ出力になると思いputs aましたが、そうではないことがわかりました。検討:

irb(main):001:0> a = [1,2]
=> [1, 2]
irb(main):002:0> puts a
1
2
=> nil
irb(main):003:0> puts "#{a}"
12
=> nil
irb(main):004:0>

上記の例では大した問題ではありませんが、(psudocode) のように複数の変数を 1 行に出力したい場合には問題になるかもしれません:

puts "There are #{a.size} items in the whitelist: #{a}"

ここで出力が異なるのはなぜですか?それらは実際に異なることを行うのでしょうか、それとも異なるセマンティクスを持っているのでしょうか?

4

1 に答える 1

6

That's because "#{a}" calls the #to_s method on the expression.

So:

puts a        # equivalent to, well, puts a
puts "#{a}"   # equivalent to the next line
puts a.to_s

Update:

To elaborate, puts eventually calls #to_s but it adds logic in front of the actual output, including special handling for arrays. It just happens that Array#to_s doesn't use the same algorithm. (See puts docs here.) Here is exactly what it does...

rb_io_puts(int argc, VALUE *argv, VALUE out)
{
    int i;
    VALUE line;

    /* if no argument given, print newline. */
    if (argc == 0) {
        rb_io_write(out, rb_default_rs);
        return Qnil;
    }
    for (i=0; i<argc; i++) {
        if (TYPE(argv[i]) == T_STRING) {
            line = argv[i];
            goto string;
        }
        line = rb_check_array_type(argv[i]);
        if (!NIL_P(line)) {
            rb_exec_recursive(io_puts_ary, line, out);
            continue;
        }
        line = rb_obj_as_string(argv[i]);
      string:
        rb_io_write(out, line);
        if (RSTRING_LEN(line) == 0 ||
            !str_end_with_asciichar(line, '\n')) {
            rb_io_write(out, rb_default_rs);
        }
    }

    return Qnil;
}
于 2012-11-09T15:45:17.670 に答える