Module: Coach4rb::Mixin::AutoConstructor::InstanceMethods

Defined in:
lib/coach4rb/mixin/auto_constructor.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Provides hash-like access on the properties of this object.

Parameters:

  • key (Symbol)

    used to access the corresponding property.



49
50
51
52
53
54
55
# File 'lib/coach4rb/mixin/auto_constructor.rb', line 49

def [](key)
  if value = instance_variable_get("@#{key}")
    value
  else
    send key rescue nil
  end
end

#[]=(key, value) ⇒ Object

Provides hash-like access on the properties of this object.

Parameters:

  • key (Symbol)

    used to access the corresponding property.



61
62
63
# File 'lib/coach4rb/mixin/auto_constructor.rb', line 61

def []=(key,value)
  instance_variable_set("@#{key}",value)
end

#initialize(a_hash = {}) ⇒ Object

Creates a object using the keys and values given in the hash.

Parameters:

Parameters:

  • a_hash (Hash) (defaults to: {})

    declaring the instance variables. The keys define the variable names and the corresponding values the initial values.

Returns:

  • (Object)

    the object specified by the given hash.



38
39
40
41
42
43
# File 'lib/coach4rb/mixin/auto_constructor.rb', line 38

def initialize(a_hash={})
  raise 'Param a_hash is not a hash!' unless a_hash.is_a? Hash
  a_hash.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end