我有以下数组:
[#<PatchedOpenStruct name="Kristen Stewart", id="162655167", characters=["Snow White"]>, #<PatchedOpenStruct name="Chris Hemsworth", id="770829335", ch
aracters=["The Huntsman"]>, #<PatchedOpenStruct name="Charlize Theron", id="162654733", characters=["The Queen"]>, #<PatchedOpenStruct name="Viggo Mort
ensen", id="162654541">, #<PatchedOpenStruct name="Sam Claflin", id="771073196", characters=["Prince"]>]
我正在尝试过滤所有'name'字段。有什么帮助吗?
答案 0 :(得分:2)
答案 1 :(得分:2)
如果这些是标准的OpenStructs,我可以想到两种方法。
array_of_things.collect{|each_thing| each_thing.name}
返回所有内容的所有名称的数组。
array_of_things.select{|each_thing| each_thing.name =~ /Kristen/}
返回一组名称与表达式/Kristen/
匹配的内容。