From: "ara.t.howard" Date: 2008-09-05T01:21:12+09:00 Subject: Re: Request for Block local methods, and Proc syntax On Sep 4, 2008, at 8:34 AM, Patrick Li wrote: > > but you still can't do something this: > > html do > @code = retrieveCode > end > puts @code <-- doesn't work, because this is a different @code than > the > one you assigned to. > > sure you can: require 'rubygems' require 'tagz' include Tagz.globally content = html_{ body_{ @code = 42 } } puts @code puts content cfp:~ > ruby a.rb 42 42 you just have to be clever about when methods are in effect - check out the tagz source for an example of this. alternatively you can skin it this way pushing and popping methods on the object in question: cfp:~ > cat a.rb content = html do body do img do @code = 42 end end end y 'content' => content, '@code' => @code BEGIN { module Html Methods = lambda do def body &block "#{ block.call }" end def img &block "#{ block.call }" end end def Methods.new Module.new do module_eval &Methods def self.delete!() instance_methods.each{|m| remove_method m} end end end def Methods.push object singleton_class_for(object) do include(( @html_methods = Methods.new )) unless @html_methods end end def Methods.pop object singleton_class_for(object) do @html_methods.delete! if @html_methods end end def Methods.singleton_class_for object, &block singleton_class = class << object self end block ? singleton_class.module_eval(&block) : singleton_class end end def html &block Html::Methods.push self content = instance_eval(&block) "#{ content }" ensure Html::Methods.pop self end require 'yaml' } cfp:~ > ruby a.rb --- "@code": 42 content: 42 you'll have to try harder than that before proposing changes to the language ;-) cheers a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama