Using arrays what’s the main difference between collect and each? Preference?
some = []
some.collect do {|x| puts x}
some.each do |x|
puts x
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
array = []is a shortcut to define an array object (long form:array = Array.new)Array#collect(andArray#map) return a new array based on the code passed in the block.Array#eachperforms an operation (defined by the block) on each element of the array.I would use collect like this:
And each like this:
Hope this helps…