Karl von Laudermann said:
"Robert Klemme" <
[email protected]> wrote in message
I can't speak for James, but coming from Java I'm used to using the
concatenation operator, like so:
print "foo" + 1;
This causes an error; you have to call to_s on the number. I find it
unintuitive that specifying multible arguments to the print method is
a way of concatenating them, so I rarely think to use it.
Others might think different about this - I do for example.
You
simply enumarate things to get printed. Also, it's more efficient to
individually print items than to first concatenate them (creates new temp
instances) and then print that temp String instance. If I have more
complex printouts I use String interpolation with "#{}" or printf anyway.
I also find
it unintuitive that you don't have to call to_s when passing a
non-string argument by itself, but you do when concatenating.
It's perfectly logical because these are two completely unlelated things:
String#+ is something different than the behavior of Kernel#print.
Perhaps
String#+ should be modified to call to_s on its arguments?
It is done deliberately the way it is: String#+ is "intelligent" enough to
invoke #to_str on its arguments; but an instance that has no #to_str
cannot be "legally" concatenated with a String.
2xxx
=> nil
BTW, one actual benefit of using concatenation vs. multiple arguments
is that you can use puts instead of print to get a "\n" for free at
the end, but not in between each component of your string.
Well, you can use puts with interpolation or define a println method if
you really care to save the "\n" typing effort:
module Kernel
private
def println(*args) print *args << "\n" end
def smart_println(*args)
args << "\n" unless /\n$/ =~ args[-1].to_s
print *args
end
end
Kind regards
robert