# String#clear

**URL:** <https://www.ruby-forum.com/t/string-clear/94040>\
**Category:** Ruby\
**Created:** [April 18, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/string-clear/94040 "2006-04-18T23:04:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/string-clear/94040/1 "2006-04-18T23:04:03Z")

</div>

Today I was using an Array and a String to track some info in my unit  
tests. I needed to clear them in the #teardown routine. My first  
thought was:

[@some\_arr, @some\_str].each { |var| var.clear }

I was surprised when that exploded and I had to change it to:

@some\_arr.clear  
@some\_str.replace("")

Is there any good reason String can’t have a #clear method?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/string-clear/94040/2 "2006-04-18T23:07:10Z")

</div>

James Edward G. II wrote:

> Is there any good reason String can’t have a #clear method?
> 
> James Edward G. II

$ irb19  
irb(main):001:0\> “foo”.clear  
=\> “”  
irb(main):002:0\> RUBY\_VERSION  
=\> “1.9.0”

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/string-clear/94040/3 "2006-04-18T23:19:31Z")

</div>

On Apr 18, 2006, at 4:06 PM, Joel VanderWerf wrote:

> > @some\_str.replace("")
> > 
> > Is there any good reason String can’t have a #clear method?
> > 
> > James Edward G. II
> 
> $ irb19  
> irb(main):001:0\> “foo”.clear  
> =\> “”  
> irb(main):002:0\> RUBY\_VERSION  
> =\> “1.9.0”

Sweet: now I’ll move to the obvious follow-up question… Why are  
we not adding this to Ruby 1.8.x?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/string-clear/94040/4 "2006-04-18T23:19:36Z")

</div>

class String  
def clear  
replace “”  
end  
end

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/string-clear/94040/5 "2006-04-18T23:34:26Z")

</div>

James G. wrote:

> On Apr 18, 2006, at 4:06 PM, Joel VanderWerf wrote:
> 
> > > @some\_str.replace("")
> > > 
> > > Is there any good reason String can’t have a #clear method?
> > > 
> > > James Edward G. II
> > 
> > $ irb19  
> > irb(main):001:0\> “foo”.clear  
> > =\> “”  
> > irb(main):002:0\> RUBY\_VERSION  
> > =\> “1.9.0”
> 
> Sweet: now I’ll move to the obvious follow-up question… Why are  
> we not adding this to Ruby 1.8.x?

For future compatibility:

class String  
instance\_method(:clear) rescue def clear  
replace “”  
end  
end

> James Edward G. II  
> –Greg

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 18, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/string-clear/94040/6 "2006-04-18T23:44:07Z")

</div>

On Apr 18, 2006, at 5:34 PM, Gregory S. wrote:

> For future compatibility:
> 
> class String  
> instance\_method(:clear) rescue def clear  
> replace “”  
> end  
> end

Now that’s an idiom!

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [April 19, 2006, 9:42am UTC](https://www.ruby-forum.com/t/string-clear/94040/7 "2006-04-19T09:42:10Z")

</div>

James Edward G. II wrote:

> Is there any good reason String can’t have a #clear method?

Better #empty!. IMHO.
