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

  • Home
  • SEARCH
  • 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 8829831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:55:30+00:00 2026-06-14T07:55:30+00:00

Why does chomp allow chaining but chomp! doesn’t? For example: HELLO .chomp.downcase #> hello

  • 0

Why does chomp allow chaining but chomp! doesn’t? For example:

"HELLO ".chomp.downcase
#> hello
"HELLO ".chomp!.downcase
#> nil

Another interesting example:

"100 ".chomp.to_i
#> 100
"100 ".chomp!.to_i
#> 0

Any ideas why this behavior occurs on the string, and why nil.to_i returns 0?

  • 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-14T07:55:31+00:00Added an answer on June 14, 2026 at 7:55 am

    From the fine manual:

    chomp(separator=$/) → new_str

    Returns a new String with the given record separator removed from the end of str (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

    and for chomp!:

    chomp!(separator=$/) → str or nil

    Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.

    So neither chomp nor chomp! do what you think they do. Observe:

    >> s = '100 '
    => "100 "
    >> s.chomp
    => "100 "
    >> s
    => "100 "
    >> s.chomp!
    => nil
    >> s
    => "100 "
    

    So neither one cares about trailing spaces unless you tell them to, they just strip off trailing EOLs by default.

    '100 '.chomp! returns nil because that’s what the documentation says it does. No substitution was made so it returns nil.

    Why does nil.to_i give you zero? Well, from the fine manual:

    to_i → 0

    Always returns zero.

    That doesn’t leave much room for ambiguity or interpretation.

    I think you’re actually after the strip family of methods rather than chomp:

    • String#lstrip
    • String#lstrip!
    • String#rstrip
    • String#rstrip!
    • String#strip
    • String#strip!

    Those remove whitespace from the string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does C++ have access to infile data through a filehandle? For example the typical
Does anyone know if there is a way to generate different code in the
Does COUNT(*) have any significant impact for MySQL performance if query already has GROUP
Does anyone have a translate function for x/y positions after rotation in javascript? for
Does anyone know of a free tool, similar to what is built into Visual
Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does anyone out there know how to integrate ContentFlow ( http://www.jacksasylum.eu/ContentFlow ) and Lightbox2
Does the following psuedo code accomplish my goal of cleaning up after myself when
Does the G'MIC (Grey's Magic Image Converter) library work on iOS ? what steps
Does anyone know of a mechanism in Sybase ASA 9 / Sybase SQL Anywhere

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.