[MacRuby-devel] [macruby] #65: #sub!, #gsub!, and other bang methods mutate frozen strings

macruby ruby-noreply at macosforge.org
Wed May 28 15:42:31 PDT 2008


#65: #sub!, #gsub!, and other bang methods mutate frozen strings
--------------------------------+-------------------------------------------
 Reporter:  binary42 at gmail.com  |        Owner:  lsansonetti at apple.com
     Type:  defect              |       Status:  closed               
 Priority:  minor               |    Milestone:                       
Component:  MacRuby             |   Resolution:  wontfix              
 Keywords:                      |  
--------------------------------+-------------------------------------------
Changes (by lsansonetti at apple.com):

  * status:  new => closed
  * resolution:  => wontfix

Comment:

 I do not understand the problem. The subject says that bang methods mutate
 frozen strings, and the description says the contrary (that a RuntimeError
 is raised when trying to mutate a frozen string).

 In any way, the good behavior is that frozen strings should not be
 modifiable, and this is what MacRuby is currently doing (as well as 1.8
 and 1.9).

 {{{
 $ ruby -ve 's="foo";s.freeze;s.gsub!(/./, "x")'
 ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
 -e:1:in `gsub!': can't modify frozen string (TypeError)
         from -e:1
 }}}

 {{{
 $ macirb
 >> s='foo'
 => "foo"
 >> s.sub!(/./, 'x')
 => "xoo"
 >> s.freeze
 => "xoo"
 >> s.sub!(/./, 'x')
 RuntimeError: can't modify frozen string
         from (irb):4:in `sub!'
         from (irb):4
         from /usr/local/bin/macirb:12:in `<main>'
 >>
 }}}

 Also, note that if you build a non-mutable NSString by yourself, bang
 operations will also result in a RuntimeError.

 {{{
 >> s = NSString.new
 => ""
 >> s.clear
 RuntimeError: can't modify immutable string
         from (irb):6:in `clear'
         from (irb):6
         from /usr/local/bin/macirb:12:in `<main>'
 >> s = NSMutableString.new
 => ""
 >> s.clear
 => ""
 >>
 }}}

-- 
Ticket URL: <http://ruby.macosforge.org/trac/ticket/65#comment:1>
macruby <http://ruby.macosforge.org/>


More information about the MacRuby-devel mailing list