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
can I tighten this up with block or eval?
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="matt neuburg, post: 4634061"] I have here a class called ClassMaker whose greeter method returns an instance of anonymous class containing a hello method: class ClassMaker def initialize(s); @howdy = s; end def greeter c = Class.new do def initialize(s); @hello = s; end def hello puts @hello # this is the nub of the matter end end c.new(@howdy) end end ClassMaker.new("gday").greeter.hello I would like the anonymous class's hello method to access whatever the value of my ClassMaker instance's @howdy ivar may be at the moment greeter is called. I am presently doing this, as you can see, by giving the anonymous class an initialize method and passing the @howdy ivar's value into an ivar of the anonymous class's instance. This works but seems heavy-handed. I feel I should be able to do it without the anonymous class having any initialize method at all. The whole situation has "closure" written all over it. So I tried to define the hello method to accomplish the same thing using evals and blocks and other tricky stuff, and became hopelessly confused. Could someone straighten me out? Thx - m. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
can I tighten this up with block or eval?
Top