将a和put放在“#{a}”之间的区别

时间:2012-11-09 15:43:17

标签: ruby

我认为执行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):

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

为什么这里的输出不同?他们实际上做了不同的事情,还是有不同的语义?

1 个答案:

答案 0 :(得分:6)

那是因为"#{a}"调用表达式上的#to_s方法。

所以:

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

更新

详细说明,puts最终会调用#to_s,但它会在实际输出前添加逻辑,包括对数组的特殊处理。恰好Array#to_s不使用相同的算法。 (See puts docs here.)这正是它的作用......

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;
}
相关问题