# Chomp like Perl operator in Ruby

**URL:** https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541
**Category:** ruby-talk
**Created:** [15 May 2010 21:26 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541 "2010-05-15T21:26:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Parag\_Kalra](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@Parag\_Kalra](https://rubytalk.org/u/Parag_Kalra)
#### Post date: [15 May 2010 21:26 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541/1 "2010-05-15T21:26:39Z")

</div>

Is there a chomp like Perl operator in Ruby using which I can literally  
remove new line characters as show below in my Perl script:

use strict;  
use warnings;

my $str1 = "Hello\n";  
my $str2 = "World";  
print $str1.$str2;

print "\n";

chomp($str1);

print $str1.$str2;

print "\n";

Any clue?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Ben\_Bleything1](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@Ben\_Bleything1](https://rubytalk.org/u/Ben_Bleything1)
#### Post date: [15 May 2010 21:34 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541/2 "2010-05-15T21:34:20Z")

</div>

There's a really easy way to answer this question for yourself:

$ ri chomp  
(from ruby core)  
Implementation from String

> **···**
>
> On Sat, May 15, 2010 at 2:26 PM, Parag Kalra \<paragkalra@gmail.com\> wrote:
> 
> > Is there a chomp like Perl operator in Ruby using which I can literally  
> > remove new line characters as show below in my Perl script:
> 
> ------------------------------------------------------------------------------  
> &nbsp;&nbsp;str.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).

---

<div class="post-metadata">

### Author: ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)
#### Post date: [15 May 2010 21:45 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541/3 "2010-05-15T21:45:29Z")

</div>

The online docs (plus a few examples) for the method Ben showed the ri of  
[http://ruby-doc.org/core/classes/String.html#M000819](http://ruby-doc.org/core/classes/String.html#M000819)

> **···**
>
> On Sat, May 15, 2010 at 4:26 PM, Parag Kalra \<paragkalra@gmail.com\> wrote:
> 
> > Is there a chomp like Perl operator in Ruby using which I can literally  
> > remove new line characters as show below in my Perl script:
> > 
> > use strict;  
> > use warnings;
> > 
> > my $str1 = "Hello\n";  
> > my $str2 = "World";  
> > print $str1.$str2;
> > 
> > print "\n";
> > 
> > chomp($str1);
> > 
> > print $str1.$str2;
> > 
> > print "\n";
> > 
> > Any clue?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Parag\_Kalra](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@Parag\_Kalra](https://rubytalk.org/u/Parag_Kalra)
#### Post date: [15 May 2010 22:19 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541/4 "2010-05-15T22:19:10Z")

</div>

Thanks it worked:

str="Hello\n"  
str2="World"

puts str+str2

str1=str.chomp

puts str1+str2

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)
#### Post date: [16 May 2010 00:48 UTC](https://rubytalk.org/t/chomp-like-perl-operator-in-ruby/58541/5 "2010-05-16T00:48:05Z")

</div>

Not the whole story. If you know you're not going to use the original string,  
you might consider chomp! instead.

> **···**
>
> On Saturday, May 15, 2010 04:34:20 pm Ben Bleything wrote:
> 
> > On Sat, May 15, 2010 at 2:26 PM, Parag Kalra \<paragkalra@gmail.com\> wrote:  
> > \> Is there a chomp like Perl operator in Ruby using which I can literally
> > 
> > \> remove new line characters as show below in my Perl script:  
> > There's a really easy way to answer this question for yourself:
> > 
> > $ ri chomp
