From: Robert Klemme Date: 2004-09-17T17:34:46+09:00 Subject: Re: Ex-Perl coders: Howz it feel to convert to Ruby? "Karl von Laudermann" schrieb im Newsbeitrag news:729e3289.0409160820.19e4ca9@posting.google.com... > "Robert Klemme" wrote in message news:<2qsugrF13g5akU1@uni-berlin.de>... > > "James Edward Gray II" schrieb im Newsbeitrag > > news:CBB35FB8-0732-11D9-B224-000A95BA45F8@grayproductions.net... > > > > > I feel Perl has a slight edge in quick and dirty scripting. Simple > > > things like having to call to_s() on the integers I print or > > > > Under which circumstances do you need to_s for numbers? > > 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. >> class Foo; def to_str; "xxx" end end => nil >> Foo.new => # >> Foo.new.to_str => "xxx" >> puts "2" + Foo.new 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