Class: Object

Inherits:
BasicObject
Defined in:
lib/musa-dsl/core-ext/arrayfy.rb,
lib/musa-dsl/core-ext/hashify.rb,
lib/musa-dsl/core-ext/deep-copy.rb,
lib/musa-dsl/core-ext/deep-copy.rb

Instance Method Summary collapse

Instance Method Details

#arrayfy(size: nil, default: nil) ⇒ Array

Note:

This method is added to Object via refinement. Requires using Musa::Extension::Arrayfy.

Converts any object into an array, optionally repeated to a target size.

Examples:

With size

using Musa::Extension::Arrayfy
"hello".arrayfy(size: 3)  # => ["hello", "hello", "hello"]

Nil handling

using Musa::Extension::Arrayfy
nil.arrayfy(size: 2, default: :empty)  # => [:empty, :empty]

Parameters:

  • size (Integer, nil) (defaults to: nil)

    target array length. If nil, returns single-element array.

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

    value to use instead of nil.

Returns:

  • (Array)

    single element repeated size times, or wrapped in array if size is nil.



83
# File 'lib/musa-dsl/core-ext/arrayfy.rb', line 83

class ::Object; end

#clone(freeze: nil, deep: false) ⇒ Object

Note:

This method is added to Object via refinement. Requires using Musa::Extension::DeepCopy.

Enhanced clone with optional deep copy.

Unlike :dup, a deep :clone preserves the modules an object has been extended with, which is why datasets are copied with it.

Freezing follows Object#clone's three rules, applied to every node of the graph rather than just the top one: true freezes, false does not, and the default nil gives each copy the frozen state its own original had.

Examples:

Deep clone

using Musa::Extension::DeepCopy
hash = { nested: { value: 1 } }
copy = hash.clone(deep: true)
copy[:nested].equal?(hash[:nested])  # => false
copy[:nested][:value] = 2
hash[:nested][:value]  # => 1

freeze: true freezes the whole copy, all the way down

using Musa::Extension::DeepCopy
copy = { nested: { value: 1 } }.clone(deep: true, freeze: true)
copy.frozen?           # => true
copy[:nested].frozen?  # => true

By default each node keeps the state its own original had

using Musa::Extension::DeepCopy
original = { constant: { name: 'white' }.freeze, mutable: [1] }
copy = original.clone(deep: true)
copy.frozen?             # => false
copy[:constant].frozen?  # => true
copy[:mutable].frozen?   # => false

# Which is what `clone` means in Ruby. `dup` is the one that hands
# back everything unfrozen.

Parameters:

  • freeze (Boolean, nil) (defaults to: nil)

    whether to freeze the clone.

  • deep (Boolean) (defaults to: false)

    if true, performs deep copy; if false, standard clone.

Returns:

  • (Object)

    cloned object (shallow or deep).



354
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 354

class ::Object; end

#dup(deep: false) ⇒ Object

Note:

This method is added to Object via refinement. Requires using Musa::Extension::DeepCopy.

Enhanced dup with optional deep copy.

Examples:

Shallow dup (default)

using Musa::Extension::DeepCopy
arr = [[1, 2]]
copy = arr.dup
copy[0] << 3
arr  # => [[1, 2, 3]] (inner array shared)

Deep dup

using Musa::Extension::DeepCopy
arr = [[1, 2]]
copy = arr.dup(deep: true)
copy[0] << 3
arr  # => [[1, 2]] (inner array independent)

Parameters:

  • deep (Boolean) (defaults to: false)

    if true, performs deep copy; if false, standard dup.

Returns:

  • (Object)

    duplicated object (shallow or deep).



310
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 310

class ::Object; end

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

Note:

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

Creates a hash mapping all specified keys to this object's value.

Useful for broadcasting a single value across multiple attributes. Nil objects can be replaced with a default value.

Examples:

Single value to multiple keys

using Musa::Extension::Hashify
127.hashify(keys: [:velocity, :pressure])
# => { velocity: 127, pressure: 127 }

Parameters:

  • keys (Array<Symbol>)

    keys for the resulting hash.

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

    value to use if self is nil.

Returns:

  • (Hash)

    hash with all keys mapped to self (or default if nil).



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

class ::Object; end