Ruby 将字符串数组连接到一个或多个字符串中,每个字符串在一定的字符限制内(+;前置和追加文本)

Ruby 将字符串数组连接到一个或多个字符串中,每个字符串在一定的字符限制内(+;前置和追加文本),ruby,regex,string,Ruby,Regex,String,假设我有一组Twitter帐户名: string = %w[example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12 example13 example14 example15 example16 example17 example18 example19 example20] 以及前置和追加变量: prepend = 'Check

假设我有一组Twitter帐户名:

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'
我如何将其转换为一个尽可能少的字符串数组,每个字符串的最大长度为140个字符,以前置文本开始,以追加文本结束,在Twitter帐户名之间,所有帐户名都以@符号开始,并用空格分隔。像这样:

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']
(账户的顺序并不重要,因此理论上,您可以尝试找到最佳顺序,以最大限度地利用可用空间,但这不是必需的。)

有什么建议吗?我想我应该使用
扫描
方法,但还没有找到正确的方法

使用一堆循环非常容易,但我猜在使用正确的Ruby方法时,这是不必要的。以下是我到目前为止的想法:

# Create one long string of @usernames separated by a space
tmp = twitter_accounts.map!{|a| a.insert(0, '@')}.join(' ')
# alternative: tmp = '@' + twitter_accounts.join(' @')

# Number of characters left for mentioning the Twitter accounts
length = 140 - (prepend + append).length

# This method would split a string into multiple strings
# each with a maximum length of 'length' and it will only split on empty spaces (' ')
# ideally strip that space as well (although .map(&:strip) could be use too) 
tweets = tmp.some_method(' ', length)

# Prepend and append
tweets.map!{|t| prepend + t + append}
附言。 如果有人有更好的标题建议,请告诉我。我很难总结我的问题。

我会利用以下方法:

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'

# Extra -1 is for the 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}" }
这将产生:

"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"
我会利用这一点:

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'

# Extra -1 is for the 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}" }
这将产生:

"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"

String
rindex
方法有一个函数,您可以在其中指定从何处开始在字符串中向后搜索:

arr = %w[example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12 example13 example14 example15 example16 example17 example18 example19 example20]
str = arr.map{|name|"@#{name}"}.join(' ')
prepend =  'Check out these cool people: '
append = ' #FollowFriday'
max_chars = 140 - prepend.size - append.size
until str.size <= max_chars do
  p str.slice!(0, str.rindex(" ", max_chars))
  str.lstrip! #get rid of the leading space
end
p str unless str.empty?
arr=%w[示例1示例2示例3示例4示例5示例6示例7示例8示例9示例10示例11示例12示例13示例14示例15示例16示例17示例18示例19示例20]
str=arr.map{| name |“@#{name}}.join(“”)
prepend='看看这些酷人:'
追加=“#跟随星期五”
max_chars=140-prepend.size-append.size

在str.size之前,String
rindex
方法具有一个可指定从何处开始在字符串中向后搜索的参数:

arr = %w[example1 example2 example3 example4 example5 example6 example7 example8 example9 example10 example11 example12 example13 example14 example15 example16 example17 example18 example19 example20]
str = arr.map{|name|"@#{name}"}.join(' ')
prepend =  'Check out these cool people: '
append = ' #FollowFriday'
max_chars = 140 - prepend.size - append.size
until str.size <= max_chars do
  p str.slice!(0, str.rindex(" ", max_chars))
  str.lstrip! #get rid of the leading space
end
p str unless str.empty?
arr=%w[示例1示例2示例3示例4示例5示例6示例7示例8示例9示例10示例11示例12示例13示例14示例15示例16示例17示例18示例19示例20]
str=arr.map{| name |“@#{name}}.join(“”)
prepend='看看这些酷人:'
追加=“#跟随星期五”
max_chars=140-prepend.size-append.size

在str.size之前,我从未想到编写twitter机器人需要解决背包问题(或者更确切地说是子集求和)呸。我只是想澄清一下,我并没有用这个机器人随意向人们发送垃圾邮件。在这一周里,我在博客上写了一些初创公司(每天约3家),我想我会在周末提到他们的Twitter账户,因为我的追随者对此很感兴趣,这也是这些公司的额外曝光。我从来没有想到你会需要解决背包问题(或者更确切地说是子集总和)要编写twitter机器人,请执行以下操作:呸。我只是想澄清一下,我并没有用这个机器人随意向人们发送垃圾邮件。在这一周里,我在博客上写了一些初创公司(~3/天),我想我会在周末提到他们的Twitter账户,因为我的追随者对此很感兴趣,这也是这些公司的额外曝光。我没有考虑过使用
slice
rindex
。我已经将Never的答案标记为可接受的答案,因为他速度稍快,包含了前置和附加文本,但我也非常感谢您的帮助。谢谢没有考虑过使用
slice
rindex
。我已经将Never的答案标记为可接受的答案,因为他速度稍快,包含了前置和附加文本,但我也非常感谢您的帮助。谢谢