Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is String#chomp named like this

Tags:

ruby

Most Ruby methods are named (in my eyes) logically and are sometimes abbreviations of their action. Numeric#divmod returns the div ision quotient and mod ulus, Numeric#fdiv stands for f loat div ision and they both make sense.

What does chomp in String#chomp mean, or what does it stand for?

like image 701
Lukas_Skywalker Avatar asked Nov 25 '25 14:11

Lukas_Skywalker


1 Answers

It's an extension or play on the word "chop" (to cut). It also has a direct meaning:

chomp verb \ˈchämp, ˈchȯmp\
: to chew or bite on something

Source:
http://www.merriam-webster.com/dictionary/chomp

Ruby docs:

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).

So this method "chomps" or "chews" at the end of the string and removes it.

Hopefully that makes better sense now.

like image 50
Casper Avatar answered Nov 28 '25 13:11

Casper