Class: VectorNumber::SpecialUnit

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

Overview

Class for representing special (unique) units.

There usually isn’t much point in using these, aside from NUMERIC_UNITS. However, they can be helpful to denote units that aren’t equal to any other object.

VectorNumber#to_s (and inspect) will use #to_s text instead of #inspect text for these units by default, without a multiplication operator. However, consider using customization of VectorNumber#to_s instead if this is what you want.

Since:

  • 0.6.0

Instance Method Summary collapse

Constructor Details

#initialize(unit, text = unit.to_s) ⇒ SpecialUnit

Returns a new, unique unit, not equal to any other unit.

Parameters:

  • unit (#to_s)

    name for #inspect

  • text (String) (defaults to: unit.to_s)

    text for #to_s

Since:

  • 0.6.0



19
20
21
22
# File 'lib/vector_number/special_unit.rb', line 19

def initialize(unit, text = unit.to_s)
  @unit = unit
  @text = text
end

Instance Method Details

#inspectString

Get string representation of the unit for debugging.

Returns:

  • (String)

Since:

  • 0.6.0



44
45
46
# File 'lib/vector_number/special_unit.rb', line 44

def inspect
  "unit/#{@unit}"
end

#pretty_print(pp) ⇒ void

This method returns an undefined value.

Support for PP, outputs the same text as #inspect.

Parameters:

  • pp (PP)

Since:

  • 0.7.0



37
38
39
# File 'lib/vector_number/special_unit.rb', line 37

def pretty_print(pp)
  pp.text(inspect)
end

#to_sString

Get predefined string representation of the unit.

Returns:

  • (String)

Since:

  • 0.6.0



27
28
29
# File 'lib/vector_number/special_unit.rb', line 27

def to_s
  @text
end