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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:17:37+00:00 2026-06-11T17:17:37+00:00

Possible Duplicate: In Ruby, can you perform string interpolation on data read from a

  • 0

Possible Duplicate:
In Ruby, can you perform string interpolation on data read from a file?

I need to loop through a few ‘li’ elements:

for i in 1..5
  Xpaths.getPath("location","elements")      
end        

Then, all xpaths are in an external file, so the value of such ‘elements’ variable is as follows:

{ 
"location":
  {
  "elements"      :"//ul[@id='locations']/li[#{i}]/a"                                                           
  }
}              

The elements variable is read as a string, and [#{i}] is not replaced with values such as [1]. Is there a way to define a specific part of a string in an external file as a variable?

  • 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-11T17:17:38+00:00Added an answer on June 11, 2026 at 5:17 pm

    If I understand your question correctly, you have some external data file in JSON format, whose structure has certain fields as XPath strings on which you would like to perform Ruby string interpolation.

    The short answer is that yes, you can do this directly using, say Kernel#eval. There are also other options such as using erb. A solution using the simple “eval” route might look like this:

    xpaths = JSON.load(File.read('my-xpaths.json'))
    (1..5).each do |i|
      xpath = eval(xpaths['location']['elements'].to_json)
      # => "//ul[@id='locations']/li[1]/a"
    end
    

    Of course, using “eval” is fraught with peril since you are essentially executing code from another source, so there are many precautions you must take to ensure safety. A better approach might involve doing a simple regular expression replacement so that you can constrain the interpolation on the XPath item:

    xpaths['location']['elements'] # => "//ul[@id='locations']/li[__INDEX__]/a"
    xpath = xpaths['location']['elements'].gsub(/__INDEX__/, i.to_s)
    # => "//ul[@id='locations']/li[1]/a"
    

    See also this related question: In Ruby, can you perform string interpolation on data read from a file?

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

Sidebar

Related Questions

Possible Duplicate: Can't convert String into integer in ruby/ruby-on-rails I'm running through a tutorial
Possible Duplicate: Ruby code to extract host from URL string I found this module
Possible Duplicate: How to remove rvm (ruby version manager) from my system? How can
Possible Duplicate: Calling a Function From a String With the Function's Name in Ruby
Possible Duplicate: Hex to binary in ruby In Python, I can do the following:
Possible Duplicate: Running a command from Ruby displaying and capturing the output I have
Possible Duplicate: Array slicing in Ruby: looking for explanation for illogical behaviour (taken from
Possible Duplicate: Exporting an Environment Variable in Ruby I need to set several environment
Possible Duplicate: Ruby on Rails scalability/performance? I'm aware from the twitter shortages that Ruby
Possible Duplicate: Ruby: Sorting 2 arrays using values from one of them I 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.