Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > calling binding() within a module method (for mixin)?????

Reply
Thread Tools

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

 
 
Ben
Guest
Posts: n/a
 
      11-07-2004
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",bind ing)
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
 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      11-07-2004
>>>>> "B" == Ben <(E-Mail Removed)> writes:

B> eval(evalStr, vars)

eval is evil

B> end

write it like this

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

Array::Fixnum.new.printFoo

B> end



Guy Decoux


 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      11-07-2004
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


 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      11-10-2004
Pit Capitain <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling Ruby module methods from a nested class within mgerstenblatt@gmail.com Ruby 1 03-16-2011 02:36 PM
Calling virtual method within the constructor Fernando Gómez C++ 7 06-08-2008 05:49 PM
ASP.Net 1.1-- calling Javascript from within a method? Steve Hershoff ASP .Net 4 10-11-2007 08:49 PM
Calling Async object within a webservice method Paul Hadfield ASP .Net 0 09-17-2007 12:00 PM
Calling a method within qq'ed string jab3 Perl Misc 2 02-22-2006 07:20 PM



Advertisments