Reputation: 6880
i have the following array:
[#<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"]>]
i am trying to filter all the 'name' fields from this. any help?
Upvotes: 0
Views: 996
Reputation: 434615
If you just want to extract all the names, use collect
(or its map
alias) to call the name
method on each element of the array and collect the results in another array:
names = a.collect(&:name)
Upvotes: 2
Reputation: 1808
Two ways I can think of to do this if these were standard OpenStructs.
array_of_things.collect{|each_thing| each_thing.name}
returns an array of all the names of all the things.
array_of_things.select{|each_thing| each_thing.name =~ /Kristen/}
returns an array of things with names that match the expression /Kristen/
.
Upvotes: 2