3 ms·
If we want to golf it, it would probably just be lines = IO.readlines ARGV[0] File.write ARGV[1], lines.sort.join('\n') certainly more readable than the C
by ollysb 13y ago
If we want to golf it, it would probably just be
lines = IO.readlines ARGV[0]
File.write ARGV[1], lines.sort.join('\n')
certainly more readable than the C++ even if you're unfamiliar with ruby.
- danmaz74 13y agoWell, if you really want to golf it, you can just write: File.write ARGV[1], IO.readlines(ARGV[0]).sort.join('\n') ;)
- Freaky 13y agoYou're fired. IO.write(ARGV[1], ARGF.lines.sort.join)
- Aaronneyer 13y agoI believe you still need to join on newline, the default is space. However you can save a couple characters by using `$*` instead of ARGV. Also, trim out the space between the arguments and kill the parenthesis for the optimal golfing.
- danmaz74 13y agoWow, this is really sweet and esoteric (didn't know about ARGF) - but, reading the docs for ARGF, wouldn't it also try to ready from ARGV[1]? Maybe this would fix it: IO.write(ARGV.pop, ARGF.lines.sort.join('\n'))
- Aaronneyer 13y agoHere's the shortest I've gotten after looking at some of the comments: IO.write$*.pop,ARGF.lines.sort.join And yes, it does work even without the space between `write` and `$*`. Also after testing, I realized the ('\n') is not required for join. When you call 'lines', it still has the '\n' character in the string, and when you join, it defaults to join without a delimiter, so it's putting them back together with the newline still there.
- trentmb 13y agoIs > File.write ARGV[1], (IO.readlines ARGV[0]).sort.join('\n') not valid Ruby?
- rofrol 13y agoFile.open(ARGV[1], 'w') { |file| file.write(File.read(ARGV[0]).lines.sort.join) } http://stackoverflow.com/questions/8278680/ruby-undefined-method-write-for-ioclass-nomethoderror http://stackoverflow.com/questions/8278680/ruby-undefined-me...