From: Ruhe Date: 2008-04-02T19:35:04+09:00 Subject: Re: MetaMonster On 1 Сав, 12:42, Robert Klemme wrote: > > > PS > > I understand that this is a bad way to build class, > > I did it to get new skills in metaprogramming, not for real-world use. > > Here are my 0.02EUR: > > Make create and others class instance methods instead of instance > methods. You do not need the instance for anything else than creating > classes so it's obsolete. At least it should not be visible from the > outside. > > You could save some typing by integrating this with Struct, i.e. let > Struct do all that it does already and only add mathematic methods, > like > > class Vector > def self.new(*syms) > cl = Struct.new(*syms) > # add math instance methods to cl > cl > end > end > > I'd probably also generate the method code and class_eval it vs. using > instance_variable_set and *get because that code is likely faster and > you do not need a closure, e.g. > > %w{+ -}.each do |op| > cl.class_eval = %Q{ > def #{op}(other) > self.class.new(#{syms.map {|s| "self.#{s} #{op} > other.#{s}"}.join(', ')}) > end > } > end > > Ah, and why the many empty lines? Or is this just a pasting artifact? > > Kind regards > > robert > > -- > use.inject do |as, often| as.you_can - without end Thank you, Robert, for response. Your 0.02EUR are 100EUR for me :)