我正在构建一个Ruby数组,用于分组选择框,如下所示
def self.actions
actions = []
status_actions = []
priority_actions = []
user_actions = []
for status in Choice.ticket_statuses
status_actions << ["Set ticket status to [#{status.name}]","ticket.status_id = #{status.id}"]
end
for priority in Choice.ticket_priorities
priority_actions << ["Set ticket priority to [#{priority.name}]","ticket.priority_id = #{priority.id}"]
end
for user in User.all
user_actions << ["Set owner to [#{user.name}]","ticket.user_id = #{user.id}"]
end
actions << ["Status", status_actions]
actions << ["Priority", priority_actions]
actions << ["User", user_actions]
return actions
end
这给了我一个如下所示的数组:
[
["Status",
[["Set ticket status to [Closed]", "ticket.status_id = 7"],
["Set ticket status to [Open]", "ticket.status_id = 6"],
["Set ticket status to [Waiting 3rd Party]", "ticket.status_id = 8"],
["Set ticket status to [Waiting on Client]", "ticket.status_id = 9"]]
],
["Priority",
[["Set ticket priority to [High]", "ticket.priority_id = 5"],
["Set ticket priority to [Low]", "ticket.priority_id = 3"],
["Set ticket priority to [Medium]", "ticket.priority_id = 4"]]
],
["User",
[["Set owner to [UNLOCK-DEV]", "ticket.user_id = 1"]]
]
]
现在我需要一种方法来搜索这个数组中的一个值,例如&#34; ticket.status_id = 7&#34;返回名称&#34;将票证状态设置为[已关闭]&#34;。
e.g。
def return_name(value)
TicketAction.actions.collect(&:last).first.select { |action| action[1] == value }.first.first
end
所以我可以像
一样打电话return_name("ticket.status_id = 7")
> "Set ticket status to [Closed]"
我当前的return_name函数(尽管非常混乱)只搜索&#34; status&#34;数组的一部分。
答案 0 :(得分:1)
您可以使用递归:
def return_name(arr, value)
if arr.is_a? Array
return arr.first if value == arr.last
arr.map { |i| return_name(i, value) }.compact.first
end
end
return_name(arr, 'ticket.priority_id = 4')
# => "Set ticket priority to [Medium]"
此代码的作用是:
nil
,除非arr
是Array
。 &#34;递归&#34;意味着为所有项目调用自己 - 它会检查它的任何一个孩子是否可以通过询问他们相同的问题来回答你的问题。
答案 1 :(得分:1)
您可以采取的一种方法是将数组转换为字符串,然后使用正则表达式搜索字符串。
<强>代码强>
def return_name(arr, str)
arr.to_s[/\"(Set ticket [a-z]+ to \[[\w\s]+\])\",\s+\"#{str}\"/,1]
end
<强>实施例强>
arr = [
["Status",
[["Set ticket status to [Closed]", "ticket.status_id = 7"],
["Set ticket status to [Open]", "ticket.status_id = 6"],
["Set ticket status to [Waiting 3rd Party]", "ticket.status_id = 8"],
["Set ticket status to [Waiting on Client]", "ticket.status_id = 9"]
]
],
["Priority",
[["Set ticket priority to [High]", "ticket.priority_id = 5"],
["Set ticket priority to [Low]", "ticket.priority_id = 3"],
["Set ticket priority to [Medium]", "ticket.priority_id = 4"]]
],
["User",
[["Set owner to [UNLOCK-DEV]", "ticket.user_id = 1"]]
]
]
return_name(arr, "ticket.status_id = 7")
#=> "Set ticket status to [Closed]"
return_name(arr, "ticket.status_id = 6")
#=> "Set ticket status to [Open]"
return_name(arr, "ticket.status_id = 8")
#=> "Set ticket status to [Waiting 3rd Party]"
return_name(arr, "ticket.status_id = 9")
#=> "Set ticket status to [Waiting on Client]"
return_name(arr, "ticket.priority_id = 5")
#=> "Set ticket priority to [High]"
return_name(arr, "ticket.priority_id = 3")
#=> "Set ticket priority to [Low]"
return_name(arr, "ticket.priority_id = 4")
#=> "Set ticket priority to [Medium]"
想要他们所有人?
r = /(Set ticket [a-z]+ to \[[\w\s]+\])(?:\",\s+\")(ticket\.[a-z]+_id = \d+)/
arr.to_s.scan(r).map(&:reverse).to_h
#=> {"ticket.status_id = 7" =>"Set ticket status to [Closed]",
# "ticket.status_id = 6" =>"Set ticket status to [Open]",
# "ticket.status_id = 8" =>"Set ticket status to [Waiting 3rd Party]",
# "ticket.status_id = 9" =>"Set ticket status to [Waiting on Client]",
# "ticket.priority_id = 5"=>"Set ticket priority to [High]",
# "ticket.priority_id = 3"=>"Set ticket priority to [Low]",
# "ticket.priority_id = 4"=>"Set ticket priority to [Medium]"}
答案 2 :(得分:0)
以下是一些非递归方式:
def return_name(search_term)
# a flattened array will alywas have the value you are searching for jsut before the search term, so just get the index before that.
temp_array = @data.flatten
ix = temp_array.index(search_term)
ix ? temp_array[ix-1] : nil
end
# some more Ruby magic.
p @data.map(&:last).flatten(1).rassoc("ticket.user_id = 1") #=> ["Set owner to [UNLOCK-DEV]", "ticket.user_id = 1"]