ruby - Join array of strings into 1 or more strings each within a certain char limit (+ prepend and append texts) -


let's have array of twitter account names:

string = %w[example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12 example13 example14 example15 example16 example17 example18 example19 example20] 

and prepend , append variable:

prepend = 'check out these cool people: ' append = ' #followfriday' 

how can turn array of few strings possible each maximum length of 140 characters, starting prepend text, ending append text, , in between twitter account names starting @-sign , separated space. this:

tweets = ['check out these cool people: @example1 @example2 @example3 @example4 @example5 @example6 @example7 @example8 @example9 #followfriday', 'check out these cool people: @example10 @example11 @example12 @example13 @example14 @example15 @example16 @example17 #followfriday', 'check out these cool people: @example18 @example19 @example20 #followfriday'] 

(the order of accounts isn't important theoretically try , find best order make use of available space, that's not required.)

any suggestions? i'm thinking should use scan method, haven't figured out right way yet.

it's pretty easy using bunch of loops, i'm guessing won't necessary when using right ruby methods. here's came far:

# create 1 long string of @usernames separated space tmp = twitter_accounts.map!{|a| a.insert(0, '@')}.join(' ') # alternative: tmp = '@' + twitter_accounts.join(' @')  # number of characters left mentioning twitter accounts length = 140 - (prepend + append).length  # method split string multiple strings # each maximum length of 'length' , split on empty spaces (' ') # ideally strip space (although .map(&:strip) use too)  tweets = tmp.some_method(' ', length)  # prepend , append tweets.map!{|t| prepend + t + append} 

p.s. if has suggestion better title let me know. had difficult time summarizing question.

i'd make use of reduce this:

string = %w[example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12 example13 example14 example15 example16 example17 example18 example19 example20] prepend = 'check out these cool people:' append = '#followfriday'  # -1 space before `append` max_content_length = 140 - prepend.length - append.length - 1  content_strings = string.reduce([""]) { |result, target|   result.push("") if result[-1].length + target.length + 2 > max_content_length   result[-1] += " @#{target}"    result }  tweets = content_strings.map { |s| "#{prepend}#{s} #{append}" } 

which yield:

"check out these cool people: @example1 @example2 @example3 @example4 @example5 @example6 @example7 @example8 @example9 #followfriday" "check out these cool people: @example10 @example11 @example12 @example13 @example14 @example15 @example16 @example17 #followfriday" "check out these cool people: @example18 @example19 @example20 #followfriday" 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -