Class: NumericWithUnit

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/numeric_with_unit.rb,
lib/numeric_with_unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/util.rb,
lib/numeric_with_unit/util2.rb,
lib/numeric_with_unit/util2.rb,
lib/numeric_with_unit/unit_definition/base.rb,
lib/numeric_with_unit/unit_definition/common.rb

Defined Under Namespace

Modules: NumUtil Classes: DimensionError, Unit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, unit) ⇒ NumericWithUnit

Returns a new instance of NumericWithUnit.



10
11
12
13
# File 'lib/numeric_with_unit.rb', line 10

def initialize(value, unit)
  @value = value
  @unit = unit.is_a?(Unit) ? unit : Unit[unit]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/numeric_with_unit/util2.rb', line 9

def method_missing(name, *args)
  if args.empty?
    unit_str = name.to_s.gsub('_', '/')
    unit_chain_util(Unit[unit_str])
  else
    raise Unit::NoUnitError
  end
rescue Unit::NoUnitError
  super
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



8
9
10
# File 'lib/numeric_with_unit.rb', line 8

def unit
  @unit
end

#unit_chain=(value) ⇒ Object (writeonly)

Sets the attribute unit_chain

Parameters:

  • value

    the value to set the attribute unit_chain to.



20
21
22
# File 'lib/numeric_with_unit/util2.rb', line 20

def unit_chain=(value)
  @unit_chain = value
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/numeric_with_unit.rb', line 8

def value
  @value
end

Instance Method Details

#*(other) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/numeric_with_unit.rb', line 82

def *(other)
  case other
  when self.class
    multiply_with_other_unit(other)
  else
    self.class.new(@value*other, @unit)
  end
end

#**(num) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/numeric_with_unit.rb', line 108

def **(num)
  # Dimension Check
  @unit.derivation.each do |k,v|
    res = v * num
    raise DimensionError, "Dimension of #{k.symbol}(#{v}*#{num}) must be Integer" unless res.to_i == res # 判定方法見なおせ
  end
  
  self.class.new(@value**num, @unit**num)
end

#+(other) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/numeric_with_unit.rb', line 69

def +(other)
  nwu = if other.is_a? self.class
    other
  else
    self.class.new(other, Unit.new)
  end
  add_with_other_unit(nwu)
end

#+@Object



61
62
63
# File 'lib/numeric_with_unit.rb', line 61

def +@
  self
end

#-(other) ⇒ Object



78
79
80
# File 'lib/numeric_with_unit.rb', line 78

def -(other)
  self + (-other)
end

#-@Object



65
66
67
# File 'lib/numeric_with_unit.rb', line 65

def -@
  self.class.new(-@value, @unit)
end

#/(other) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/numeric_with_unit.rb', line 91

def /(other)
  case other
  when self.class
    devide_with_other_unit(other)
  else
    self.class.new(@value/other, @unit)
  end
end

#<=>(other) ⇒ Object

If ohter is NumericWithUnit and same dimension, comparing value with converting to si. Else return nil.



27
28
29
30
31
# File 'lib/numeric_with_unit.rb', line 27

def <=>(other)
  if other.is_a?(self.class) and @unit.dimension_equal? other.unit
    @unit.to_si(@value) <=> other.unit.to_si(other.value)
  end
end

#cbrtObject

立方根



122
# File 'lib/numeric_with_unit.rb', line 122

def cbrt; root(3) end

#coerce(other) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/numeric_with_unit.rb', line 100

def coerce(other)
  if other.is_a?(self.class)
    [other, self]
  else
    [self.class.new(other, Unit.new), self]
  end
end

#inspectObject

Return String for inspect



16
17
18
# File 'lib/numeric_with_unit.rb', line 16

def inspect
  "#{@value.inspect} [#{@unit.symbol}] #{unit.dimension.inspect}"
end

#root(num) ⇒ Object



118
119
120
# File 'lib/numeric_with_unit.rb', line 118

def root(num)
  self**(Rational(1,num))
end

#sqrtObject

平方根



121
# File 'lib/numeric_with_unit.rb', line 121

def sqrt; root(2) end

#succObject

Return succed value with same unit.



34
35
36
# File 'lib/numeric_with_unit.rb', line 34

def succ
  self.class.new(@value.succ, @unit)
end

#to_fObject

Return value.to_f



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

def to_f
  @value.to_f
end

#to_iObject

Return value.to_i



39
40
41
# File 'lib/numeric_with_unit.rb', line 39

def to_i
  @value.to_i
end

#to_nwu(unit) ⇒ Object Also known as: []

Return NumericWithUnit with given unit



49
50
51
52
53
54
55
56
57
58
# File 'lib/numeric_with_unit.rb', line 49

def to_nwu(unit)
  new_unit = unit.is_a?(Unit) ? unit : Unit[unit]
  
  unless @unit.dimension_equal? new_unit
    raise DimensionError, "Dimensions are different between #{@unit.symbol}#{@unit.dimension} #{new_unit.symbol}#{new_unit.dimension}"
  end
  
  new_value = new_unit.from_si(@unit.to_si(@value))
  self.class.new(new_value, new_unit)
end

#to_sObject

Return String with value and unit symbol



21
22
23
# File 'lib/numeric_with_unit.rb', line 21

def to_s
  "#{@value.to_s} #{@unit.symbol}"
end