Module: Musa::Extension::Arrayfy

Defined in:
lib/musa-dsl/core-ext/arrayfy.rb

Overview

Note:

This refinement must be activated with using Musa::Extension::Arrayfy

Note:

Arrays are cloned and singleton class modules are preserved

Refinement that converts any object to an array, with optional repetition and defaults.

This refinement is essential for normalizing parameters in the DSL, allowing users to provide either single values or arrays and have them processed uniformly.

Core Behavior

  • Object: Wraps in array; nil becomes []
  • Array: Returns clone or cycles to requested size
  • size parameter: Repeats/cycles to achieve target length
  • default parameter: Replaces nil values

Use Cases

  • Normalizing velocity parameters (single value or per-note array)
  • Ensuring consistent array handling in DSL methods
  • Cycling patterns to fill required lengths
  • Providing default values for missing data

Methods Added

Object

  • Object#arrayfy - Converts any object into an array, optionally repeated to a target size

Array

  • Array#arrayfy - Clones or cycles the array to achieve the target size, with nil replacement

Examples:

Basic object wrapping

using Musa::Extension::Arrayfy

5.arrayfy           # => [5]
nil.arrayfy         # => []
[1, 2, 3].arrayfy   # => [1, 2, 3]

Repetition with size

using Musa::Extension::Arrayfy

5.arrayfy(size: 3)        # => [5, 5, 5]
[1, 2].arrayfy(size: 5)   # => [1, 2, 1, 2, 1]
[1, 2, 3].arrayfy(size: 2) # => [1, 2]

Default values for nil

using Musa::Extension::Arrayfy

nil.arrayfy(size: 3, default: 0)           # => [0, 0, 0]
[1, nil, 3].arrayfy(size: 5, default: -1)  # => [1, -1, 3, 1, -1]

Musical application - velocity normalization

using Musa::Extension::Arrayfy

# User provides single velocity for chord
velocities = 90.arrayfy(size: 3)  # => [90, 90, 90]

# User provides array of velocities that cycles
velocities = [80, 100].arrayfy(size: 5)  # => [80, 100, 80, 100, 80]

See Also: