method

cattr_reader

v2.2.1 - Show latest stable - Class: Class
cattr_reader(*syms)
public

No documentation available.

# File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 10
  def cattr_reader(*syms)
    syms.flatten.each do |sym|
      next if sym.is_a?(Hash)
      class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
    end
  end

2Notes

Description

zvoase · Jan 27, 20092 thanks

Similar to Ruby's built-in +attr_reader+, only it creates a class attribute reader method (as opposed to an instance attribute reader method).

Other Example

jackson_pires · Feb 2, 2013
##

class Exam cattr_reader :code, :description, :points, :instance_reader => false

@@code = "EXM"
@@description = "Sent Exam"
@@points = 1000

end

=== In this case it's possible to use

Exam.code # => EXM Exam.description # => Sent Exam Exam.points # => 1000