Recursive line counter (review?)

Discussion in 'Ruby' started by Thibaut Barrère, Mar 13, 2006.

  1. Hi

    I'm practicing ruby everyday on small tools to learn more, could anyone
    review what I've coded today ? This is a basic line of code counter
    (pure ruby), any feedback welcome!

    thanks !

    Thibaut

    def loc_file(file)
    File.open(file).inject(0) { |loc,line| loc+1 }
    end

    def loc_by_extension(root,ext)
    Dir[root+ext].inject(0) { |total,f| total + loc_file(f) }
    end

    ["*.cs","*.cpp","*.h","*.resx"].each do |ext|
    puts "#{loc_by_extension('c:/evolutionsyncfusion/**/',ext)} loc for
    #{ext}"
    end
     
    Thibaut Barrère, Mar 13, 2006
    #1
    1. Advertising

  2. Thibaut Barrère

    ChrisH Guest

    I like your use of inject. However, the code you show is not
    recursive, but thats not really important unless it being recursive was
    the point...

    When I was playing with a similar bit of code I recall using
    <code>
    IO.readlines('filename').size
    </code>

    I also recall trying
    <code>
    f=File.open('filename')
    f.seek(0,IO::SEEKEND)
    lc = f.lineno
    </code>

    If speed is an issue oyu might want to compare various methods.

    Happy coding

    Cheers
     
    ChrisH, Mar 13, 2006
    #2
    1. Advertising

  3. Thibaut Barrère wrote:
    > Hi
    >
    > I'm practicing ruby everyday on small tools to learn more, could
    > anyone review what I've coded today ? This is a basic line of code
    > counter (pure ruby), any feedback welcome!
    >
    > thanks !
    >
    > Thibaut
    >
    > def loc_file(file)
    > File.open(file).inject(0) { |loc,line| loc+1 }
    > end


    You're not properly closing IO's. Rather do

    def loc_file(file)
    File.open(file) {|io| io.inject(0) { |loc,line| loc+1 } }
    end

    Kind regards

    robert
     
    Robert Klemme, Mar 13, 2006
    #3
  4. ChrisH wrote:
    > I like your use of inject. However, the code you show is not
    > recursive, but thats not really important unless it being recursive
    > was the point...
    >
    > When I was playing with a similar bit of code I recall using
    > <code>
    > IO.readlines('filename').size
    > </code>


    This doesn't work well with large files and also uses more resources
    (memory).

    > I also recall trying
    > <code>
    > f=File.open('filename')
    > f.seek(0,IO::SEEKEND)
    > lc = f.lineno
    > </code>


    Does this really work? I can't believe it because during seeking the file
    is actually not read - so the IO instance cannot really count line
    numbers, can it?

    > If speed is an issue oyu might want to compare various methods.


    Yeah.

    Kind regards

    robert
     
    Robert Klemme, Mar 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    1
    Views:
    465
    Cowboy \(Gregory A. Beamer\)
    Jan 15, 2004
  2. The Eeediot
    Replies:
    3
    Views:
    2,304
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  3. www
    Replies:
    51
    Views:
    1,516
  4. George2
    Replies:
    1
    Views:
    850
    Alf P. Steinbach
    Jan 31, 2008
  5. Wiktor
    Replies:
    5
    Views:
    95
    Wiktor
    Jan 4, 2014
Loading...

Share This Page