Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:30:25+00:00 2026-05-27T10:30:25+00:00

I managed to cobble together this statement based on lots of help and copying

  • 0

I managed to cobble together this statement based on lots of help and copying and pasting. It basically returns the first x number of words in a string and im using it as a helper in my app.

Could someone please help me understand how I would add a condition to say if the actual string is less than x words don’t add the finishing bit (which is a …). So in the equation below I’ like the ‘finish’ section to only be added if they are more than the number of words passed into the equation.

 def first_x_words(str,n=20,finish='…')
    str.split(' ')[0,n].inject{|sum,word| sum + ' ' + word} + finish
 end

Actually – if I could make it more complicated, is it possible, after I find a condition where there are less than x words, to check to see if the last 4 characters are </p> and if they are, remove them.

Thanks,
Adam

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:30:25+00:00Added an answer on May 27, 2026 at 10:30 am

    This should do what you’re looking for:

    def first_x_words(str, n = 20, finish = '&hellip;')
      # By default, Ruby will split on whitespace, so no
      # argument needs to be passed.
      words = str.split
    
      # Rebuild 'n' words into a new string.
      truncated = words[0..n-1].inject do |sum, word|
        sum << ' ' << word
      end
    
      # Either append a finishing string or remove any
      # trailing '</p>' tag.
      if words.length > n
        truncated << finish
      else
        truncated.chomp!("</p>")
      end
    
      # Return the completed string.
      truncated
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've managed to cobble together a CI system using Jenkins with an Ant build
I managed to cobble together a suitable implementation of a sample game in F#
I managed to get some help from a previous question and I have the
I managed to get authentication to work by following the tutorial from this page
In managed C++/CLI, I could do this either as (1): array<System::Byte>^ css_keycode = {0x51,
Seems this namespace is limited to IIS7 and upwards. Are there any managed libraries
I managed to update my repeater based on a dropdown change using the onItemChanged
I managed to get a fr.yaml. I want this to be the default, an
I managed to do this on excel by using the following (note that I
I managed to create a class using CodeDom and add a single method to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.