dsl - Making things available only inside Ruby blocks -


Is there a way to make methods and tasks available only in blocks? What am I trying to do:

  block some_all_one_in_block_is_this_here? Okay_cool end  

but is_this_here? , okay_cool , etc. are accessible only within that block, do not exclude it. Any ideas got?

Pass an object with the methods that you want to be available in the block argument. This is a pattern that is widely used in Ruby, such as or

  some_block Talk Cheese.available_only_in_blocks thing.Is_this_Yes? Thing.okay_cool end  

Note that you can get close to the thing you used to use, but this is usually a bad idea because it can have a lot of surprising results. Are there.

  Square Foo def Bar "Hello" end end def with_foo & amp; Block Foo.new.instance_exec and block end with_foo {bar} # = & gt; "Hello" bar = 10 with_foo {times} # = & gt; 10 with_foo {self.bar} # = & gt; "Hello"  

However, if you pass an argument in, you always know what you will mention:

  def with_foo yield Foo .new end with_foo {| x | x.bar} # = & gt; "hello" bar = 10x = 20 with_foo {| x | x.bar} # = & gt; "hello"  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -