Forum: Ruby on Rails class_eval in ROR's attribure_accessor.rb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
21fea609369395569cead719dce589e0?d=identicon&s=25 mengjiang Liu (limux)
on 2006-05-05 08:12
hi,
here is a snippet code in ROR's attribure_accessor.rb. the usage of
class_eval is different  with "class_eval(string, <, file, <line>>)"
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-05 17:21
(Received via mailing list)
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, <line>>)"
> 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
This topic is locked and can not be replied to.