Class: Rational

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#to_s_as_inspectBoolean?

Note:

This attribute is added to Rational's singleton class via refinement. Requires using Musa::Extension::InspectNice.

Controls whether Rational#to_s uses inspect format.

When true: to_s displays detailed format (e.g., "1+1/4r") When false/nil: to_s displays simple format (e.g., "5/4")

Examples:

Switching modes

using Musa::Extension::InspectNice
Rational.to_s_as_inspect = true
(5/4r).to_s  # => "1+1/4r"

Returns:

  • (Boolean, nil)

    current mode



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

class ::Rational; end

Instance Method Details

#inspect(simple: nil) ⇒ String

Note:

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

Provides musical-friendly inspect output for Rational numbers.

Two modes:

  • Simple: Just numerator/denominator (e.g., "5/4", "2")
  • Detailed: Mixed number with 'r' suffix (e.g., "1+1/4r", "2r")

The detailed format is particularly useful for musical time values, making expressions like "3+1/2r" (3.5 bars) immediately readable.

Examples:

Detailed format (default for inspect)

using Musa::Extension::InspectNice
(5/4r).inspect            # => "1+1/4r"
(7/4r).inspect            # => "1+3/4r"
(-3/2r).inspect           # => "-1-1/2r"
(8/4r).inspect            # => "2r"
(3/4r).inspect            # => "3/4r"

Simple format

using Musa::Extension::InspectNice
(5/4r).inspect(simple: true)   # => "5/4"
(8/4r).inspect(simple: true)   # => "2"

Parameters:

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

    if true, uses simple format; if false/nil, uses detailed.

Returns:

  • (String)

    formatted rational.



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

class ::Rational; end

#to_sString

Note:

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

Provides string representation, format controlled by Rational.to_s_as_inspect.

Delegates to #inspect with the appropriate simple flag based on the global Rational.to_s_as_inspect setting.

Examples:

When to_s_as_inspect is true

using Musa::Extension::InspectNice
Rational.to_s_as_inspect = true
(5/4r).to_s  # => "1+1/4r"

When to_s_as_inspect is false/nil

using Musa::Extension::InspectNice
Rational.to_s_as_inspect = false
(5/4r).to_s  # => "5/4"

Returns:

  • (String)

    formatted rational.



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

class ::Rational; end