
["Enter Sandman","One","nothing Else Matters","Master of Puppets","The Unforgiven","The Day That Never Comes","For Whom the Bell Tolls","Fade to Black","Sad But True","Wherever I May Roam","Turn the Page","I disappear","Fuel","CyanIDe","Seek & Destroy","Whiskey In the Jar","All Nightmare Long","Battery","Welcome Home (Sanitarium)","The Unforgiven III","The Unforgiven II","King nothing","RIDe the lightning","No Leaf Clover","Until It Sleeps","...And Justice for All","Blackened","The Memory Remains","Hero of the Day","The Four Horsemen","Orion","CreePing Death","St. Anger","Harvester of Sorrow","Don't Tread on Me","broken,Beat & Scarred","disposable Heroes","fight Fire With Fire","The End of the line","Trapped Under Ice","Of Wolf and Man","Whiplash","My Apocalypse","SuicIDe & Redemption","The Shortest Straw","Tuesday's Gone"]
此数组由此命令生成
artists = search_object.map{|x| x["trackname"]}.uniq.delete_if {|x| x == nil} 这很好,但我需要过滤掉一些元素.用户将键入文本字段,因为他们键入我需要缩小结果范围.因此,例如,如果用户键入字符串“Matters”,我需要取出名称中没有的元素或名称.所以它被描述为“没有其他事情”.如果用户输入字母“a”,那么阵列中没有“a”的所有其他人都会被删除.
他们会参加params [:text]
我做到了这一点并且有效,但也许有一种更清洁的方式
query = params[:term] artists = search_object.map{|x| x["trackname"]}.uniq.delete_if {|x| x == nil} filtered = [] artists.each do |artist| filtered << artist if artist.include?(query) end@H_502_30@解决方法 快速ruby变种是: albums = ["hello kitty","bad day","all is good","day is okay"]def filter_word_in(word,array) array.delete_if { |data| !data.match(word) } return arrayendresult1 = filter_word_in("y",albums)puts result1.inspect # => ["hello kitty","day is okay"]result2 = filter_word_in("ay",result1)puts result2.inspect # => ["bad day","day is okay"]result3 = filter_word_in("day",result2)puts result3.inspect # => ["bad day","day is okay"]result4 = filter_word_in("day i",result3)puts result4.inspect # => ["day is okay"] 你如何在这段代码中看到:我们只是将结果保存在变量中.
那么,我们可以将数据存储在rails中?
您可以使用user_model,也可以将此数据存储在内存中.
创建这样的东西:
class UserSongMemory attr_accessor :memory def initialize @memory = [] end def push(ID,data) @memory << {ID => data} end def pop(ID) @memory.delete_if {|obj| obj.ID == ID} endenduser_memory = UserSongMemory.newuser_memory.add(@user.ID,params[:inputed_string])# after our calculationsuser.pop(@user.ID) 我更喜欢在Class中存储状态内存,但不要忘记清除这个类数据
总结以上是内存溢出为你收集整理的ruby – 如何从数组中删除不需要的东西?全部内容,希望文章能够帮你解决ruby – 如何从数组中删除不需要的东西?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)