Gwater17 Gwater17 - 7 months ago 14
Ruby Question

Not quite clear how join operation works in ruby arrays

I'm reading Chris Pine's Learn to Program and I'm on Ch 7 discussing array methods. https://pine.fm/LearnToProgram/chap_07.html

One example using the join operation is

foods = ['artichoke', 'brioche', 'caramel']

puts foods.join(' :) ') + ' 8)'


and the output is

artichoke :) brioche :) caramel 8)


I am not clear why the :) is added after artichoke and brioche while 8) is added after caramel.

I'd appreciate any help!

Answer
 foods.join('  :)  ') + '  8)' 
 #  artichoke  :)  brioche  :)  caramel  8)
 a =  foods.join('  :)  ')
 # artichoke  :)  brioche  :)  caramel
 a + '  8)' 
 #  artichoke  :)  brioche  :)  caramel  8)

Join return string with separator injected between elements, to which string you add ' 8)'