Class: Trema::StatsHelper

Inherits:
Object
  • Object
show all
Defined in:
ruby/trema/stats-helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields, options = {}) ⇒ void

Invoked by each StatsReply subclass to assign their instance attributes to a value.

Parameters:

  • fields (Array)

    an array of attribute names.

  • options (Hash)

    key/value pairs of attributes to match against the fields to set.



36
37
38
39
40
# File 'ruby/trema/stats-helper.rb', line 36

def initialize fields, options
  fields.each do |field|
    instance_variable_set( "@#{field}", options[field.intern] )
  end
end

Instance Method Details

#to_sString

Returns an alphabetically sorted text of attribute name/value pairs.

Returns:

  • (String)

    an alphabetically sorted text of attribute name/value pairs.



47
48
49
50
51
52
53
54
# File 'ruby/trema/stats-helper.rb', line 47

def to_s
  str = super.to_s + "\n"
  instance_variables.sort.each do |var|
    str += "#{var[1..var.length]}: #{instance_variable_get( var ).to_s}\n"
  end
  # remove the last newline character
  str[0..-2]
end