Applying line-wrap to a long list of words

require 'dynarex'

dx = Dynarex.new 'http://www.jamesrobertson.eu/dynarex/' + \
                        '2015/dec/27/my-gems-which-don-t-have-dependencies.xml'

a = dx.flat_records.select{|x| x[:unmaintained] == ''}.map {|x| x[:title]}.sort

a2 = a.inject([%w(RUN gem install)]) do |r, word|

  if r.last.map(&:length).inject(&:+).to_i  + word.length + 
                                                        r.last.length < 80 then
    r.last << word    
  else
    r << [word]
  end

  r
end

s = a2.map {|x| x.join ' '}.join " \n"
puts s

In the above example a list of words are split into smaller lists, each containing words which will fit on a line not execeeding 80 characters.

Output:

RUN gem install asterisk_call_notifier christian_calendar classic_omah \
contacts_txt cronspeak daily_planner dandelion_s1 dockerfilemerge dummy_omah \
dynarex-events dynarex-levenshtein dynmap ec2i3 filetree_xml gmail_omah gogreen \
gtk2html gtk2notify hcitools_wrapper hlt-site_builder html_to_sliml humble_rpi \
humble_rpi-plugin-button humble_rpi-plugin-gps humble_rpi-plugin-led \
humble_rpi-plugin-lirc humble_rpi-plugin-logger humble_rpi-plugin-mtp \
humble_rpi-plugin-pir humble_rpi-plugin-relay humble_rpi-plugin-rsf \
indented-tracer indydoc ipscanner just-tmux kanban_txt leetpassword \
list_formatter liveblog liveblog-plugin-dxtags liveblog-plugin-indexer \
liveblog-plugin-sps lognotify maygion-ipcam morsecode mtlite mymedia-blogbase \
mymedia-card mymedia-dynarex mymedia-polyrex mymedia_ftp mymedia_snippets \
myuniversaljobsmatchfeed myweatherfeed obfsctr omah-plugin-sps \
omah-plugin-sqlite pfpux polyrex-calendar polyrex-feed-reader polyrex-headings \
polyrex-links polyrex-search polyrex-xslt railsbuilder rcscript-client \
requestor resultx rexle-xpath rot13 rpi_lcd16x2 rsc rscript-server rsf_services \
rsfhelper rss_timeline rsscache rx_sliml scotsman_obituaries sectionx \
simplepubsub simplevpim sps-ftpd-driver sps_bot sps_mqtt_bridge spsclient_m2m \
sqlparser tagcloud tagcloudbase testdata weekly_planner wii-robotic-arm \
wiimagickcam wiimote_sps xml_to_xsl yatter