problems with literals and replacing classes

Discussion in 'Ruby' started by Mark Hubbart, May 14, 2004.

  1. Mark Hubbart

    Mark Hubbart Guest

    Hi all,

    I've been trying to get some code working that restores original
    versions of classes after they have been modified. I ran into a strange
    problem, though; it seems that when you use literals, they don't
    respect which class is actually assigned to their class constant.
    Here's an example of the basic concept I'm looking at, which shows my
    problem:

    # Add a method to String; returns nil
    class String; def foo() end end

    # Backup a copy of String class
    str = String.dup

    # Modify String#foo to return 23 instead of nil
    class String; def foo() 23 end end

    # Test #foo method; should return nil
    String.new.foo #=> 23
    "".foo #=> 23

    # Restore String class from backup copy
    String = str

    # Test #foo method. The first String class
    # doesn't have it, so it should raise an error.
    String.new.foo #=> nil
    "".foo #=> 23

    Why does the string literal use the old, modified version of the String
    class? I did various tests, "".class and String.new.class both return
    String; but the two String classes have different ids. I can't seem to
    get the literals to use the new class; they *always* use the original
    class.

    Is this a bug? Is there any way around it? Am I doing something stupid?

    cheers,
    --Mark
    Mark Hubbart, May 14, 2004
    #1
    1. Advertising

  2. On Sat, May 15, 2004 at 03:53:15AM +0900, Mark Hubbart wrote:
    > Hi all,
    >
    > I've been trying to get some code working that restores original
    > versions of classes after they have been modified. I ran into a strange
    > problem, though; it seems that when you use literals, they don't
    > respect which class is actually assigned to their class constant.
    > Here's an example of the basic concept I'm looking at, which shows my
    > problem:
    >
    > # Add a method to String; returns nil
    > class String; def foo() end end
    >
    > # Backup a copy of String class
    > str = String.dup
    >
    > # Modify String#foo to return 23 instead of nil
    > class String; def foo() 23 end end
    >
    > # Test #foo method; should return nil
    > String.new.foo #=> 23
    > "".foo #=> 23
    >
    > # Restore String class from backup copy
    > String = str
    >
    > # Test #foo method. The first String class
    > # doesn't have it, so it should raise an error.
    > String.new.foo #=> nil
    > "".foo #=> 23
    >
    > Why does the string literal use the old, modified version of the String
    > class? I did various tests, "".class and String.new.class both return
    > String; but the two String classes have different ids. I can't seem to
    > get the literals to use the new class; they *always* use the original
    > class.
    >
    > Is this a bug? Is there any way around it? Am I doing something stupid?


    It's no bug. You have modified the String class *object*. This object
    was bound to the String constant, but Ruby use this object, not the
    String constant to create new instances.

    Regards,

    Michael
    Michael Neumann, May 14, 2004
    #2
    1. Advertising

  3. Mark Hubbart

    Mark Hubbart Guest

    On May 14, 2004, at 12:13 PM, Michael Neumann wrote:

    >> Is this a bug? Is there any way around it? Am I doing something
    >> stupid?

    >
    > It's no bug. You have modified the String class *object*. This object
    > was bound to the String constant, but Ruby use this object, not the
    > String constant to create new instances.


    Well, that's kinda what I'd figured it was doing. But still, I would
    have thought that it would use the class defined in the String
    constant. Is it really *that* expensive to do a lookup?

    I'll probably have to find another way to do what I'm trying for; maybe
    using the 'evil' module to swap objects... But I really didn't want to
    have to do that.

    thanks,
    --Mark
    Mark Hubbart, May 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    8
    Views:
    16,424
  2. Replies:
    5
    Views:
    267
  3. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    254
    Chris Hohmann
    Apr 11, 2005
  4. Sam C.

    Problems with replacing text

    Sam C., Jan 23, 2007, in forum: Ruby
    Replies:
    4
    Views:
    86
    Glen Pfeiffer
    Jan 23, 2007
  5. Andrei Caragea

    several replacing problems

    Andrei Caragea, Jul 6, 2009, in forum: Ruby
    Replies:
    4
    Views:
    85
    Andrei Caragea
    Jul 8, 2009
Loading...

Share This Page