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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:24:36+00:00 2026-06-04T12:24:36+00:00

I am trying to parse URLs. For example where I am trying to pull

  • 0

I am trying to parse URLs. For example where I am trying to pull out:

~/locations/1 => [locations,1]
~/locations/1/comments => [locations,1]
~/locations/1/comments/22 => [locations,1]
~/locations/1/buildings/3 => [buildings,3]
~/locations/1/buildings/3/comments => [buildings,3]
~/locations/1/buildings/3/comments/34 => [buildings,3]

The format is pretty consistent. I started with arrays but it seems to still fail:

@request_path = request.path.downcase.split('/')
@comment_index = @request_path.index("comments").to_i
if @comment_index > 0
  @request_path = @request_path.drop_while { |i| i.to_i >= @comment_index }
end
resource, id = @request_path.last(2)

I added the downcase just incase someone manually typed in an uppercase URL. The drop_while seems to not be working.

  • 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-04T12:24:37+00:00Added an answer on June 4, 2026 at 12:24 pm

    What kind of output you have after processing your code?

    Edited

    Your problem is that you convert element to_i and it is 0. But you want to compare index of element, but can normally get index of element in that situation using Array#index method.

    Correct approach:

    @request_path.drop_while { |i| @request_path.index(i) >= @comment_index }
    

    You can parse path without drop_while.

    My solution:

    def resource_details(path)
        resource_array = path.downcase.split("/").reject!(&:empty?)
        key = resource_array.index("comments")
        return key.present? ? (resource_array - resource_array[key..key + 1]).last(2) : resource_array.last(2)
     end
    

    It will cut out ["comments"] or ["comments","2"] for your path.

    Invoke that method:

    1.9.3p0 :051 > resource_details("/locations/1/buildings/3/comments")
     => ["buildings", "3"] 
    
    1.9.3p0 :052 > resource_details("/locations/1/comments/2")
     => ["locations", "1"] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to parse an SQL string and pull out the parameters. Ex: select *
Trying to parse some XML but apparently this is too much for a lazy
What I'm trying to achieve here is lets say we have two example URLs:
I'm trying to parse mailto URLs into a nice object or dictionary which includes
I'm building a little Twitter thing in PHP and I'm trying to parse URLs,
I am trying parse out some text that is in an html file. The
Trying to parse this json and cannot seem to figure it out. { description
I'm trying parse the follow XML file: <root>Root <pai>Pai_1 <filho>Pai1,Filho1</filho> <filho>Pai1,Filho2</filho> </pai> <pai>Pai_2 <filho>Pai2,Filho1</filho>
Trying to parse an HTML document and extract some elements (any links to text
Trying to parse Maplines for an airport. Each airport can have X number of

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.