From: Robert Klemme Date: 2007-05-16T00:10:07+09:00 Subject: Re: Why was the "Symbol is a String"-idea dropped? On 15.05.2007 16:34, Robert Dober wrote: > On 5/15/07, Robert Klemme wrote: >> On 15.05.2007 15:54, Robert Dober wrote: >> > On 5/15/07, Brian Candler wrote: >> > >> >> But then what you want are not symbols, but true immutable strings. By >> >> that >> >> I mean: some object where I can write 10MB of binary dump. If I want >> >> to add >> >> one character to the end of it, then I create another object >> containing >> >> 10MB+1byte of binary dump, and the old 10MB object is >> garbage-collected. >> > But of course we have immutable strings already :))) >> > >> > class IString < String >> > def initialize str >> > super(str) >> > freeze >> > end >> > end >> >> What advantages does this have over using "freeze" directly? > > Dunno :) > > x = IString.new("Hello World") # Not even tested yet > vs. > x="HelloWorld".freeze > > Well the first one has the advantage that I thought about it ;) > > Now I reckon that the subclass stuff is baaad > > def blah str > raise ArgumentError unless IString === str > ... > end > > but now someone does > class MString < IString > get rid of the freeze (by calling superclass.superclass.new in > self.class.new e.g) > end > > and my code is broken, while in > > > def blah str > raise ArgumentError unless str.respond_to? :frozen && str.frozen? > ... > end > > frozen is frozen forever. Corrent. And since #frozen? is defined in Kernel you can skip the first test. > So do what Robert told you and beware of what Robert told you;) :-) >> > You see things; and you say Why? >> > But I dream things that never were; and I say Why not? >> > -- George Bernard Shaw >> >> Greetings to George, btw. :-) > Well last time I met him he was admiring your posts to the list :-P Wow! So he didn't die but just went home like this other guy who invented a vi clone (or at least provided his name for the operation)... :-) >> robert >> >> > idem :-) While we're at it: *if* you want to define something (and are a fan of C++) you can do this: irb(main):001:0> module Kernel irb(main):002:1> private irb(main):003:1> def const(*a) a.each {|x| x.freeze } end irb(main):004:1> end => nil irb(main):005:0> nil => nil irb(main):006:0> foo, bar = const "foo", "bar" => ["foo", "bar"] irb(main):007:0> ["foo", "bar"] => ["foo", "bar"] irb(main):008:0> foo << bar TypeError: can't modify frozen string from (irb):8:in `<<' from (irb):8 from :0 irb(main):009:0> bar << foo TypeError: can't modify frozen string from (irb):9:in `<<' from (irb):9 from :0 irb(main):010:0> Hihi... Kind regards robert