From: Gunnar Andersson Date: 2002-02-13T08:40:04+09:00 Subject: Re: [newbie] make first letter lowercase Hi everyone! Cool ideas with the parts/expose... Some discussion points: I think Matju's original Part, or Slice or Fragment are reasonable names and maybe it's good to clarify that it doesn't return a String but something different. I used expose() below but the names part(), slice() or fragment() tell you what you actually get back. Because if you consider that side of it, you would be inclined to actually handle the return value of expose/part/fragment. But I'm not sure it's a good thing after all. (Arguing with myself for a while...) because a ref to this thing is powerful but a little scary. It's like having a pointer into the middle of a C-string that you use later on - I mean I would find such code harder to understand and debug, you see what I mean? The other way, the name expose() doesn't really tell you what you get back and instead suggests the user to use it in that manner: expose().something! which might be a good thing if it can avoid proliferation of these "dangerous" Fragment objects which effectively alias (part of) the original object. I guess what I'm saying is that such simple things like the actual names of things can affect people's use of these features and, basically, their coding style. You give people powerful tools and they get crazy ideas! ;-) But I'm not your mommy so why should I care? To conclude, I see advantages and disadvantages with both types of names. What do you think? OK, so just for fun, here's a first attempt at a Ruby hack to do it. I love it when Ruby just makes it possible. While it isn't always the nicest solutions, at least everyone can play around with it to get the feel for it without any hacking/recompile of the interp. It's based on a method_missing hack like so many other such tricks. (What did I say about providing powerful stuff in the language? :-) Lots to improve I'm sure... It now returns the result of the operation, not self so you can't chain operations. Actually if method_missing returned self instead it should work, but there are some other problems I see. I think it'll bug out if the first op returns a longer string than the original fragment (the range is not reset to the longer length), and so on. Should be possible to fix something though... Later... /Gunnar ---- class String def expose(range) @range = range Fragment.new(self, range) end def expose_result(s) self[@range] = s end end class Fragment def initialize(string, range) @string = string @range = range end def method_missing(m, *args) s = @string[@range] retval = s.send(m, *args) @string.expose_result(s) retval end def inspect; "\"#{@string[@range]}\""; end end # Try it out: s2 = "The quick brown fox" fragment = s2.expose(4..8) # >> "quick" p fragment p fragment.upcase # >> "QUICK" p s2 # >> "The quick brown fox" fragment.upcase! # >> "QUICK", also affects s2 p s2 # >> "The QUICK brown fox" # Substitute within fragment only s1 = "The quick quick quick brown fox" s1.expose(10..20).gsub!(/quick/, "slow") # >> "The quick slow slow..." p s1 s = "No squeezing here. But here: -X X X X X X-" p s s.expose(34..-1).squeeze!(" ") p s __________________________________________________ Do You Yahoo!? Send FREE Valentine eCards with Yahoo! Greetings! http://greetings.yahoo.com