Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Ruby/Rails: Natural increase of string (succ)

I would like to have consecutive invoice numbers, but the succ method sucks (pun intended) in this case.

'427'.succ
> '428' (works!)

'2021-9'.succ
> '2022-0' (does not work)

'2021.9'.succ
> '2022.0' (does not work)

I couldn't find a gem for this, only a gem to sort strings naturally. If no one knows any existing solutions, I will answer this question with a self-programmed method shortly.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use String#gsub and apply succ on the matched digit:

'427'.gsub(/\D(\d+)$|^\d+$/, &:succ)
# "428"
'2021-9'.gsub(/\D(\d+)$|^\d+$/, &:succ)
# "2021-10"
'2021-624'.gsub(/\D(\d+)$|^\d+$/, &:succ)
# "2021-625"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!