Marshal and allocating objects

Discussion in 'Ruby' started by Stephen Sykes, Sep 4, 2004.

  1. I would find it useful if Marshal were to use "allocate" to generate
    new objects. This is so that I can load unknown objects by doing
    something like this:

    class Object
    def self.const_missing(name)
    const_set(name, kl = Class.new)
    kl
    end
    end

    And then be able to load objects with marshal without getting
    ArgumentError: undefined class/module.

    For example (after running the above code)...

    irb(main):026:0> M.allocate
    => #<M:0x2bb2168>

    That works ok, but...

    irb(main):029:0> Marshal.load("\004\010o:\006N\000")
    ArgumentError: undefined class/module N
    from (irb):29:in `load'
    from (irb):29
    from (null):0

    If Mashal used allocate, and hence called const_missing, then the load
    would not fail. Sound reasonable?

    Stephen Sykes
    Stephen Sykes, Sep 4, 2004
    #1
    1. Advertising

  2. Stephen  Sykes

    Paul Brannan Guest

    On Sat, Sep 04, 2004 at 07:45:22PM +0900, Stephen Sykes wrote:
    > I would find it useful if Marshal were to use "allocate" to generate
    > new objects.


    It does use allocate, for objects of type T_DATA (objects created from C
    extensions).

    > This is so that I can load unknown objects by doing something like
    > this:
    >
    > class Object
    > def self.const_missing(name)
    > const_set(name, kl = Class.new)
    > kl
    > end
    > end
    >
    > And then be able to load objects with marshal without getting
    > ArgumentError: undefined class/module.


    Even if allocate is used, this won't work. The lookup for the class is
    failing before allocate is called.

    > For example (after running the above code)...
    >
    > irb(main):026:0> M.allocate
    > => #<M:0x2bb2168>
    >
    > That works ok, but...
    >
    > irb(main):029:0> Marshal.load("\004\010o:\006N\000")
    > ArgumentError: undefined class/module N
    > from (irb):29:in `load'
    > from (irb):29
    > from (null):0
    >
    > If Mashal used allocate, and hence called const_missing, then the load
    > would not fail. Sound reasonable?


    I don't like your solution, since now if I write:

    class Foo
    SOME_CONSTANT = 1
    def foo
    return SOME_CONSTATN
    end
    end

    my code will no longer break, but will silently do the wrong thing.

    Arguably better would be a callback for when Marshal.load cannot find
    the class it needs. I say arguably because there really is no good way
    to create a class with the correct behavior unless you already know
    something about the class.

    Paul
    Paul Brannan, Sep 8, 2004
    #2
    1. Advertising

  3. Stephen  Sykes

    ts Guest

    >>>>> "P" == Paul Brannan <> writes:

    P> Even if allocate is used, this won't work. The lookup for the class is
    P> failing before allocate is called.

    Well, if I've well understood he want that Marshal use the same algorithm
    than ruby when it find a constant name.

    Actually marshal just look is the constant is defined, when ruby can call
    #const_missing when he write `M.allocate'

    P> class Foo
    P> SOME_CONSTANT = 1
    P> def foo
    P> return SOME_CONSTATN
    P> end
    P> end

    P> my code will no longer break, but will silently do the wrong thing.

    Not understood, sorry


    Guy Decoux
    ts, Sep 8, 2004
    #3
  4. Stephen  Sykes

    Paul Brannan Guest

    On Wed, Sep 08, 2004 at 11:31:04PM +0900, ts wrote:
    > >>>>> "P" == Paul Brannan <> writes:

    >
    > P> Even if allocate is used, this won't work. The lookup for the class is
    > P> failing before allocate is called.
    >
    > Well, if I've well understood he want that Marshal use the same algorithm
    > than ruby when it find a constant name.


    That's the way I read it.

    > Actually marshal just look is the constant is defined, when ruby can call
    > #const_missing when he write `M.allocate'


    Right, which is why const_missing never gets called. Making Ruby call
    allocate doesn't solve the problem, because Ruby is checking to see if
    the constant exists (in rb_path2name) before trying to look it up.

    > P> class Foo
    > P> SOME_CONSTANT = 1
    > P> def foo
    > P> return SOME_CONSTATN
    > P> end
    > P> end
    >
    > P> my code will no longer break, but will silently do the wrong thing.
    >
    > Not understood, sorry


    In order to demonstrate why I do not think const_missing should be
    defined for class Object, I intentionally misspelled "SOME_CONSTATN".
    Before const_missing was defined I would get an exception here,
    whereas after Stephen's version of const_missing is in place, I will
    instead get a new class. Even with well-written unit tests, finding
    this misspelling can be a challenge.

    Paul
    Paul Brannan, Sep 8, 2004
    #4
  5. Stephen  Sykes

    ts Guest

    >>>>> "P" == Paul Brannan <> writes:

    P> In order to demonstrate why I do not think const_missing should be
    P> defined for class Object, I intentionally misspelled "SOME_CONSTATN".
    P> Before const_missing was defined I would get an exception here,
    P> whereas after Stephen's version of const_missing is in place, I will
    P> instead get a new class. Even with well-written unit tests, finding
    P> this misspelling can be a challenge.

    and ???

    This does not his problem where `M.allocate' work but not 'Marshal.load()'


    Guy Decoux
    ts, Sep 8, 2004
    #5
  6. Stephen  Sykes

    Paul Brannan Guest

    On Wed, Sep 08, 2004 at 11:47:48PM +0900, ts wrote:
    > >>>>> "P" == Paul Brannan <> writes:

    >
    > P> In order to demonstrate why I do not think const_missing should be
    > P> defined for class Object, I intentionally misspelled "SOME_CONSTATN".
    > P> Before const_missing was defined I would get an exception here,
    > P> whereas after Stephen's version of const_missing is in place, I will
    > P> instead get a new class. Even with well-written unit tests, finding
    > P> this misspelling can be a challenge.
    >
    > and ???
    >
    > This does not his problem where `M.allocate' work but not 'Marshal.load()'


    Your sentence seems to be missing a necessary verb, so I'm not sure
    what you are trying to say.

    All I mean to say is that using const_missing to create new classes
    on-the-fly is a bad idea, imo. This is what the original poster was
    trying to do. It does not mean that changing Marhsal.load to have
    consistent lookup with the rest of Ruby is a bad idea.

    Paul
    Paul Brannan, Sep 8, 2004
    #6
  7. Stephen  Sykes

    ts Guest

    >>>>> "P" == Paul Brannan <> writes:

    P> On Wed, Sep 08, 2004 at 11:47:48PM +0900, ts wrote:

    >> This does not his problem where `M.allocate' work but not 'Marshal.load()'


    solve

    P> Your sentence seems to be missing a necessary verb, so I'm not sure
    P> what you are trying to say.

    P> All I mean to say is that using const_missing to create new classes
    P> on-the-fly is a bad idea, imo. This is what the original poster was
    P> trying to do. It does not mean that changing Marhsal.load to have
    P> consistent lookup with the rest of Ruby is a bad idea.

    In this case, I'm agree with you :)


    Guy Decoux
    ts, Sep 8, 2004
    #7
  8. Available to work at a basic rate

    Dear Rubists

    I am looking for some paid work of any kind, perhaps using ruby.

    I need to work from home because I have a commitment to care for and
    be available to my friend who is sick.

    Would anyone be interested in employing me?

    If the work can fit with my caring commitments then I will work for
    basic rate just for the flexibility and the opportunity.

    many thanks in advance

    Keith Hodges
    Keith P Hodges, Sep 8, 2004
    #8
    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. jason
    Replies:
    2
    Views:
    1,785
    jason
    Jan 10, 2006
  2. Rakesh Kumar
    Replies:
    5
    Views:
    680
    James Kanze
    Dec 21, 2007
  3. Replies:
    10
    Views:
    530
    Aaron Watters
    Jun 18, 2008
  4. nielinjie
    Replies:
    4
    Views:
    296
  5. Michael Davis

    Ruby 1.8 and Marshal.load/Marshal.dump

    Michael Davis, Oct 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    167
    Michael Davis
    Oct 10, 2003
Loading...

Share This Page