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

  • Home
  • SEARCH
  • 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 4025278
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:49:37+00:00 2026-05-20T10:49:37+00:00

in ruby/rails3, I need to do some heavy text parsing to find a certain

  • 0

in ruby/rails3, I need to do some heavy text parsing to find a certain string. Right now I’m doing something like the following:

extract_type1 = body.scan(/(stuff)/m).size
extract_type2 = body.scan(/(stuff)/m).size
extract_type3 = body.scan(/(stuff)/m).size
extract_type4 = body.scan(/(stuff)/m).size
extract_type5 = body.scan(/(stuff)/m).size

if extract_type1 > 0
elsif  extract_type2 > 0
elsif  extract_type3 > 0
elsif  extract_type4 > 0
elsif  extract_type5 > 0

The problem here is that I keep needing to add extract types based on the app. And that results in a lot of processing when the case occurs that extract_type1 >0 and the rest aren’t needed.

But at the same time, it’s nice and clean to have the extract logic separated from the if block as that would be busy messy and hard to read.

Any thoughts on how to optimize this while not compromising readability?

Thanks

  • 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-05-20T10:49:37+00:00Added an answer on May 20, 2026 at 10:49 am

    what about storing all your “keywords” you are searching for in an array and iterate over it like:

    stuff   = ["stuff1", "stuff2"]
    
    stuff.each do |c_stuff|
      if body.scan(/(#{Regexp.escape(c_stuff)})/m).size > 0
        # do something
        # break the loop
        break
      end
    end    
    

    Edit: If you need the index of the element, you can use each_with_index do |c_stuff, c_index|

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some Ruby code, not Rails, and I need to handle something
Something like phpmyadmin but for Ruby on Rails ? I need an app to
I need some jQuery help on a search form I've created. Right now, in
I'm a 80% ruby on rails developer, but still need to do some Access
I need some advice on configuring mail in production Ruby-on-Rails sites. I deploy my
I'm developing a Ruby on Rails application need some advanced search engine functionality: specifically,
need some advice. I'm doing a project on RoR, and do not sure what
I need to generate some charts and graphs in a Ruby on Rails 3
I am using Ruby on Rails and I need to store a search result
I need to implement fine-grained access control in a Ruby on Rails app. The

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.