Professional Web Applications Themes

Destructive String Methods - Ruby

Is there a way to tell when a destructive method has been called? I've created a subclass of String to represent a URL. It has handy methods for extracting parts of the string like the host, port, path, or query (yes I know about URI). I'd like to cache the results of calling my extract methods unless the string has changed, in which case I'll extract and return the correct value again. The only way I can think of to tell when the string has changed is to dup in the initialize method and compare with self on every method ...

  1. #1

    Default Destructive String Methods

    Is there a way to tell when a destructive method has been called? I've
    created a subclass of String to represent a URL. It has handy methods
    for extracting parts of the string like the host, port, path, or query
    (yes I know about URI). I'd like to cache the results of calling my
    extract methods unless the string has changed, in which case I'll
    extract and return the correct value again.

    The only way I can think of to tell when the string has changed is to
    dup in the initialize method and compare with self on every method
    call. Are there other options?

    --
    John Long
    http://wiseheartdesign.com
    John Guest

  2. #2

    Default Re: Destructive String Methods

    Well, here -- http://www.rubycentral.com/ref/ref_c_string.html -- is
    the reference for the Ruby String class.

    The first thing that comes to mind is going through the list, finding
    destructive methods, and overriding them in your subclass with a call
    to some update function, like so:

    class String
    def chop!
    super

    update
    end

    # ...

    def update
    # invalidate the cache somehow
    end

    # ...
    end

    - Dan


    Dan Guest

  3. #3

    Default Re: Destructive String Methods


    "Dan Doel" <cwru.edu> schrieb im Newsbeitrag
    news:cwru.edu... 

    Just keep in mind to preserve the return values like in

    def chop!
    result = super
    update
    result
    end

    The other thing is constness... I feel, that - apart from freeze - Ruby is
    not so well suited to differentiating between const and non const instances.
    There are too many methods one has to take care of (just think of
    instance_eval and instance_variable_get). And even languages that are
    better suited to this task (C++ to name one) have problems of their own with
    constness...

    Kind regards

    robert

    Robert Guest

  4. #4

    Default Re: Destructive String Methods

    I was hoping for something other than that. I like to maintain forward
    compatibility.

    Of course some of this process could be automated by cycling through the
    methods and aliasing and redefining methods that end in !, but this wouldn't
    work for << or other methods that don't have the !.

    --
    John Long
    www.wiseheartdesign.com


    ----- Original Message -----
    From: "Dan Doel" <cwru.edu>
    To: "ruby-talk ML" <org>
    Sent: Monday, February 02, 2004 2:44 PM
    Subject: Re: Destructive String Methods

     




    John Guest

  5. #5

    Default Re: Destructive String Methods

    On Feb 2, 2004, at 19:23, John W. Long wrote:
     

    I don't know what you're doing with it, but this seems to be begging
    for a custom object. Perhaps the difficulty of doing what you want to
    is Ruby's way of telling you to adjust your design. Just a thought.


    Nathaniel

    <:((><



    Nathaniel Guest

  6. #6

    Default Re: Destructive String Methods

    > I don't know what you're doing with it, but this seems to be begging 

    Right. A to_s method on a custom object may be just the thing... Strings are
    strange objects though. In order to make an effective subclass it seems like
    there are times when an #updated method would be nice.

    --
    John




    John Guest

Similar Threads

  1. filling a components array with createClassObject self destructive?
    By jony_calavera webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: January 30th, 10:06 PM
  2. Destructive type conversion (to_i!, to_f!)
    By Charles Comstock in forum Ruby
    Replies: 5
    Last Post: November 24th, 08:14 AM
  3. #26350 [NEW]: Inherited methods can't access private methods
    By forseti at oak dot rpg dot pl in forum PHP Development
    Replies: 0
    Last Post: November 21st, 02:31 PM
  4. Replies: 2
    Last Post: August 16th, 10:26 AM
  5. Replies: 8
    Last Post: July 18th, 04:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139