I have an array of ruby objects that looks something like this:
[#<email: "someemail" other_properties: "SDFDF">, #<...>, #<...>]
Each of the objects in the array has an email
property. I want to get a new array of all the email properties of the ruby objects in the array.
After executing the code, I would have an array that looked like this:
["[email protected]", "[email protected]", ...]
I am newer to ruby and want to do this in the most rubyish way possible.
My question is, what's the best way to do this in ruby?
You can use the map
method to apply a block to each element of the array, returning a new array containing the results of each invocation:
somearray.map {|x| x.email}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With