Class: Measurement::Unit
- Inherits:
-
Object
- Object
- Measurement::Unit
- Defined in:
- lib/ruby-measurement/unit.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#conversions ⇒ Object
readonly
Returns the value of attribute conversions.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #alias(*args) ⇒ Object
- #conversion(name) ⇒ Object
- #convert_to(name, &block) ⇒ Object
-
#initialize(name, &block) ⇒ Unit
constructor
A new instance of Unit.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Unit
Returns a new instance of Unit.
7 8 9 10 11 12 13 14 |
# File 'lib/ruby-measurement/unit.rb', line 7 def initialize(name, &block) @name = name.to_s @aliases = Set.new @conversions = {} self.alias(name) block.call(self) if block_given? end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
5 6 7 |
# File 'lib/ruby-measurement/unit.rb', line 5 def aliases @aliases end |
#conversions ⇒ Object (readonly)
Returns the value of attribute conversions.
5 6 7 |
# File 'lib/ruby-measurement/unit.rb', line 5 def conversions @conversions end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/ruby-measurement/unit.rb', line 5 def name @name end |
Instance Method Details
#==(obj) ⇒ Object
39 40 41 |
# File 'lib/ruby-measurement/unit.rb', line 39 def ==(obj) obj.kind_of?(self.class) && hash == obj.hash end |
#alias(*args) ⇒ Object
16 17 18 |
# File 'lib/ruby-measurement/unit.rb', line 16 def alias(*args) args.each { |arg| @aliases << arg.to_s } end |
#conversion(name) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/ruby-measurement/unit.rb', line 24 def conversion(name) Measurement.unit(name).aliases.each do |a| conversion = @conversions[a.to_s] return conversion if conversion end end |
#convert_to(name, &block) ⇒ Object
20 21 22 |
# File 'lib/ruby-measurement/unit.rb', line 20 def convert_to(name, &block) @conversions[name.to_s] = block end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/ruby-measurement/unit.rb', line 31 def inspect to_s end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/ruby-measurement/unit.rb', line 35 def to_s name end |