String#clear

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

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”

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

class String
def clear
replace “”
end
end

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

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!

James Edward G. II wrote:

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

Better #empty!. IMHO.