Passing the class as an argument

Discussion in 'Ruby' started by Damphyr, Nov 20, 2005.

  1. Damphyr

    Damphyr Guest

    I'm too lazy to figure it out (and a bit hangover).
    I have several collections of objects (read over an OLE API) that I
    transform to arrays of ruby objects.
    Against the DRY religion I ended up with several methods similar to the
    following:

    def get_processors col
    ar=Array.new
    count=col.Count
    1.upto(count){|i|
    ar<<Processor.new(col.GetAt(i))
    }
    return ar
    end

    Now, this begs for the working equivalent of

    def get_collection col, _class
    ar=Array.new
    count=col.Count
    1.upto(count){|i|
    ar<<_class.new(col.GetAt(i))
    }
    return ar
    end

    How do I do it?
    (I do know how do it - I think . It's just that the hangover is not
    helping the search functions of my brain :) )

    i.e. this would do what I want
    def get_collection col, _class
    ar=Array.new
    count=col.Count
    1.upto(count){|i|
    ar<<instance_eval(_class+".new(col.GetAt(i))")
    }
    return ar
    end

    ...but, is there another way?
    Cheers,
    V.-
    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
     
    Damphyr, Nov 20, 2005
    #1
    1. Advertising

  2. Damphyr

    Damphyr Guest

    Damphyr wrote:
    > I'm too lazy to figure it out (and a bit hangover).
    > I have several collections of objects (read over an OLE API) that I
    > transform to arrays of ruby objects.
    > Against the DRY religion I ended up with several methods similar to the
    > following:
    >
    > def get_processors col
    > ar=Array.new
    > count=col.Count
    > 1.upto(count){|i|
    > ar<<Processor.new(col.GetAt(i))
    > }
    > return ar
    > end
    >
    > Now, this begs for the working equivalent of
    >
    > def get_collection col, _class
    > ar=Array.new
    > count=col.Count
    > 1.upto(count){|i|
    > ar<<_class.new(col.GetAt(i))
    > }
    > return ar
    > end
    >
    > How do I do it?
    > (I do know how do it - I think . It's just that the hangover is not
    > helping the search functions of my brain :) )
    >
    > i.e. this would do what I want
    > def get_collection col, _class
    > ar=Array.new
    > count=col.Count
    > 1.upto(count){|i|
    > ar<<instance_eval(_class+".new(col.GetAt(i))")
    > }
    > return ar
    > end
    >
    > ...but, is there another way?

    And to clarify (it's just impossible to concentrate today :( ).
    Ideally I would like to call

    get_collection(ole_collection,Processor)

    which looks nicer than

    get_collection(ole_collection,"Processor")
    Cheers,
    V.-
    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
     
    Damphyr, Nov 20, 2005
    #2
    1. Advertising

  3. Hi --

    On Mon, 21 Nov 2005, Damphyr wrote:

    > I'm too lazy to figure it out (and a bit hangover).
    > I have several collections of objects (read over an OLE API) that I transform
    > to arrays of ruby objects.
    > Against the DRY religion I ended up with several methods similar to the
    > following:
    >
    > def get_processors col
    > ar=Array.new
    > count=col.Count


    Are you sure you've got Count as a method name? It's possible, but
    fairly bizarre.

    > 1.upto(count){|i|
    > ar<<Processor.new(col.GetAt(i))
    > }
    > return ar
    > end
    >
    > Now, this begs for the working equivalent of
    >
    > def get_collection col, _class
    > ar=Array.new
    > count=col.Count
    > 1.upto(count){|i|
    > ar<<_class.new(col.GetAt(i))
    > }
    > return ar
    > end


    I guess col is 1-originating, so you'd have to adapt this, but here it
    is with a normal array:

    def get_collection(col, klass)
    col.map {|e| klass.new(e) }
    end

    If klass is a string, you'd have to do a const_get on it first:

    klass = Object.const_get(klass)

    or the traditional deep version, if it's a nested constant:

    class Module
    def deep_const_get(c)
    c.split("::").inject(self) {|acc,const| acc.const_get(const) }
    end
    end

    or some variant thereof.


    David

    --
    David A. Black
     
    David A. Black, Nov 20, 2005
    #3
  4. Damphyr

    Damphyr Guest

    David A. Black wrote:
    > Hi --
    >
    > On Mon, 21 Nov 2005, Damphyr wrote:
    >
    >> I'm too lazy to figure it out (and a bit hangover). I have several
    >> collections of objects (read over an OLE API) that I transform to
    >> arrays of ruby objects. Against the DRY religion I ended up with
    >> several methods similar to the following:
    >>
    >> def get_processors col ar=Array.new count=col.Count

    >
    >
    > Are you sure you've got Count as a method name? It's possible, but
    > fairly bizarre.

    Yeap, we're not talking Ruby objects here. It's an OLE API and it uses
    some pretty un-ruby names.
    So col is an an OLE object missing such nice conveniences like each and
    collect and map.
    Well, assumptions can kill you. The following works as is, so all this
    was a post with no reason to exist whatsoever.

    class P
    attr_reader :c
    def initialize c
    @c=c
    end
    def to_s
    return @c
    end
    end
    def get_collection col,_class
    ar=Array.new
    col.each{|e|
    ar<<_class.new(e)
    }
    return ar
    end

    col=["w","o","w"]
    ps=get_collection col,P
    puts ps
    >

    w
    o
    w

    If only everything turned out so easy...
    V.-
    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
     
    Damphyr, Nov 21, 2005
    #4
    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. Frank Schmitt
    Replies:
    0
    Views:
    495
    Frank Schmitt
    Oct 14, 2003
  2. Ahti Legonkov
    Replies:
    0
    Views:
    508
    Ahti Legonkov
    Dec 12, 2003
  3. Trans
    Replies:
    3
    Views:
    198
    Robert Klemme
    Jan 26, 2005
  4. ±è ÁØ¿µ
    Replies:
    7
    Views:
    256
    Einar Magnús Boson
    Nov 27, 2008
  5. User1014
    Replies:
    1
    Views:
    187
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page