by danmaz74 · 13y ago · 0 comments
array = IO.readlines ARGV[0]
array.sort!
File.write ARGV[1], array.join("\n")
edit: get input and output file names from command line 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. File.write ARGV[1], IO.readlines(ARGV[0]).sort.join('\n')
;) IO.write(ARGV[1], ARGF.lines.sort.join) 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.
Maybe this would fix it:
IO.write(ARGV.pop, ARGF.lines.sort.join('\n'))Also, trim out the space between the arguments and kill the parenthesis for the optimal golfing.
> File.write ARGV[1], (IO.readlines ARGV[0]).sort.join('\n')
not valid Ruby?
File.open(ARGV[1], 'w') { |file| file.write(File.read(ARGV[0]).lines.sort.join) }
http://stackoverflow.com/questions/8278680/ruby-undefined-me...BTW, I would never use Ruby for anything that needs performance, but for ease of use and readability it's really great.
ruby -e 'puts ARGF.sort' file.txt
Also, this reads stdin if the argument is omitted.