4 ms·
Ruby: array = IO.readlines ARGV[0] array.sort! File.write ARGV[1], array.join("\n") edit: get input and output file names from command line
by danmaz74 13y ago
Ruby:
array = IO.readlines ARGV[0]
array.sort!
File.write ARGV[1], array.join("\n")
edit: get input and output file names from command line
- stinos 13y agoNow that's the succinctness I'd expect from an actual scripting language
- deleted 13y ago[deleted]
- deleted 13y ago[deleted]
- hamidr 13y agoThe thing is in ruby you are already in main method so there's no need to declare main function as an entry point. The main reason that the c++ version the code is longer has some historical/performance related issues! In c++11 it could've been with smaller standard library at design but that could break the old codes! Although obviously, "Little code =! Better code". What you want to achieve is actually more important. Btw, C++ as a scripting language? at first you might think that way but truly that's a big lie :)
- danmaz74 13y agoI'm not arguing with that ;) BTW, I would never use Ruby for anything that needs performance, but for ease of use and readability it's really great.
- ollysb 13y agoIf 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'))
- deleted 13y ago[deleted]
- vjoel 13y agoOr: ruby -e 'puts ARGF.sort' file.txt Also, this reads stdin if the argument is omitted.