ruby / pathname

Pathname represents the name of a file or directory on the filesystem, but not the file itself.
BSD 2-Clause "Simplified" License
26 stars 16 forks source link

Add puts #39

Open bkuhlmann opened 2 months ago

bkuhlmann commented 2 months ago

Why

Hello. :wave:

I keep running into situations where I'd like to use puts when swapping out an IO-like object (i.e. IO, StringIO, File, Kernel, Pathname, and so forth) and can't find any mention as to why Pathname#puts isn't supported as found in similar IO-like objects.

Allowing the use of Pathname#puts would make injecting IO-like dependencies behave similarly in terms of writing with a newline. This would allow anyone to more easily swap these objects out while still quacking like a duck so-to-speak. I bring this up because Pathname#write is supported but am confused as to why Pathname#puts is not.

How

Would it be possible to add Pathname#puts or is there some other reason why this isn't exposed? The workaround is to use Pathname#write by manually adding a new line each time. Example:

path = Pathname "test.txt"
path.write "Hi\n"

This achieves the same functionality but is tedious especially if you are not using a string literal each time. Example:

path = Pathname "test.txt"

# Tedious
path.write "#{some_computed_result}\n"

# Nicer (proposed solution) because you don't have to do the extra work of string concatenation.
path.puts some_computed_result