Issue #7091 has been reported by prijutme4ty (Ilya Vorontsov).

----------------------------------------
Feature #7091: Object#puts_to
https://bugs.ruby-lang.org/issues/7091

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee: 
Category: 
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
readlines.sort.map{...}.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( readlines.sort.map{...}.select{...} )
or at least
readlines.sort.map{...}.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.open('file.txt','w'){|f| f.puts( readlines.sort.map{...}.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:

readlines.sort.map{...}.select{...}.puts_to
readlines.sort.map{...}.select{...}.puts_to($stderr)
readlines.sort.map{...}.select{...}.puts_to('')
readlines.sort.map{...}.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)
      io_or_filename.puts(self)
    else
      case io_or_filename
      when String
        File.open(io_or_filename,'w'){|f| f.puts self }
      when Hash
        File.open(io_or_filename[:filename],io_or_filename[:append] ? 'a' : 'w'){|f| f.puts self }
      end
    end
  end
end


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


-- 
http://bugs.ruby-lang.org/