Module: AttrReadonly

Included in:
Module
Defined in:
lib/attr_readonly.rb,
lib/attr_readonly/version.rb

Overview

Note:

this module include a method to be mix in Module (Class)

module Methodic

See Also:

Author:

Version:

  • 1.0.0

Constant Summary collapse

VERSION =

current version of attr_readonly

"1.0.0"

Instance Method Summary collapse

Instance Method Details

#attr_readonly(*syms) ⇒ Array

pretty accessor for specifying mandatories options

Examples:

usage

require 'attr_readonly' 
class Test
  attr_readonly :foo
  def initialize(foo: '')
    @foo = :foo
  end
end

Parameters:

  • syms (*Symbol)

    a list of symbols defining accessors to add

Returns:

  • (Array)

    Array of symbols methods name (accessors) created



36
37
38
39
40
41
42
# File 'lib/attr_readonly.rb', line 36

def attr_readonly *syms
  syms.each do |method|
    define_method(method){
      return self.instance_variable_get("@#{method.to_s}").dup.freeze 
    }
  end
end