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
我需要为很多很多类别重复这一点.我尝试过这样的东西来干掉代码,但它不起作用:
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
本质上,我试图创建一个采取“行为”(例如,“阅读”,“收听”等)的函数,并使该函数内的变量是动态的.这可以实现吗?如果是这样,怎么样?
解决方法 在这里查找instance_variable_set: http://ruby-doc.org/core/classes/Object.html.这是动态创建这些变量所需的.instance_variable_set "@#{act}ing_img".to_sym,res.items.first.get_hash('MediumImage')["URL"]
等等…
总结以上是内存溢出为你收集整理的ruby-on-rails – 如何在rails函数中创建动态变量?全部内容,希望文章能够帮你解决ruby-on-rails – 如何在rails函数中创建动态变量?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)