Module: Tins::Constant

Included in:
Module
Defined in:
lib/tins/dslkit.rb

Overview

A module that provides method-based DSL constant creation functionality. These constants are particularly useful for creating DSLs and configuration systems where symbolic names improve readability and maintainability.

Examples:

Basic constant creation

class MyClass
  extend Tins::Constant

  constant :yes, true
  constant :no, false
end

MyClass.instance_eval do
  yes    # => true
  no     # => false
end

See Also:

Instance Method Summary collapse

Instance Method Details

#constant(name, value = name) ⇒ void

This method returns an undefined value.

Creates a method-based constant named name that returns value.

This method defines a method with the given name that always returns the specified value. The value is attempted to be frozen for immutability, though this will fail gracefully if freezing isn’t possible for the value.

Parameters:

  • name (Symbol)

    The name of the constant method to define

  • value (Object) (defaults to: name)

    The value the constant should return (defaults to name)



337
338
339
340
# File 'lib/tins/dslkit.rb', line 337

def constant(name, value = name)
  value = value.freeze rescue value
  define_method(name) { value }
end