From: Robert Klemme Date: 2004-08-24T16:35:43+09:00 Subject: Re: *with* block? "Jamis Buck" schrieb im Newsbeitrag news:412A9A3D.2050808@email.byu.edu... > Zach Dennis wrote: > > I am looking for a structure like: > > > > my_obj = boxWidget.new > > with my_obj do > > x = 0 > > y = 10 > > width = 100 > > height = 100 > > end > > > > instead of having to say > > > > my_obj = boxWidget.new > > my_obj.x = 0 > > my_obj.y = 10 > > my_obj.width = 100 > > my_obj.height = 100 > > > > > > > > Can you do this already? If not....anyone support an RCR for it besides me? > > No such beast "out of the box," but there have been various discussions > on the list about this feature. I believe one implementation presented was: > > class Object > def with(obj, &block) > obj.instance_eval &block > end > end > > with "hello" do > puts length > puts reverse > end Doesn't work for member init since all statements of the form "x=y" create local variables in the block. I'd prefer something like: class Object def member_init(map) map.each do |name, val| send "#{name}=", val end self end end ost = OpenStruct.new.member_init( :name => "foo", :age => 10 ) Kind regards robert