Module: MathViz::Measured

Includes:
Units
Included in:
Term
Defined in:
lib/mathviz.rb

Overview

Something (i.e. MathViz::Term) which has MathViz::Units.

Instance Method Summary collapse

Methods included from Units::Class

#included, #new_units

Instance Method Details

#perObject

Statefull toggle numerator/denominator of unit assignment; e.g. m/s = .m.per.s



268
269
270
271
272
# File 'lib/mathviz.rb', line 268

def per
  @unit_sign ||= 1
  @unit_sign *= -1
  self
end

#to_valueObject

api method for unwrapping terms



280
281
282
# File 'lib/mathviz.rb', line 280

def to_value
  to_f
end

#unit(x) ⇒ Object

Add the named unit to our units and return self. See also MathViz::Units::Class#new_units



256
257
258
259
260
261
262
263
264
265
# File 'lib/mathviz.rb', line 256

def unit(x)
  @unit ||= MathViz::Unit.new
  @unit_sign ||= 1
  if (@unit_sign > 0)
    @unit *= MathViz::Unit.new(x)
  else
    @unit /= MathViz::Unit.new(x)
  end
  self
end

#unitsObject

attr_reader



275
276
277
# File 'lib/mathviz.rb', line 275

def units
  @unit || MathViz::Unit.new
end

#with_unitsObject

Return a string representation of the units portion, with space if applicable



246
247
248
249
250
251
252
253
# File 'lib/mathviz.rb', line 246

def with_units
  u = units.to_s
  if (u.empty?)
    u
  else
    ' ' + u
  end
end