extending object with another object

Discussion in 'Ruby' started by Ittay Dror, Oct 22, 2008.

  1. Ittay Dror

    Ittay Dror Guest

    Hi,


    How is it possible to extend an object using another object?


    I know I can iterate the public methods of one and define them (or maybe
    clone, and bind) in the other, but I'm looking for something that will
    work more closely to how 'extend' works.


    Thanks,

    Ittay

    --
    --
    Ittay Dror <>
     
    Ittay Dror, Oct 22, 2008
    #1
    1. Advertising

  2. Ittay Dror wrote:
    > How is it possible to extend an object using another object?


    It depends exactly on what you're trying to achieve.

    You have two separate objects, both with their own sets of methods *and*
    their own sets of instance variables. In what way do you want to
    "extend" the first with the second? What happens to instance variables
    belonging to the second object?

    Now, if you simply want a call to method X on object A to be forwarded
    to object B (in which context it would use object B's instance
    variables), then look at delegate.rb, or you can synthesise this pattern
    yourself using method_missing

    class A
    def hello
    puts "hello"
    end
    end

    class B
    def bye
    puts "bye"
    end
    end

    a = A.new
    b = B.new

    # This is a rough implementation of SimpleDelegator
    def a.other=(x)
    @other=x
    end
    def a.method_missing(*args)
    @other.send(*args)
    end

    a.other = b
    a.hello
    a.bye

    The important thing here is that when methods are delegated to the other
    object, they will be in the context of that object and hence have access
    to that object's instance variables. Put simply, that object will
    "work".

    In cases where the methods by themselves make sense, then generally
    you'd stick those methods into a Module so that both a and b could make
    use of them.

    class A
    include MyStuff
    end
    ...
    b = B.new
    b.extend MyStuff

    But clearly you know about this already.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 22, 2008
    #2
    1. Advertising

  3. Ittay Dror

    Ittay Dror Guest

    [Note: parts of this message were removed to make it a legal post.]



    Brian Candler wrote:

    > Ittay Dror wrote:
    >
    >> How is it possible to extend an object using another object?
    >>

    >
    > It depends exactly on what you're trying to achieve.
    >
    > You have two separate objects, both with their own sets of methods *and*
    > their own sets of instance variables. In what way do you want to
    > "extend" the first with the second? What happens to instance variables
    > belonging to the second object?
    >

    I want just to get the methods defined in the second object, something like
    object.extend(as_module(other_object))

    where 'as_module' is some method that returns a module representing the
    class hierarchy (with singletons) of the other object.

    class A
    def initialize(name)
    @name = name
    end
    def hi
    puts @name
    end
    end

    class B
    def initialize(name)
    @name = name
    end
    end

    a = A.new('a')
    b = B.new('b')
    b.object_extend(a)
    b.hi #=> 'b'

    Ittay


    > Now, if you simply want a call to method X on object A to be forwarded
    > to object B (in which context it would use object B's instance
    > variables), then look at delegate.rb, or you can synthesise this pattern
    > yourself using method_missing
    >
    > class A
    > def hello
    > puts "hello"
    > end
    > end
    >
    > class B
    > def bye
    > puts "bye"
    > end
    > end
    >
    > a = A.new
    > b = B.new
    >
    > # This is a rough implementation of SimpleDelegator
    > def a.other=(x)
    > @other=x
    > end
    > def a.method_missing(*args)
    > @other.send(*args)
    > end
    >
    > a.other = b
    > a.hello
    > a.bye
    >
    > The important thing here is that when methods are delegated to the other
    > object, they will be in the context of that object and hence have access
    > to that object's instance variables. Put simply, that object will
    > "work".
    >
    > In cases where the methods by themselves make sense, then generally
    > you'd stick those methods into a Module so that both a and b could make
    > use of them.
    >
    > class A
    > include MyStuff
    > end
    > ...
    > b = B.new
    > b.extend MyStuff
    >
    > But clearly you know about this already.
    >


    --
    Ittay Dror <>
    Tikal <http://www.tikalk.com>
    Tikal Project <http://tikal.sourceforge.net>


    --
    --
    Ittay Dror <>
     
    Ittay Dror, Oct 22, 2008
    #3
  4. Ittay Dror wrote:
    > Brian Candler wrote:
    >
    >> belonging to the second object?
    >>

    > I want just to get the methods defined in the second object, something
    > like
    > object.extend(as_module(other_object))
    >
    > where 'as_module' is some method that returns a module representing the
    > class hierarchy (with singletons) of the other object.


    But I still don't know what the *purpose* of this is, i.e. what actual
    problem you're trying to solve, and without that I can't propose a
    better solution.

    What you're asking for is not especially easy to do. I guess you could
    iterate over the methods of B, obtain UnboundMethod objects, and bind
    them to A. But I've never come across any situation where I've needed
    (or wanted) to do this.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Oct 22, 2008
    #4
  5. Ittay Dror

    Trans Guest

    On Oct 22, 3:52=A0am, Ittay Dror <> wrote:
    > Hi,
    >
    > How is it possible to extend an object using another object?
    >
    > I know I can iterate the public methods of one and define them (or maybe
    > clone, and bind) in the other, but I'm looking for something that will
    > work more closely to how 'extend' works.


    You have to use delegation. Look up delegate.rb.

    T.
     
    Trans, Oct 22, 2008
    #5
    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. mark d.
    Replies:
    0
    Views:
    877
    mark d.
    Feb 6, 2006
  2. Replies:
    0
    Views:
    315
  3. Andreas M.
    Replies:
    1
    Views:
    459
    Joseph Kesselman
    Oct 4, 2007
  4. Rakesh
    Replies:
    10
    Views:
    12,264
    Mike Schilling
    Apr 8, 2008
  5. Bryan Richardson
    Replies:
    2
    Views:
    120
Loading...

Share This Page