Skip to content

Instantly share code, notes, and snippets.

@altamic
Created April 6, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altamic/b8c92a637153cc12d6a76dab79823248 to your computer and use it in GitHub Desktop.
Save altamic/b8c92a637153cc12d6a76dab79823248 to your computer and use it in GitHub Desktop.
wc -l in Ruby
#!/usr/bin/env ruby
# Example implementation of wc -l
(ARGV.length == 0 ?
[["", STDIN]] :
ARGV.lazy.map { |file_name|
[file_name, File.open(file_name)]
})
.map { |file_name, file|
"%8d %s\n" % [*file
.each_line
.lazy
.map { |line| 1 }
.reduce(:+), file_name]
}
.each(&:display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment