Class: Tesco::Class

Inherits:
Object
  • Object
show all
Defined in:
lib/tesco.rb

Overview

Unobtrusive modifications to the Class class.

Instance Method Summary collapse

Instance Method Details

#attr_reader(*params, &block) ⇒ Object

Pass a block to attr_reader and the block will be evaluated in the context of the class instance before the instance variable is returned.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tesco.rb', line 24

def attr_reader(*params,&block)
  if block_given?
    params.each do |sym|
      # Create the reader method
      define_method(sym) do
        # Force the block to execute before we…
       self.instance_eval(&block)
        # … return the instance variable
        self.instance_variable_get("@#{sym}")
      end
    end
  else # Keep the original function of attr_reader
    params.each do |sym|
      attr sym
    end
  end
end