6

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?

1 Answer 1

16

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}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This exactly what I was after. I will accept as soon as SO lets me. :)
In Ruby 1.87+ you can simplify this as somearray.map(&:email)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.