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 7877905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:30:39+00:00 2026-06-03T03:30:39+00:00

I thought I’d throw out this problem to see what elegant solutions folk could

  • 0

I thought I’d throw out this problem to see what elegant solutions folk
could come up with and, in the process, hopefully learn some new ruby
tricks.

I’ll set the problem in the context of producing a twitter message,
which has a maximum length of 140 characters. I’m looking for a concise
function that will deliver a tweet no longer than 140 characters from
three inputs: text_a (mandatory), text_b (optional), boolean that
triggers a function that returns a string (optional).

(I’ve used the twitter-text gem to take byte, char, and encoding issues
out of play, as that is not the focus of the problem.)

The main constraint is that to achieve the required maximum length, it
is text_a that must be truncated.

Here’s some long-winded sample code (working, I think) that hopefully
makes the requirement clear.

# encoding: utf-8

require 'twitter-text'

def tweet(text_a, text_b=nil, suffix=false)
  text = "fixed preamble #{text_a}"
  text << " #{text_b}" if text_b
  text << get_suffix if suffix
  return text unless Twitter::Validation.tweet_invalid?(text) == :too_long
  excess_length = Twitter::Validation.tweet_length(text) - Twitter::Validation::MAX_LENGTH
  text_a = text_a[0..-(excess_length + 1)]
  text = "fixed preamble #{text_a}"
  text << " #{text_b}" if text_b
  text << get_suffix if suffix
  text
end

def get_suffix
  " some generated suffix"
end

It’s ugly, especially with the duplication. Ideas?

  • 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-06-03T03:30:41+00:00Added an answer on June 3, 2026 at 3:30 am

    Why not build the string properly in the first place?

    def tweet(text_a, text_b=nil, suffix=false)
      text = ""
      text << " #{text_b}" if text_b
      text << get_suffix if suffix
    
      space = Twitter::Validation::MAX_LENGTH - Twitter::Validation.tweet_length(text)
      raise "too long" unless space > 0
    
      "fixed preamble #{text_a}"[0, space] + text
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thought I would throw this one out there and see what other people's experiences
I thought I could figure this out on my own, but apparently I am
Never thought I'd have this problem :) The following snippet of code works in
I thought this would be fairly trivial but it's turned out be more difficult
I thought this would be easy, but after scanning many pages Google hasn't come
I thought I had this figured out, but can't get it working correctly: All
I thought I had got this to work before, but I just don't see
I thought I'd offer this softball to whomever would like to hit it out
Thought I would pose this to the StackOverflow crowd since I've run out of
Thought my range of search options would easily find this. I wish to combine

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.