Created
April 6, 2016 19:22
-
-
Save altamic/b8c92a637153cc12d6a76dab79823248 to your computer and use it in GitHub Desktop.
wc -l in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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