简体   繁体   English

找到一个数组元素并将其设置为Ruby中的最后一个?

[英]Find an array element and set it to last in Ruby?

In my Ruby on Rails app I have an array: 在我的Ruby on Rails应用程序中,我有一个数组:

Car.color.values.map{|x| [x.text, x]}.sort 

This code gives me the following array: 这段代码给了我以下数组:

[["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Inny", "other"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"]]

Now I want to find this element: ["Inny", "other"] and set it to last element of an array. 现在我想找到这个元素: ["Inny", "other"]并将其设置为数组的最后一个元素。 How can I do this in Ruby? 我怎么能用Ruby做到这一点?

a = [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Inny", "other"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"]]
a.push(a.delete(["Inny", "other"]))
# => [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"], ["Inny", "other"]]

For a more generic solution (when you don't know the exact value of the element), you can use partition , and then concatenate the answers back: 对于更通用的解决方案(当您不知道元素的确切值时),您可以使用partition ,然后将答案连接起来:

arr = [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Inny", "other"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"]]
arr.partition { |k, v| v != "other" }.inject(:+)
# => [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"], ["Inny", "other"]] 

Array#rassoc Searches through the array whose elements are also arrays. Array#rassoc搜索其元素也是数组的数组。

arr = [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], .....
arr.push(arr.delete(arr.rassoc("other")))
i = a.index(["Inny", "other"])
a.take(i) + a.drop(i + 1) << ["Inny", "other"]
=> [["Beżowy", "beige"], ["Biały", "white"], ["Brązowy", "brown"], ["Czarny", "black"], ["Czerwony", "red"], ["Fioletowy", "violet"], ["Grafitowy", "graphite"], ["Niebieski", "blue"], ["Perłowy", "pearl"], ["Srebrny", "silver"], ["Szary", "grey"], ["Zielony", "green"], ["Żółty", "yellow"], ["Inny", "other"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM