Class: Vissen::Parameterized::Value::Vec

Inherits:
Object
  • Object
show all
Includes:
Vissen::Parameterized::Value
Defined in:
lib/vissen/parameterized/value/vec.rb

Overview

Vector type values are stored internally as arrays of floats.

Usage

vec = Vec[1, 0.2]
vec.value # => [1.0, 0.2]

Constant Summary collapse

DEFAULT =

Returns the default value that will be used when ‘.new` is called without arguments, or with nil.

Returns:

  • (Array<Float>)

    the default value that will be used when ‘.new` is called without arguments, or with nil.

[0.0, 0.0].freeze

Instance Attribute Summary

Attributes included from Vissen::Parameterized::Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Vissen::Parameterized::Value

canonicalize, #scope, #tainted?, #to_s, types, #untaint!

Constructor Details

#initialize(initial_value = nil) ⇒ Vec

Returns a new instance of Vec.

Parameters:

  • initial_value (Array<#to_f>) (defaults to: nil)

    the initial value to use.



21
22
23
24
25
26
# File 'lib/vissen/parameterized/value/vec.rb', line 21

def initialize(initial_value = nil)
  @value = DEFAULT.dup
  taint!

  write initial_value if initial_value
end

Class Method Details

.[](a, b) ⇒ Vec

Returns a new Vec instance.

Parameters:

  • a (#to_f)

    the first vector component.

  • b (#to_f)

    the second vector component.

Returns:

  • (Vec)

    a new Vec instance.



49
50
51
# File 'lib/vissen/parameterized/value/vec.rb', line 49

def [](a, b)
  new [a, b]
end

Instance Method Details

#write(new_value) ⇒ true, false

Parameters:

  • new_value (Array<#to_f>)

    the new values to write.

Returns:

  • (true)

    if the value was changed.

  • (false)

    otherwise.

Raises:

  • (TypeError)

    if the given value does not respond to ‘#[]`.

  • (TypeError)

    if the elements of the given value does not cannot be coerced into floats.



35
36
37
38
39
40
41
42
43
# File 'lib/vissen/parameterized/value/vec.rb', line 35

def write(new_value)
  return if @value == new_value

  @value[0] = Float(new_value[0])
  @value[1] = Float(new_value[1])
  taint!
rescue NoMethodError
  raise TypeError, 'The given object must support #[]'
end