Hi --
On Sun, 18 Oct 2009, Bill Devaul wrote:
>
> Thanks to your help David I now have:
>
> # lib/normalizer.rb
> module Normalizer
>
> BLANKS = [0, "0", "", " "]
>
> def self.included(base)
> base.class_eval do
> def self.normalize(*syms)
> syms.each do |attr|
> define_method("#{attr}=") do |value|
> write_attribute(attr, BLANKS.include?(value) ? nil : value)
> end
> end
> end
> end
> end
>
end
>
> (I struggled with the above until I read that class_eval is needed to
> make the inclusion happen. I wonder if a change to class_eval to yield
> the included module would feel more natural.)
You should be able to do:
def self.included(base)
def base.normalize(*syms)
...
end
end
I might be inclined to do:
module Normalizer
def normalize(*syms)
...
end
end
class Whatever
extend Normalizer
...
end
i.e., extend the class object directly rather than grabbing it
indirectly and operating on it. It seems a little more streamlined.
David
--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---