Show a string of certain length without truncating

后端 未结 4 962
天命终不由人
天命终不由人 2021-01-20 22:18

I am displaying a string of certain length in ruby. only 80 characters of that string can be displayed in one line. for example if string length is 82 then it will be shown

4条回答
  •  遇见更好的自我
    2021-01-20 23:06

    Just out of curiosity:

    loop.inject([[], input]) do |(acc, src)|
      if m = src[/.{1,79}(\s|\z)/]
        [acc << m, $']
      else
        break acc << src
      end
    end
    

提交回复
热议问题