Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:19:48+00:00 2026-06-16T17:19:48+00:00

I am new to rails , currently i am working on code statistics for

  • 0

I am new to rails , currently i am working on code statistics for php,javascript/coffeescript repositories using ruby where i have a requirement to get no of coded lines and comment lines(single line and multi line comments) of php and javascript and coffeescript files using regular expressions . can any one help on this , thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:19:49+00:00Added an answer on June 16, 2026 at 5:19 pm

    Here it is my man:

    f = File.open("test.php")
    
    loc = 0
    comments = 0
    while line = f.gets
      if commented ||= line.match(/^\/\*/)
        commented = nil if line.match(/\*\/$/)
        comments += 1
      elsif line.match(/^\s*\/\//)
        comments += 1
      else
        loc +=1 unless line.match(/^\s*\n/)
      end
    end
    
    puts loc
    puts comments
    

    This works for:

    //line comments
    /* inline block comments */
    /* multi
    line
    comments */
    

    How it works:

    • You open the file
    • Initialize the counters in 0
    • Go trough each line with gets and assign it to the line variable
    • If the line matches the beggining of a block comment set the commented variable
    • Add 1 to comments variable
    • If commented is set, search for the end of comment and set commented to nil if found
    • Else if comment block not opened and “//” is found add 1 to comments
    • Else add 1 to loc unless it’s an “empty” line
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to Ruby on Rails. As of now have this working code
hi im new in ruby on rails development and currently im working in a
I'm new to Ruby & Rails, and am currently working on the Rails Tutorial
I'm new to Ruby on Rails (formerly and currently PHP expert) so forgive my
I am new to Rails routing and I currently have a problem and hope
I'm currently evaluating options for adding sub-domain support to a new Ruby on Rails
I have built a blog application using Ruby on Rails. In the application I
I am new to Ruby on Rails using authlogic for user authentication and its
So I have been working on a multi-step form in ruby on rails (1.9.3)
So I'm trying to get some basic jQuery code with Rails working, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.