Class: Hash

Inherits:
Object show all
Defined in:
lib/musa-dsl/core-ext/hashify.rb,
lib/musa-dsl/core-ext/inspect-nice.rb,
lib/musa-dsl/core-ext/inspect-nice.rb

Instance Method Summary collapse

Instance Method Details

#hashify(keys: , default: nil) ⇒ Hash

Note:

This method is added to Hash via refinement. Requires using Musa::Extension::Hashify.

Note:

Singleton class modules are preserved via DeepCopy.copy_singleton_class_modules

Filters and reorders hash to include only specified keys, preserving modules.

Creates a new hash with only the requested keys, in the order specified. Missing keys get nil (or default). Singleton class modules (like dataset extensions) are copied to the result.

Examples:

Filtering keys

using Musa::Extension::Hashify
{ pitch: 60, velocity: 100, channel: 0 }
  .hashify(keys: [:pitch, :velocity])
# => { pitch: 60, velocity: 100 }

Reordering keys

using Musa::Extension::Hashify
{ velocity: 100, pitch: 60 }
  .hashify(keys: [:pitch, :velocity])
# => { pitch: 60, velocity: 100 }

Adding missing keys with default

using Musa::Extension::Hashify
{ pitch: 60 }
  .hashify(keys: [:pitch, :velocity], default: 80)
# => { pitch: 60, velocity: 80 }

Preserving dataset modules

using Musa::Extension::Hashify
event = { pitch: 60, velocity: 100 }.extend(Musa::Datasets::AbsI)
event.hashify(keys: [:pitch, :velocity])
# Result also extended with AbsI

Parameters:

  • keys (Array<Symbol>) (defaults to: )

    keys to include in result (order matters).

  • default (Object, nil) (defaults to: nil)

    value for keys not present in source hash.

Returns:

  • (Hash)

    new hash with specified keys, preserving singleton modules.



189
# File 'lib/musa-dsl/core-ext/hashify.rb', line 189

class ::Hash; end

#inspectString

Note:

This method is added to Hash via refinement. Requires using Musa::Extension::InspectNice.

Provides compact, readable inspect output with symbol-key shorthand.

Symbol keys are displayed as key: value (Ruby 2.0+ syntax) instead of :key => value. String/other keys use the fat arrow syntax.

Examples:

Mixed keys

using Musa::Extension::InspectNice
{ pitch: 60, 'name' => 'C4' }.inspect
# => "{ pitch: 60, 'name' => 'C4' }"

Returns:

  • (String)

    compact hash representation.



75
# File 'lib/musa-dsl/core-ext/inspect-nice.rb', line 75

class ::Hash; end

#to_sString

Note:

This method is added to Hash via refinement. Requires using Musa::Extension::InspectNice.

Aliases to_s to inspect for consistency.

Returns:

  • (String)

    compact hash representation.

See Also:



85
# File 'lib/musa-dsl/core-ext/inspect-nice.rb', line 85

class ::Hash; end