Forum: Ruby String#clear

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-18 23:04
(Received via mailing list)
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 Gray II
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-04-18 23:07
(Received via mailing list)
James Edward Gray II wrote:
>
> Is there any good reason String can't have a #clear method?
>
> James Edward Gray II

$ irb19
irb(main):001:0> "foo".clear
=> ""
irb(main):002:0> RUBY_VERSION
=> "1.9.0"
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-18 23:19
(Received via mailing list)
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 Gray 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 Gray II
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-04-18 23:19
(Received via mailing list)
class String
   def clear
     replace ""
   end
end
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-18 23:34
James Gray 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 Gray 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 Gray II
--Greg
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-18 23:44
(Received via mailing list)
On Apr 18, 2006, at 5:34 PM, Gregory Seidman wrote:

> For future compatibility:
>
> class String
>   instance_method(:clear) rescue def clear
>                                    replace ""
>                                  end
> end

Now that's an idiom!
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2006-04-19 09:42
(Received via mailing list)
James Edward Gray II wrote:
> Is there any good reason String can't have a #clear method?

Better #empty!. IMHO.
This topic is locked and can not be replied to.