Feature #7091


Added by prijutme4ty (Ilya Vorontsov) over 7 years ago. Updated over 7 years ago.

Target version:


I suggest a new method Object#puts_to(io_or_filename) (or BasicObject#puts_to)

It's usual that one-two-three-line scripts have big chains like{...}.select{...} and so on and after you wrote such a monstruous expression to process input, you understand that you should output it. If script's written offhand, you wouldn't create a new variable just to use it at next line, so you write smth like

puts({...}.select{...} )
or at least{...}.select{...}.tap{|x| puts x}

It looks ugly and isn't readable. Thing get even worse when you are writing object info a file:'file.txt','w'){|f| f.puts({...}.select{...} ) }

I write such constructions many times a day, just because my scripts are usually used once or twice and I can't waste my time to make this more clear.
Instead of such a pasta-code, one can make smth like this:{...}.select{...}.puts_to{...}.select{...}.puts_to($stderr){...}.select{...}.puts_to(''){...}.select{...}.puts_to(filename:'', append:true)

Implementation can be smth like this:

class Object
def puts_to(io_or_filename = $stdout)
if io_or_filename.respond_to?(:puts)
case io_or_filename
when String,'w'){|f| f.puts self }
when Hash[:filename],io_or_filename[:append] ? 'a' : 'w'){|f| f.puts self }

Or may be Hash-syntax for append-mode should be written simply as two arguments:
obj.puts_to('file.txt', true)


Updated by drbrain (Eric Hodel) over 7 years ago

  • Status changed from Open to Rejected

There are several ways to do this just as easily:

Write to $stdout:


Write to $stderr:


Write to 'file.txt':

File.write 'file.txt',{...}.select{...}.join

Append to 'file.txt':

File.write 'file.txt',{...}.select{...}.join, 'a'

Updated by nobu (Nobuyoshi Nakada) over 7 years ago


Updated by prijutme4ty (Ilya Vorontsov) over 7 years ago

Don't you feel that mixing function call and method-chain is ugly? This was the main reason why I proposed this method, not inability to do this.
In case that expression is full of curl-braces and so on, additional level of "brace-deepness" is much worse that additional method in chain. Compare a real-life example (discreting a matrix):

File.write('matrix.out', File.readlines('').map{|line|"\t")})


Both expressions are rather complex, but in the first form main operations are hidden inside braces of write or puts function. Also here input and output appear in adjacent expressions. In the second form main part of calculations is not shaded by output code. Also one can trace path of data from input file through calculations to output file.

Updated by prijutme4ty (Ilya Vorontsov) over 7 years ago

nobu (Nobuyoshi Nakada) wrote:


Thank you, Nobu. Hadn't seen this before. I wish to have similar syntax for output in file by filename, but this is also good enough.

Also available in: Atom PDF