From: dblack@... Date: 2003-05-08T23:03:17+09:00 Subject: Re: [Q] Block passing: obj.new(){block} Hi -- On Thu, 8 May 2003, Peter Schrammel wrote: > Hi, > > How do I pass a block to the initialize method that calls some methods > of the new object? Here is what I want to get: > > class Test > def initialize(foo) > ... > end > def set=(myval) > @set=myval > end > def set > @set > end > end > > t=Test.new("foo"){set=89} > puts t.set > =>89 You're going to need to state the receiver explicitly for that call to set (because otherwise Ruby will favor the interpretation that it's a local variable assignment). So, you might end up with something like: class Test attr_accessor :set # save some typing :-) def initialize(foo) yield(self) end end t=Test.new("foo") {|o| o.set=89} puts t.set Are you sure you wouldn't rather do: t = Test.new("foo") t.set(89) ? :-) David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav