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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:50:08+00:00 2026-05-23T16:50:08+00:00

I have this code that’s working: case index when Books @reading_img = res.items.first.get_hash(‘MediumImage’)[URL] #

  • 0

I have this code that’s working:

  case index
   when "Books"
     @reading_img = res.items.first.get_hash('MediumImage')["URL"] # don't show an image
     @reading_link = create_amz_url(search, asin)        

     tempy = @nowreading.content.match(/#nowreading.*/).to_s.gsub("#nowreading",'') # strips away the #nowreading tag
     @nowreading.content = tempy.match(/#{search}.*/).to_s.gsub("#{search}", @reading_link) 
     # replaces the book title (passed in as variable 'search') with the URL'ed title

   when "Music"
     @listening_img = res.items.first.get_hash('MediumImage')["URL"] # don't show an image
     @listening_link = create_amz_url(search, asin)        

     tempy = @nowlistening.content.match(/#nowlistening.*/).to_s.gsub("#nowlistening",'') # strips away the #nowreading tag
     @nowlistening.content = tempy.match(/#{search}.*/).to_s.gsub("#{search}", @listening_link) 
     # replaces the song title (passed in as variable 'search') with the URL'ed title       

   end

I need to repeat this for many, many categories. I tried something like this to DRY the code but it didn’t work:

     def get_img_and_links(act, res, search, asin)
        '@'+act+'ing_img' = res.items.first.get_hash('MediumImage')["URL"] # don't show an image
        '@'+act+'ing_link' = create_amz_url(search, asin)        

        tempy = '@now'+act+'ing'.content.match(/#now"#{act}"ing.*/).to_s.gsub("#now#{act}ing",'') # strips away the #nowreading tag
        '@now'+act+'ing'.content = tempy.match(/#{search}.*/).to_s.gsub("#{search}", '@'+act+'ing_link') 
        # replaces the book title (passed in as variable 'search') with the URL'ed title
     end

Essentially, I was trying to create a function that took an “act” (e.g., “read”, “listen”, etc) and have the variables within that function be dynamic. Can this be accomplished? If so, how?

  • 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-23T16:50:09+00:00Added an answer on May 23, 2026 at 4:50 pm

    Look up instance_variable_set here: http://ruby-doc.org/core/classes/Object.html. It’s what you need to dynamically create these variables.

    instance_variable_set "@#{act}ing_img".to_sym, res.items.first.get_hash('MediumImage')["URL"]
    

    And so on…

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

Sidebar

Related Questions

I have this code that determines whether a word (ignoring case) is included in
I have this code that performs an ajax call and loads the results into
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);
I have this code that has one button that let's me choose an entry
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,
So I have this code that takes care of command acknowledgment from remote computers,
Challenge: I have this code that fails to compile. Can you figure out what's

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.