Getting the customer’s to_s method by looping through
Is there a Ruby idiom to write the code in 1 line (or shorter than 3 lines of code)?
def method
string = ""
@customers.each { |customer| string += customer.to_s + "\n" }
string
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.
joincreates a string from an array by callingto_son each element that is not already a string and inserting them into the new string separated by the parameter tojoin(in this case\n). Since your code also adds a\nat the end (andjoindoes not), you need to add+ "\n"after the call tojointo get the same behavior.