Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/core-extensions/class.rb

Overview

:title: Class: Class

Instance Method Summary collapse

Instance Method Details

#attr_property(*accessors) ⇒ Object

Syntactic sugar for providing instance level attributes. Similar to attr_accessor except the value of property is set without requiring “=” syntax, e.g.

foo.propname 5        (rather than <tt>foo.attrname=5</tt>)

foo.propname            #=> 5

In other words, setting is performed by specifing an argument, getting is performed using the same method call without an argument.



19
20
21
22
23
24
25
26
# File 'lib/core-extensions/class.rb', line 19

def attr_property(*accessors)
  accessors.each do |m|
    define_method(m) do |*val|
      instance_variable_set("@#{m}",val.first) unless val.empty? #Array Hack to avoid warning
      instance_variable_get("@#{m}")
    end
  end
end