calling binding() within a module method (for mixin)?????

Discussion in 'Ruby' started by Ben, Nov 7, 2004.

  1. Ben

    Ben Guest

    I've been trying to write a method that can dynamically mixin a module
    to a class. I tried the following code:
    #=========================================
    module MixinUtils
    def MixinUtils.includeMixin(className,mixinName,vars)
    evalStr= <<-END
    class #{className.to_s}
    include #{mixinName.to_s}
    end
    END
    eval(evalStr, vars)
    end
    end

    module Foo
    def printFoo
    puts "foo"
    end
    end

    class Array
    def someMethod
    self.each do |item|
    MixinUtils.includeMixin(item.class.to_s,"Foo",binding)
    item.printFoo
    end
    end
    end

    [1,2,3].someMethod
    #======================================

    But running this gives

    question.rb:22:in `something': undefined method `printFoo' for
    1:Fixnum (NoMethodError)
    from question.rb:20:in `each'
    from question.rb:20:in `something'
    from question.rb:31

    But if I take out the '[1,nil,true].something' and append

    a=1
    MixinUtils.includeMixin("Fixnum","Foo",binding)
    a.printFoo

    it produces

    foo

    as expected. Why the difference? Why does calling 'binding' inside a
    block produce different results? Thanks in advance...

    Ben
     
    Ben, Nov 7, 2004
    #1
    1. Advertising

  2. Ben

    ts Guest

    >>>>> "B" == Ben <> writes:

    B> eval(evalStr, vars)

    eval is evil

    B> end

    write it like this

    B> MixinUtils.includeMixin(item.class.to_s,"Foo",binding)
    B> item.printFoo

    Array::Fixnum.new.printFoo

    B> end



    Guy Decoux
     
    ts, Nov 7, 2004
    #2
    1. Advertising

  3. Ben

    Pit Capitain Guest

    Ben schrieb:
    > I've been trying to write a method that can dynamically mixin a module
    > to a class. I tried the following code:
    > (... code using eval ...)


    Hi Ben,

    in order to avoid eval as Guy suggested, you could try

    module MixinUtils
    def self.includeMixin(klass, mixin)
    klass.send:)include, mixin) # include is a private method
    end
    end

    module Foo
    def printFoo
    puts "foo"
    end
    end

    class Array
    def someMethod
    self.each do |item|
    MixinUtils.includeMixin(item.class, Foo)
    item.printFoo
    end
    end
    end

    Calling

    [1,nil,true].someMethod

    results in

    foo
    foo
    foo

    as you expected.

    Regards,
    Pit
     
    Pit Capitain, Nov 7, 2004
    #3
  4. Ben

    Ben Guest

    Pit Capitain <> wrote in message news:<>...
    > Ben schrieb:
    > > I've been trying to write a method that can dynamically mixin a module
    > > to a class. I tried the following code:
    > > (... code using eval ...)

    >
    > Hi Ben,
    >
    > in order to avoid eval as Guy suggested, you could try
    >
    > module MixinUtils
    > def self.includeMixin(klass, mixin)
    > klass.send:)include, mixin) # include is a private method
    > end
    > end
    >
    > module Foo
    > def printFoo
    > puts "foo"
    > end
    > end
    >
    > class Array
    > def someMethod
    > self.each do |item|
    > MixinUtils.includeMixin(item.class, Foo)
    > item.printFoo
    > end
    > end
    > end
    >
    > Calling
    >
    > [1,nil,true].someMethod
    >
    > results in
    >
    > foo
    > foo
    > foo
    >
    > as you expected.
    >
    > Regards,
    > Pit


    I'll try this out. Thanks!
     
    Ben, Nov 10, 2004
    #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. Mac
    Replies:
    1
    Views:
    594
  2. Farrel Lifson

    Mixin a module method as a class method

    Farrel Lifson, Oct 18, 2006, in forum: Ruby
    Replies:
    9
    Views:
    126
    Trans
    Oct 19, 2006
  3. Sijo Kg

    class, Module and Mixin

    Sijo Kg, Sep 24, 2009, in forum: Ruby
    Replies:
    1
    Views:
    123
    Robert Klemme
    Sep 24, 2009
  4. John Lane
    Replies:
    6
    Views:
    188
    John Lane
    Feb 9, 2010
  5. pp
    Replies:
    1
    Views:
    115
    Andrew Wagner
    Mar 3, 2011
Loading...

Share This Page