想要在ruby中显示文本字段的前50或60个单词吗?

时间:2022-09-13 09:10:21

I have a stories text field and want to show the first few lines – say the first 50 words of that field – in a snapshot page. How can I do that in Ruby (on Rails)?

我有一个故事文本字段,想要在快照页面中显示前几行 - 比如该字段的前50个单词。我怎么能在Ruby(在Rails上)做到这一点?

3 个解决方案

#1


Mostly the same as Aaron Hinni's answer, but will try and keep 3 full sentences (then truncate to 50 words, if it's the sentences were too long)

大部分与Aaron Hinni的答案相同,但会尝试保留3个完整的句子(然后截断为50个单词,如果它的句子太长了)

def truncate(text, max_sentences = 3, max_words = 50)
  # Take first 3 setences (blah. blah. blah)
  three_sentences = text.split('. ').slice(0, max_sentences).join('. ')
  # Take first 50 words of the above
  shortened = three_sentences.split(' ').slice(0, max_words).join(' ')
  return shortened # bah, explicit return is evil
end

Also, if this text has any HTML, my answer on "Truncate Markdown?" might be of use

此外,如果此文本有任何HTML,我的答案是“截断Markdown?”可能有用

#2


Assuming your words are delimited by a space, you can do something like this.

假设你的单词是由空格分隔的,你可以做这样的事情。

stories.split(' ').slice(0,50).join(' ')

#3


In use something very similar in a Rails application to extend ("monkey patch") the base String class.

在Rails应用程序中使用非常相似的东西来扩展(“猴子补丁”)基本的String类。

I created lib/core_extensions.rb which contains:

我创建了lib / core_extensions.rb,其中包含:

class String
  def to_blurb(word_count = 30)
    self.split(" ").slice(0, word_count).join(" ")
  end
end

I then created config/initializers/load_extensions.rb which contains:

然后我创建了config / initializers / load_extensions.rb,其中包含:

require 'core_extensions'

Now I have the to_blurb() method on all my String objects in the Rails application.

现在我在Rails应用程序中的所有String对象上都有to_blurb()方法。

#1


Mostly the same as Aaron Hinni's answer, but will try and keep 3 full sentences (then truncate to 50 words, if it's the sentences were too long)

大部分与Aaron Hinni的答案相同,但会尝试保留3个完整的句子(然后截断为50个单词,如果它的句子太长了)

def truncate(text, max_sentences = 3, max_words = 50)
  # Take first 3 setences (blah. blah. blah)
  three_sentences = text.split('. ').slice(0, max_sentences).join('. ')
  # Take first 50 words of the above
  shortened = three_sentences.split(' ').slice(0, max_words).join(' ')
  return shortened # bah, explicit return is evil
end

Also, if this text has any HTML, my answer on "Truncate Markdown?" might be of use

此外,如果此文本有任何HTML,我的答案是“截断Markdown?”可能有用

#2


Assuming your words are delimited by a space, you can do something like this.

假设你的单词是由空格分隔的,你可以做这样的事情。

stories.split(' ').slice(0,50).join(' ')

#3


In use something very similar in a Rails application to extend ("monkey patch") the base String class.

在Rails应用程序中使用非常相似的东西来扩展(“猴子补丁”)基本的String类。

I created lib/core_extensions.rb which contains:

我创建了lib / core_extensions.rb,其中包含:

class String
  def to_blurb(word_count = 30)
    self.split(" ").slice(0, word_count).join(" ")
  end
end

I then created config/initializers/load_extensions.rb which contains:

然后我创建了config / initializers / load_extensions.rb,其中包含:

require 'core_extensions'

Now I have the to_blurb() method on all my String objects in the Rails application.

现在我在Rails应用程序中的所有String对象上都有to_blurb()方法。