Class: Code::Object::Number

Inherits:
Code::Object show all
Defined in:
lib/code/object/number.rb

Direct Known Subclasses

Decimal, Integer

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Number",
  description:
    "represents numeric behavior shared by integers and decimals.",
  examples: [
    "Number.documentation.name",
    "Number.instance_functions.keys.include?(:between?)",
    "1.5.clamp(0, 1)"
  ]
}.freeze
INSTANCE_FUNCTIONS =
{
  "between?" => {
    name: "between?",
    description: "returns whether the number is within inclusive bounds.",
    examples: [
      "2.between?(1, 3)",
      "1.5.between?(1, 2)",
      "4.between?(1, 3)"
    ]
  },
  "clamp" => {
    name: "clamp",
    description: "returns the number limited to inclusive bounds.",
    examples: ["5.clamp(1, 3)", "0.clamp(1, 3)", "2.5.clamp(1, 3)"]
  },
  "divide" => {
    name: "divide",
    description:
      "returns floor division of the number by another number.",
    examples: %w[5.divide(2) 10.divide(3) 9.5.divide(2)]
  },
  "divide_modulo" => {
    name: "divide_modulo",
    description: "returns floor division and modulo as a list.",
    examples: %w[
      5.divide_modulo(2)
      10.divide_modulo(3)
      9.5.divide_modulo(2)
    ]
  },
  "next" => {
    name: "next",
    description: "returns the number plus one.",
    examples: %w[1.next 1.5.next -1.next]
  },
  "successor" => {
    name: "successor",
    description: "returns the number plus one, matching next.",
    examples: %w[1.successor 1.5.successor -1.successor]
  },
  "previous" => {
    name: "previous",
    description: "returns the number minus one.",
    examples: %w[1.previous 1.5.previous -1.previous]
  },
  "predecessor" => {
    name: "predecessor",
    description: "returns the number minus one, matching previous.",
    examples: %w[1.predecessor 1.5.predecessor -1.predecessor]
  },
  "remainder" => {
    name: "remainder",
    description: "returns the remainder from division by another number.",
    examples: %w[5.remainder(2) 10.remainder(3) 9.5.remainder(2)]
  }
}.freeze

Constants inherited from Code::Object

CLASS_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object

Class Method Details

.function_documentation(scope) ⇒ Object



73
74
75
76
77
# File 'lib/code/object/number.rb', line 73

def self.function_documentation(scope)
  return INSTANCE_FUNCTIONS if scope == :instance

  {}
end

Instance Method Details

#code_between?(min, max) ⇒ Boolean

Returns:



79
80
81
82
83
84
# File 'lib/code/object/number.rb', line 79

def code_between?(min, max)
  code_min = min.to_code
  code_max = max.to_code

  Boolean.new(raw.between?(code_min.raw, code_max.raw))
end

#code_clamp(min, max) ⇒ Object



86
87
88
89
90
91
# File 'lib/code/object/number.rb', line 86

def code_clamp(min, max)
  code_min = min.to_code
  code_max = max.to_code

  self.class.new(raw.clamp(code_min.raw, code_max.raw))
end

#code_divide(other) ⇒ Object



93
94
95
96
97
# File 'lib/code/object/number.rb', line 93

def code_divide(other)
  code_other = other.to_code

  Integer.new(raw.div(code_other.raw))
end

#code_divide_modulo(other) ⇒ Object



99
100
101
102
103
# File 'lib/code/object/number.rb', line 99

def code_divide_modulo(other)
  code_other = other.to_code

  List.new(raw.divmod(code_other.raw))
end

#code_nextObject



105
106
107
# File 'lib/code/object/number.rb', line 105

def code_next
  code_plus(Integer.new(1))
end

#code_predecessorObject



123
124
125
# File 'lib/code/object/number.rb', line 123

def code_predecessor
  code_previous
end

#code_previousObject



109
110
111
# File 'lib/code/object/number.rb', line 109

def code_previous
  code_minus(Integer.new(1))
end

#code_remainder(other) ⇒ Object



113
114
115
116
117
# File 'lib/code/object/number.rb', line 113

def code_remainder(other)
  code_other = other.to_code

  self.class.new(raw.remainder(code_other.raw))
end

#code_successorObject



119
120
121
# File 'lib/code/object/number.rb', line 119

def code_successor
  code_next
end