Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/string_builder.rb

Overview

Ruby evaluates ‘3.px` as Integer#px — a method call on the literal 3. Without this patch, that raises NoMethodError because Integer has no `px`.

Inside a ScopedStringBuilder block, expressions like ‘a.b / 3.px` need the right-hand operand `3.px` to produce an InnerStringBuilder so the `/` operator can cleanly append the separator symbol followed by the operand’s tokens. The problem is that Ruby resolves ‘3.px` before `/` is called, and it resolves it on Integer, not on the ScopedStringBuilder.

The monkey patch bridges this gap: any unknown method on an Integer creates a new InnerStringBuilder, records the integer as the first token via ‘call`, then forwards the method (e.g. `px`) so it chains naturally. The result is a standalone builder that the operator receives as a distinct `other` object — no different from how any other right-hand operand works.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



109
110
111
# File 'lib/string_builder.rb', line 109

def method_missing(*)
  InnerStringBuilder.new.call(self.to_s).send(*)
end

Instance Method Details

#respond_to_missing?Boolean

Returns:

  • (Boolean)


107
# File 'lib/string_builder.rb', line 107

def respond_to_missing?(...) = true