Class_eval in ROR's attribure_accessor.rb


#1

hi,
here is a snippet code in ROR’s attribure_accessor.rb. the usage of
class_eval is different with “class_eval(string, <, file, >)”
somewhat,
i don’t understand this usage, who can explain it more detail to me?

def cattr_writer(*syms)
syms.flatten.each do |sym|
class_eval(<<-EOS, FILE, LINE)
unless defined? @@#{sym}
@@#{sym} = nil
end

    def self.#{sym}=(obj)
      @@#{sym} = obj
    end

    def #{sym}=(obj)
      @@#{sym} = obj
    end
  EOS
end

end

can it write as fellow:

def cattr_writer(*syms)
syms.flatten.each do |sym|
class_eval(<<-EOS
unless defined? @@#{sym}
@@#{sym} = nil
end

    def self.#{sym}=(obj)
      @@#{sym} = obj
    end

    def #{sym}=(obj)
      @@#{sym} = obj
    end
  EOS, __FILE__, __LINE__)
end

end


#2

On May 4, 2006, at 11:12 PM, mengjiang liu wrote:

here is a snippet code in ROR’s attribure_accessor.rb. the usage of
class_eval is different with “class_eval(string, <, file, >)”
somewhat,
i don’t understand this usage, who can explain it more detail to me?

From the Ruby documentation for Module#class_eval: “The optional
filename and lineno parameters set the text for error messages.”

$ ri class_eval

jeremy