Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Eval and block problems
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Devin Mullins, post: 4485813"] Not sure if this'll help, but it's something I'd thrown together awhile=20 back that attempted to the same sorta thing: module Kernel def multidef(name,&blk) Multidef.new(self,name).instance_eval(&blk) nil end end class Multidef def initialize(receiver, name) @receiver =3D receiver name =3D name.to_s @name =3D name @receiver.send(:define_method,name) do |*a| send(name+a.size.to_s,*a) end end def args arity, &blk name =3D @name @receiver.send(:define_method,@name+arity.to_s,&blk) #do |*a| # if a.size !=3D arity then send(name+a.size.to_s,*a) # else blk.call(*a) end #can I get this to execute in self.binding? # end end end multidef :[]=3D do args 2 do |coord,val| x,y =3D convert(coord) self[x,y] =3D val end args 3 do |x,y,val| @thing[x][y] =3D val end end class Foo multidef :fun do args 1 do |a| puts "one: #{a}" end args 2 do |a,b| puts "two: #{a}, #{b}" fun a end args 3 do |a,b,c| puts "three: #{a}, #{b}, #{c}" fun a,b end end end f =3D Foo.new f.fun(1) f.fun('a','b','c') f.fun('hello',/nightmare/) __END__ Problems: 1. super doesn't work across different arities. 2. Can't take blocks. Devin [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Eval and block problems
Top