All,
New to Ruby and playing around. Here's my array:
fruits = ["banana", "apple", "pear", "orange"]
I accumulated like this:
longest_sentence = fruits.inject("banana") do |memo, fruit|
if memo == fruits[0]
puts "I like " + fruit + "s and "
else
puts fruit + "s and "
end
end
As expected, it gave me this:
I like bananas and
apples and
pears and
oranges and
=> nil
Here's are my two questions:
- How would you condense this code into a single line (i.e.no code block)?
- How would you display the results as a single string (i.e. "I like bananas and apples and pears and oranges and" without the awkward line returns)?
Thanks!