add character to first and last array elements in ruby

asked 2018-08-31 03:29:12 -0500

gilbo gravatar image


I'm trying to format the output of a custom fact in ruby.

I've got this array:

array = [ "a", "b", "c", "d", "e", "f", "g", "h" ]

what i'd like to do is add a character to the first and last elements, so i end up with:

[ "na", "b", "c", "d", "e", "f", "g", "hn" ]

I've tried all sorts of ways using an if array.first? or array.last? conditions in a map! statement but just can't seem to get there. Any help appreciated!


edit retag flag offensive close merge delete