From: Gavin Kistner Date: 2005-05-26T23:01:04+09:00 Subject: Re: Invoking a Class block on an Instance --Apple-Mail-1--777588682 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On May 26, 2005, at 2:57 AM, Pit Capitain wrote: > Gavin Kistner schrieb: >> Unfortunately, I can't figure out how to get an instance of a >> class to invoke a block stored in its parent class, using the >> scope of the instance for the block. In JS this would be easy, >> but I don't yet grok how to have Ruby run a set of code on any >> arbitrary scope (while passing parameters to that code). > > executing a code block in the context of an object is normally done > via instance_eval, but I, too, couldn't find a way to pass > parameters to the block. > > One workaround is to create methods for the states. If you change > the following methods of FiniteStateMachine, it should work: Thanks for this; it solved my current problem. I would still like to know how (if it's possible) I can do the Ruby equivalent of this JS code: var addSome = function( addend ){ return this.val + addend } var obj1 = new Object; obj1.val = 10; var obj2 = new Object; obj2.val = 100; var sum1 = addSome.call( obj1, 10 ); //20 var sum2 = addSome.call( obj2, 10 ); //110 Is this the problem people are talking about when they say that Ruby doesn't have "first class" functions/methods? -- (-, /\ \/ / /\/ --Apple-Mail-1--777588682--