On Wed, 2006-02-22 at 03:23 +0900, Adam Shelly wrote:

> I notice that most people extended Module, while I extended Object. 
> Is there a reason to prefer one over the other?
> 

Well, you're extending Object with a method that relies on methods
defined in Module (class_eval), and your method is available in places
where it really didn't ought to be. 

> #-------Knowledge.rb
> #
> class Object
>   def attribute *names, &block
>     names.each do |name|
>       attribute *name.map{|k,v|[k,v]} and next if name.kind_of? Hash
>       name,v = name
>       class_eval "def #{name};"+
>             "@#{name}=(defined?(@#{name}) ? @#{name} : #{name}_init_); end"
>       class_eval "def #{name}?; !(self.#{name}.nil?); end"
>       class_eval "def #{name}=(v); @#{name}=v; end"
>       private; define_method("#{name}_init_", block || proc {v})
>     end
>   end
> end
> 
attribute :oops

-:7:in `attribute': undefined method `class_eval' for main:Object
(NoMethodError)
        from -:3:in `attribute'
        from -:15

By defining an instance method on Module (from which Class inherits) you
ensure that a) class_eval is there, and b) attribute can be called only
in a module/class definition context.

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk