Hi --

On Sat, 17 Feb 2007, Clifford Heath wrote:

> Austin Ziegler wrote:
>> Ruby 1.9 accepts blocks as parameters to blocks.
>
> I can't immediately see how that solves this problem...?
> I need to create a string to eval so I can fabricate the
> method names, and I want a block to be available within
> that eval. Can you show a quick example?

I guess my first take on it would be this:

   class Module
     def validated_attr(*names, &block)
       names.each do |name|
         define_method(name) { instance_variable_get("@#{name}") }
         define_method("#{name}=") {|val|
           unless block.call(val)
             raise "Invalid assignment of #{val.inspect} to #{name}."
           end
         }
       end
     end
   end

(using define_method rather than def so that the local variable block
will still be in scope).


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)