Skip to content

Instantly share code, notes, and snippets.

@devboy
Created August 21, 2011 15:54
Show Gist options
  • Select an option

  • Save devboy/1160781 to your computer and use it in GitHub Desktop.

Select an option

Save devboy/1160781 to your computer and use it in GitHub Desktop.
Does anyone know why block_given? always returns false?
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
@DNNX
Copy link

DNNX commented Dec 14, 2012

I'm having the same issue with ruby 1.8.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment