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 8255371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:30:09+00:00 2026-06-08T01:30:09+00:00

I would like to extract some information from a string in Ruby by only

  • 0

I would like to extract some information from a string in Ruby by only reading the String once (O(n) time complexity).

Here is an example:

The string looks like this: -location here -time 7:30pm -activity biking

I have a Ruby object I want to populate with this info. All the keywords are known, and they are all optional.

def ActivityInfo
  _attr_reader_ :location, :time, :activity

  def initialize(str)
    @location, @time, @activity = DEFAULT_LOCATION, DEFAULT_TIME, DEFAULT_ACTIVITY

    # Here is how I was planning on implementing this
    current_string = ""
    next_parameter = nil # A reference to keep track of which parameter the current string is refering to
    words = str.split
    while !str.empty?
      word = str.shift
      case word
      when "-location"
        if !next_parameter.nil?
          next_parameter.parameter = current_string # Set the parameter value to the current_string
          current_string = ""
        else
        next_parameter = @location
      when "-time"
        if !next_parameter.nil?
          next_parameter.parameter = current_string
          current_string = ""
        else
        next_parameter = @time
      when "-activity"
        if !next_parameter.nil?
          next_parameter.parameter = current_string
          current_string = ""
        else
        next_parameter = @time
      else
        if !current_string.empty?
          current_string += " "
        end
        current_string += word
      end
    end
  end   
end

So basically I just don’t know how to make a variable be the reference of another variable or method, so that I can then set it to a specific value. Or maybe there is just another more efficient way to achieve this?

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-06-08T01:30:10+00:00Added an answer on June 8, 2026 at 1:30 am

    The string looks suspiciously like a command-line, and there are some good Ruby modules to parse those, such as optparse.

    Assuming it’s not, here’s a quick way to parse the commands in your sample into a hash:

    cmd = '-location here -time 7:30pm -activity biking'
    Hash[*cmd.scan(/-(\w+) (\S+)/).flatten]
    

    Which results in:

    {
        "location" => "here",
            "time" => "7:30pm",
        "activity" => "biking"
    }
    

    Expanding it a bit farther:

    class ActivityInfo
      def initialize(h)
        @location = h['location']
        @time     = h['time'    ]
        @activity = h['activity']
      end
    end
    act = ActivityInfo.new(Hash[*cmd.scan(/-(\w+) (\S+)/).flatten])
    

    Which sets act to an instance of ActivityInfo looking like:

    #<ActivityInfo:0x101142df8
        @activity = "biking",
        @location = "here",
        @time = "7:30pm"
    >
    

    —

    The OP asked how to deal with situations where the commands are not flagged with - or are multiple words. These are equivalent, but I prefer the first stylistically:

    irb(main):003:0> cmd.scan(/-((?:location|time|activity)) \s+ (\S+)/x)
    [
        [0] [
            [0] "location",
            [1] "here"
        ],
        [1] [
            [0] "time",
            [1] "7:30pm"
        ],
        [2] [
            [0] "activity",
            [1] "biking"
        ]
    ]
    
    irb(main):004:0> cmd.scan(/-(location|time|activity) \s+ (\S+)/x)
    [
        [0] [
            [0] "location",
            [1] "here"
        ],
        [1] [
            [0] "time",
            [1] "7:30pm"
        ],
        [2] [
            [0] "activity",
            [1] "biking"
        ]
    ]
    

    If the commands are multiple words, such as “at location”:

    irb(main):009:0> cmd = '-at location here -time 7:30pm -activity biking'
    "-at location here -time 7:30pm -activity biking"
    irb(main):010:0> 
    irb(main):011:0* cmd.scan(/-((?:at \s location|time|activity)) \s+ (\S+)/x)
    [
        [0] [
            [0] "at location",
            [1] "here"
        ],
        [1] [
            [0] "time",
            [1] "7:30pm"
        ],
        [2] [
            [0] "activity",
            [1] "biking"
        ]
    ]
    

    If you need even more flexibility look at Ruby’s strscan module. You can use that to tear apart a string and find the commands and their parameters.

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

Sidebar

Related Questions

I would like to extract only tar.gz from the following strings. /root/abc/xzy/file_tar_src-5.2.8.23.tar.gz or /root/abc/xyz/file_tar_src-5.2.tar.gz
I would like to extract some text between two points in a string, in
I would like to submit some information collected from user during Inno setup installation
Hey Im trying to extract certain information from a string. The String looks like
I would like to extract some lines from a text file, I have started
I would like to extract and construct some strings after identifying a substring that
I would like to extract items from this sample html, more specificly, i would
I would like to extract the GPS EXIF tag from pictures using php. I'm
I would like to extract a value or its inverse from database. I would
Hello! I would like to extract all citations from a text. Additionally, the name

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.