Created
August 21, 2011 15:54
-
-
Save devboy/1160781 to your computer and use it in GitHub Desktop.
Does anyone know why block_given? always returns false?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Configuration | |
| def create_method(name, &block) | |
| self.class.send(:define_method, name, &block ) | |
| end | |
| def add_configuration(name) | |
| create_method "#{name.to_s}=" do |val| | |
| instance_variable_set("@#{name}",val) | |
| end | |
| create_method name do | |
| puts "method:#{name}" | |
| puts "hasblock:#{block_given?}" | |
| yield(instance_variable_get("@#{name}")) if block_given? | |
| instance_variable_get("@#{name}") | |
| end | |
| end | |
| def test=val | |
| @val = val | |
| end | |
| def test | |
| yield(@test) if block_given? | |
| @test | |
| end | |
| end | |
| conf = Configuration.new | |
| conf.test = {:a => 1, :b => 2} | |
| conf.test do |p| | |
| puts "yield test" | |
| end | |
| conf.add_configuration :foo | |
| conf.foo = {:a => 1, :b => 2} | |
| conf.foo do |f| | |
| puts "yield foo" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having the same issue with ruby 1.8.7