Ruby file count lines


















For reasons I don't fully understand, scanning the file for newlines using File seems to be a lot faster than doing CSV readlines. The wc -l code has one problem. If the file is a CSV file, the length of the records should be pretty uniform if the content of the file is numeric. Wouldn't it make sense to just divide the size of the file by the length of the record or a mean of the first records.

That's it. So, yeah, this looks like C. So what? C's awesome and Ruby is awesome because when a C answer is easiest that's what you can expect your Ruby code to look like. Hopefully your dain hasn't already been bramaged by Java. By the way, please don't even consider any of the answers above that use the IO read or IO readlines method in turn calling a String method on what's been read.

You said you didn't want to read the whole file into memory and that's exactly what these do. This is why Donald Knuth recommends people understand how to program closer to the hardware because if they don't they'll end up writing "weird code".

Obviously you don't want to code close to the hardware whenever you don't have to, but that should be common sense. However you should learn to recognize the instances which you do have to get closer to the nuts and bolts such as this one. And don't try to get more "object oriented" than the situation calls for. That's an embarrassing trap for newbies who want to look more sophisticated than they really are.

You should always be glad for the times when the answer really is simple, and not be disappointed when there's no complexity to give you the opportunity to write "impressive" code. However if you want to look somewhat "object oriented" and don't mind reading an entire line into memory at a time i.

This would be a good compromise but only if the lines aren't too long in which case it might even run more quickly than my first solution. Both are very much faster more than x in my experience than using getc.

Using foreach without inject can also be slightly simplified relative to the snippet given elsewhere in this thread as follows:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Count the number of lines in a file without reading entire file into memory? Ask Question. Asked 11 years, 9 months ago. Active 1 year, 10 months ago. Viewed 50k times. I'm processing huge data files millions of lines each.

Add a comment. Active Oldest Votes. The ". Now it won't run on Windows.. I would take a small performance hit within the same complexity class to avoid portability issues.

If you are in a Unix environment, you can just let wc -l do the work. Jason WC is so fast that one probably won't need a progress counter. There's an edge condition: if the last line of the file doesn't have a newline, wc comes up one short. This is by posix design, see backreference. I know, "Google harder! Show 1 more comment. I'm assuming the original poster was ok with reading through the file, just not having the entire contents of it in memory.

JBoy JBoy 5, 12 12 gold badges 54 54 silver badges 92 92 bronze badges. Your system's wc has issues. I am quite sure you cannot do this with a huge file because its content will be entirely held in memory. Viewed 2k times. It seems checking for correct command-line parameter combinations takes up a lot of space Improve this question. Sebastian N. Add a comment. Active Oldest Votes. Ruby is an OOP language, but it's not necessary to create a bunch of classes for simple scripts like this.

Improve this answer. And thanks for the example. I know there was a "Ruby way" of doing it Great answer. One small suggestion for an improvement: Put all the argument checking and fail statements at the top. Then the program reads: 1. It has the added benefit of getting rid of all the "if Wayne Conrad Wayne Conrad 3, 1 1 gold badge 14 14 silver badges 37 37 bronze badges.

Interesting approach with your Arguments class Have you considered using a special library for command-line argument validation? I have often looked for libraries which do good handling of non-switch arguments; I am not aware of one that just parses arguments. The ones I've seen have strong opinions on parts of your program that are not argument parsing. Cary Swoveland Cary Swoveland 1, 9 9 silver badges 10 10 bronze badges.

But please don't change your example! I like it that it's so short. Unlike Python, you can de-struct even if sizes do not match. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown.



0コメント

  • 1000 / 1000