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

Within Ruby on Rails applications database.yml is a plain text file that stores database
my ruby (on rails) class looks like: class Foo def self.method1 someAction end def
I must be missing something very simple, but can't find the answer to this.
Greetings, all, I'm working on an application in Ruby on Rails where we need
So I'm finally ready to allow some friends to use my Ruby on Rails
Has anyone used Ruby/Rails with a Sales Logix database?
Ruby on Rails does not do multithreaded request-responses very well, or at least, ActiveRecord
Ruby on Rails has magic timestamping fields that are automatically updated when a record
Ruby on Rails has a lot of ways to generate JavaScript. Particularly when it
Ruby on Rails controllers will automatically convert parameters to an array if they have

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.