Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:07:36+00:00 2026-06-14T09:07:36+00:00

I thought that doing puts #{a} would result in the same output as puts

  • 0

I thought that doing puts #{a} would result in the same output as puts a, but found this not to be the case. Consider:

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>

In the above example it doesn’t matter much, but it may matter when I want to print multiple variables on one line, such as (psudocode):

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

Why is the output different here? Do they actually do different things, or have different semantics?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:07:37+00:00Added an answer on June 14, 2026 at 9:07 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that by default my Regex would exhibit the greedy behavior that I
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I thought that layout is just a widget that keeps more widgets inside. But
I thought that in principle Haskell's type system would forbid calls to impure functions
I thought that I improve performance when I replace this code: def f(a, b):
Edit: I thought I solved it but I didn't. I have javascript that gathers
I wound up converting a table to just divs... But, in doing that, I
UPDATE: So, I've found this , and apparently that's why this old way of
I thought that by convention, only methods with an exclamation mark altered the object.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.