News for hackers. Stuff that matters.
Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @08:07AM (#6559111)
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
Re:C++ has become a scripting language by ollysb on Wednesday October 16, 2013 @08:28AM (#6559221)
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.
Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @08:33AM (#6559250)
Well, if you really want to golf it, you can just write:

  File.write ARGV[1], IO.readlines(ARGV[0]).sort.join('\n')
;)
Re:C++ has become a scripting language by Freaky on Wednesday October 16, 2013 @08:37AM (#6559278)
You're fired.

    IO.write(ARGV[1], ARGF.lines.sort.join)
Re:C++ has become a scripting language by Aaronneyer on Wednesday October 16, 2013 @08:53AM (#6559349)
Here'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.

Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @08:48AM (#6559333)
Wow, 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'))
Re:C++ has become a scripting language by Freaky on Wednesday October 16, 2013 @09:54AM (#6559620)
Yes, good catch. join('\n') is wrong though - IO#lines preserves newlines, the default join is the empty string, and you meant "\n" :)
Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @11:14AM (#6560089)
:)
Re:C++ has become a scripting language by Aaronneyer on Wednesday October 16, 2013 @08:48AM (#6559332)
I 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.

Re:C++ has become a scripting language by trentmb on Wednesday October 16, 2013 @08:38AM (#6559284)
Is

> File.write ARGV[1], (IO.readlines ARGV[0]).sort.join('\n')

not valid Ruby?

Re:C++ has become a scripting language by rofrol on Wednesday October 16, 2013 @12:14PM (#6560461)

  File.open(ARGV[1], 'w') { |file| file.write(File.read(ARGV[0]).lines.sort.join) }
http://stackoverflow.com/questions/8278680/ruby-undefined-me...
Re:C++ has become a scripting language by hamidr on Wednesday October 16, 2013 @08:25AM (#6559210)
The 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 :)
Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @08:31AM (#6559233)
I'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.

Re:C++ has become a scripting language by stinos on Wednesday October 16, 2013 @08:11AM (#6559127)
Now that's the succinctness I'd expect from an actual scripting language
Re:C++ has become a scripting language by adestefan on Wednesday October 16, 2013 @08:17AM (#6559165)
Except that's not what the C++ code does. The C++ reads and writes from files given as command line arguments.
Re:C++ has become a scripting language by danmaz74 on Wednesday October 16, 2013 @08:23AM (#6559197)
Fixed
Re:C++ has become a scripting language by adestefan on Wednesday October 16, 2013 @08:35AM (#6559265)
Thanks. I know enough ruby to be dangerous so didn't know if more needed to be done than that simple substitution.
Re:C++ has become a scripting language by vjoel on Wednesday October 16, 2013 @04:17PM (#6561894)
Or:

   ruby -e 'puts ARGF.sort' file.txt
Also, this reads stdin if the argument is omitted.