in reply to puts vs say

That means I'd have to use

say("$var\n");

to make sure say prints what I want it to print. That's pretty dumb because the whole point of say is to remove the need to specify "\n".

Replies are listed 'Best First'.
Re^2: puts vs say
by massa (Hermit) on Mar 01, 2009 at 12:18 UTC
    ikegami, you and CountZero below got the original question wrong. The case is that, in ruby,
    puts "a\n"
    outputs the same as
    puts "a"
    And that is: a followed by a newline.
    The question is: why doesn't say does the same? I happen to think that autochomping-before-printing-and-then-printing-a-newline is an interesting piece of DWIMery (if one really wants an a followed by two newlines, puts "a\n\n" always works. Of course,
    sub puts { local $_ = pop; chomp; push @_, $_; goto &say }
    should also work :-)
    []s, HTH, Massa (κς,πμ,πλ)

      You have not changed my understanding of anything. One of us hasn't made their point clearly enough, and I'm guessing it's me.

      Most of the time, I generate the text I output. I don't want it to be modified by my output function. When using puts, the only way to guarantee the text I generated will be output with a newline added is to add one myself.

      say($var); # Newline added. That's why I use "say". puts($var); # Newline maybe added. Useless for text I generated. puts("$var\n"); # Newline added, but purpose defied.

      Yes, they are uses for puts, but there are for say too. Don't change say, add puts.

      But that's not to say ruby's puts makes sense either. Since the goal of puts is to prevent third parties from messing the output, it makes no sense for puts to chomp just one newline. It should remove all but one, adding one if necessary.