# Class: Numeric

Inherits:
Object
show all
Extended by:
Docs, NumericDocs
Includes:
NumericDeprecated, ValueType
Defined in:
dragon/numeric.rb,
dragon/numeric_docs.rb

## Overview

Copyright 2019 DragonRuby LLC MIT License numeric.rb has been released under MIT (*only this file*).

## Instance Method Summary collapse

• Easing function progress/percentage for a specific point in time.

• Easing function progress/percentage for a specific point in time.

• Easing function progress/percentage for a specific point in time.

• Easing function progress/percentage for a specific point in time.

• Returns `true` if the numeric value has passed a duration/offset number.

• Easing function progress/percentage for a specific point in time.

• Easing function progress/percentage for a specific point in time.

• Divides the number by `2.0` and returns a `float`.

• Divides `self` by a number `n` as a float, and converts it `to_i`.

• Given `self` and a number representing `y` of a grid.

• This provides a way for a numeric value to be randomized based on a combination of two options: `:sign` and `:ratio`.

• Converts a numeric value representing seconds into frames.

• Converts a number representing an angle in radians to degrees.

• Converts a number representing an angle in degrees to radians.

• Given `self`, a rectangle primitive is returned.

• Returns a numeric value that is a quantity `magnitude` closer to `self`.

• Returns a normal vector for a number that represents an angle in degrees.

• Returns the x component of a normal vector for a number that represents an angle in degrees.

• Returns the y component of a normal vector for a number that represents an angle in degrees.

## Class Method Details

### .clamp(n, min, max) ⇒ Object

 ``` 579 580 581``` ```# File 'dragon/numeric.rb', line 579 def self.clamp n, min, max n.clamp min, max end```

### .from_right(n) ⇒ Object

 ``` 570 571 572 573``` ```# File 'dragon/numeric.rb', line 570 def self.from_right n return 1280 - n unless \$gtk \$gtk.args.grid.right - n end```

### .from_top(n) ⇒ Object

 ``` 561 562 563 564``` ```# File 'dragon/numeric.rb', line 561 def self.from_top n return 720 - n unless \$gtk \$gtk.args.grid.top - n end```

## Instance Method Details

### #*(other) ⇒ Object

 ``` 543 544 545 546 547 548``` ```# File 'dragon/numeric.rb', line 543 def * other return nil unless other super rescue Exception => e __raise_arithmetic_exception__ other, :*, e end```

### #+(other) ⇒ Object

 ``` 536 537 538 539 540 541``` ```# File 'dragon/numeric.rb', line 536 def + other return nil unless other super rescue Exception => e __raise_arithmetic_exception__ other, :+, e end```

### #-(other) ⇒ Object

 ``` 529 530 531 532 533 534``` ```# File 'dragon/numeric.rb', line 529 def - other return nil unless other super rescue Exception => e __raise_arithmetic_exception__ other, :-, e end```

### #/(other) ⇒ Object

 ``` 550 551 552 553 554 555``` ```# File 'dragon/numeric.rb', line 550 def / other return nil unless other super rescue Exception => e __raise_arithmetic_exception__ other, :/, e end```

### #<(other) ⇒ Object

 ``` 433 434 435 436``` ```# File 'dragon/numeric.rb', line 433 def < other return false if !other return gt other end```

### #<=(other) ⇒ Object

 ``` 428 429 430 431``` ```# File 'dragon/numeric.rb', line 428 def <= other return false if !other return lte other end```

### #==(other) ⇒ Object

 ``` 438 439 440 441 442``` ```# File 'dragon/numeric.rb', line 438 def == other return true if __original_eq_eq__ other return __original_eq_eq__ other.entity_id if other.is_a? OpenEntity return false end```

### #>(other) ⇒ Object

 ``` 423 424 425 426``` ```# File 'dragon/numeric.rb', line 423 def > other return false if !other return gt other end```

### #>=(other) ⇒ Object

 ``` 418 419 420 421``` ```# File 'dragon/numeric.rb', line 418 def >= other return false if !other return gte other end```

### #__original_eq_eq__ ⇒ Object

 ``` 14``` ```# File 'dragon/numeric.rb', line 14 alias_method :__original_eq_eq__, :==```

### #__raise_arithmetic_exception__(other, m, e) ⇒ Object

 ``` 516 517 518 519 520 521 522 523 524 525 526 527``` ```# File 'dragon/numeric.rb', line 516 def __raise_arithmetic_exception__ other, m, e raise <<-S * ERROR: Attempted to invoke :#{m} on #{self} with the right hand argument of: #{other} The object above is not a Numeric. #{e} S end```

### #above? ⇒ Object

 ``` 128``` ```# File 'dragon/numeric.rb', line 128 alias_method :above?, :>```

 ``` 406 407 408``` ```# File 'dragon/numeric.rb', line 406 def add i self + i end```

### #below? ⇒ Object

 ``` 132``` ```# File 'dragon/numeric.rb', line 132 alias_method :below?, :<```

### #cap(i) ⇒ Object

 ``` 377 378 379 380``` ```# File 'dragon/numeric.rb', line 377 def cap i return i if self > i self end```

### #cap_min_max(min, max) ⇒ Object

 ``` 382 383 384 385 386``` ```# File 'dragon/numeric.rb', line 382 def cap_min_max min, max return min if self < min return max if self > max self end```

### #clamp_wrap(min, max) ⇒ Object

 ``` 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50``` ```# File 'dragon/numeric.rb', line 35 def clamp_wrap min, max max, min = min, max if min > max return self if self >= min && self <= max return min if min == max if self < min overflow = min - self return (max - overflow).clamp_wrap min, max end if self > max overflow = self - max return (min + overflow).clamp_wrap min, max end return self end```

### #combinations(other_int) ⇒ Object

 ``` 369 370 371``` ```# File 'dragon/numeric.rb', line 369 def combinations other_int self.numbers.product(other_int.numbers) end```

### #each ⇒ Object

 ``` 459 460 461 462 463 464 465 466 467 468 469 470 471``` ```# File 'dragon/numeric.rb', line 459 def each unless block_given? raise <<-S * ERROR: A block is required for Numeric#each. S end self.to_i.times do yield end end```

### #each_with_index ⇒ Object

 ``` 487 488 489 490 491 492 493 494 495 496 497 498 499``` ```# File 'dragon/numeric.rb', line 487 def each_with_index unless block_given? raise <<-S * ERROR: A block is required for Numeric#each_with_index. S end self.to_i.times.with_index do |i| yield i end end```

### #ease(duration, *definitions) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 216 217 218 219 220 221 222``` ```# File 'dragon/numeric.rb', line 216 def ease duration, *definitions ease_extended Kernel.tick_count, duration, GTK::Easing.initial_value(*definitions), GTK::Easing.final_value(*definitions), *definitions end```

### #ease_extended(tick_count_override, duration, default_before, default_after, *definitions) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 193 194 195 196 197 198 199 200``` ```# File 'dragon/numeric.rb', line 193 def ease_extended tick_count_override, duration, default_before, default_after, *definitions GTK::Easing.ease_extended self, tick_count_override, self + duration, default_before, default_after, *definitions end```

### #ease_spline(duration, spline) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 246 247 248 249 250``` ```# File 'dragon/numeric.rb', line 246 def ease_spline duration, spline ease_spline_extended Kernel.tick_count, duration, spline end```

### #ease_spline_extended(tick_count_override, duration, spline) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 227 228 229 230 231 232``` ```# File 'dragon/numeric.rb', line 227 def ease_spline_extended tick_count_override, duration, spline GTK::Easing.ease_spline_extended self, tick_count_override, self + duration, spline end```

### #elapsed?(offset = 0, tick_count_override = Kernel.tick_count) ⇒ Boolean

Returns `true` if the numeric value has passed a duration/offset number. `Kernel.tick_count` is used to determine if a number represents an elapsed moment in time.

Returns:

• (Boolean)
 ``` 69 70 71``` ```# File 'dragon/numeric.rb', line 69 def elapsed? offset = 0, tick_count_override = Kernel.tick_count (self + offset) < tick_count_override end```

### #elapsed_time(tick_count_override = nil) ⇒ Object

 ``` 52 53 54``` ```# File 'dragon/numeric.rb', line 52 def elapsed_time tick_count_override = nil (tick_count_override || Kernel.tick_count) - self end```

### #elapsed_time_percent(duration) ⇒ Object

 ``` 56 57 58``` ```# File 'dragon/numeric.rb', line 56 def elapsed_time_percent duration elapsed_time.percentage_of duration end```

### #fdiv(n) ⇒ Object

 ``` 322 323 324``` ```# File 'dragon/numeric.rb', line 322 def fdiv n self / n.to_f end```

### #five ⇒ Object

 ``` 119 120 121``` ```# File 'dragon/numeric.rb', line 119 def five 5 end```

### #frame_index(*opts) ⇒ Object

 ``` 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101``` ```# File 'dragon/numeric.rb', line 73 def frame_index *opts frame_count_or_hash, hold_for, repeat, tick_count_override = opts if frame_count_or_hash.is_a? Hash frame_count = frame_count_or_hash[:count] hold_for = frame_count_or_hash[:hold_for] repeat = frame_count_or_hash[:repeat] tick_count_override = frame_count_or_hash[:tick_count_override] else frame_count = frame_count_or_hash end tick_count_override ||= Kernel.tick_count animation_frame_count = frame_count animation_frame_hold_time = hold_for animation_length = animation_frame_hold_time * animation_frame_count return nil if Kernel.tick_count < self if !repeat && (self + animation_length) < (tick_count_override - 1) return nil else return self.elapsed_time.-(1).idiv(animation_frame_hold_time) % animation_frame_count end rescue Exception => e raise <<-S * ERROR: #{opts} #{e} S end```

### #from_right ⇒ Object

 ``` 575 576 577``` ```# File 'dragon/numeric.rb', line 575 def from_right Numeric.from_right self end```

### #from_top ⇒ Object

 ``` 566 567 568``` ```# File 'dragon/numeric.rb', line 566 def from_top Numeric.from_top self end```

### #global_ease(duration, *definitions) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 205 206 207 208 209 210 211``` ```# File 'dragon/numeric.rb', line 205 def global_ease duration, *definitions ease_extended Kernel.global_tick_count, duration, GTK::Easing.initial_value(*definitions), GTK::Easing.final_value(*definitions), *definitions end```

### #global_ease_spline(duration, spline) ⇒ Object

Easing function progress/percentage for a specific point in time.

 ``` 237 238 239 240 241``` ```# File 'dragon/numeric.rb', line 237 def global_ease_spline duration, spline ease_spline_extended Kernel.global_tick_count, duration, spline end```

### #greater(other) ⇒ Object

 ``` 393 394 395 396``` ```# File 'dragon/numeric.rb', line 393 def greater other return other if other > self self end```

### #gt ⇒ Object

 ``` 10``` ```# File 'dragon/numeric.rb', line 10 alias_method :gt, :>```

### #gte ⇒ Object

 ``` 11``` ```# File 'dragon/numeric.rb', line 11 alias_method :gte, :>=```

### #half ⇒ Object

Divides the number by `2.0` and returns a `float`.

 ``` 27 28 29``` ```# File 'dragon/numeric.rb', line 27 def half self / 2.0 end```

### #idiv(n) ⇒ Object

Divides `self` by a number `n` as a float, and converts it `to_i`.

 ``` 329 330 331``` ```# File 'dragon/numeric.rb', line 329 def idiv n (self / n.to_f).to_i end```

### #left_of? ⇒ Object

 ``` 133``` ```# File 'dragon/numeric.rb', line 133 alias_method :left_of?, :<```

### #lesser(other) ⇒ Object

 ``` 388 389 390 391``` ```# File 'dragon/numeric.rb', line 388 def lesser other return other if other < self self end```

### #lt ⇒ Object

 ``` 12``` ```# File 'dragon/numeric.rb', line 12 alias_method :lt, :<```

### #lte ⇒ Object

 ``` 13``` ```# File 'dragon/numeric.rb', line 13 alias_method :lte, :<=```

### #map ⇒ Object

 ``` 445 446 447 448 449 450 451 452 453 454 455 456 457``` ```# File 'dragon/numeric.rb', line 445 def map unless block_given? raise <<-S * ERROR: A block is required for Numeric#map. S end self.to_i.times.map do yield end end```

### #map_with_index ⇒ Object

 ``` 502 503 504 505 506 507 508 509 510 511 512 513 514``` ```# File 'dragon/numeric.rb', line 502 def map_with_index unless block_given? raise <<-S * ERROR: A block is required for Numeric#map. S end self.to_i.times.map do |i| yield i end end```

### #map_with_ys(ys, &block) ⇒ Object

Given `self` and a number representing `y` of a grid. This function will return a one dimensional array containing the value yielded by an implicit block.

Examples:

``````3.map_with_ys 2 do |x, y|
x * y
end
#     x y   x y  x y  x y  x y  x y
#     0*0,  0*1  1*0  1*1  2*0  2*1
# => [  0,    0,   0,   1,   0,   2]``````
 ``` 359 360 361 362 363 364 365 366 367``` ```# File 'dragon/numeric.rb', line 359 def map_with_ys ys, &block self.times.flat_map do |x| ys.map_with_index do |y| yield x, y end end rescue Exception => e raise_immediately e, :map_with_ys, [self, ys] end```

### #minus(i) ⇒ Object

 ``` 402 403 404``` ```# File 'dragon/numeric.rb', line 402 def minus i self - i end```

### #mod(n) ⇒ Object

 ``` 305 306 307``` ```# File 'dragon/numeric.rb', line 305 def mod n self % n end```

### #mod_zero?(*ns) ⇒ Boolean

Returns:

• (Boolean)
 ``` 309 310 311``` ```# File 'dragon/numeric.rb', line 309 def mod_zero? *ns ns.any? { |n| mod(n) == 0 } end```

### #mult(n) ⇒ Object

 ``` 317 318 319``` ```# File 'dragon/numeric.rb', line 317 def mult n self * n end```

### #new? ⇒ Boolean

Returns:

• (Boolean)
 ``` 60 61 62``` ```# File 'dragon/numeric.rb', line 60 def new? elapsed_time == 0 end```

### #numbers ⇒ Object

 ``` 414 415 416``` ```# File 'dragon/numeric.rb', line 414 def numbers (0..self).to_a end```

### #one ⇒ Object

 ``` 111 112 113``` ```# File 'dragon/numeric.rb', line 111 def one 1 end```

### #percentage_of(n) ⇒ Object

 ``` 373 374 375``` ```# File 'dragon/numeric.rb', line 373 def percentage_of n (self / n.to_f).cap_min_max(0, 1) end```

### #plus(i) ⇒ Object

 ``` 410 411 412``` ```# File 'dragon/numeric.rb', line 410 def plus i self + i end```

### #rand_ratio ⇒ Object

 ``` 182 183 184``` ```# File 'dragon/numeric.rb', line 182 def rand_ratio self * rand end```

### #rand_sign ⇒ Object

 ``` 177 178 179 180``` ```# File 'dragon/numeric.rb', line 177 def rand_sign return self * -1 if rand > 0.5 self end```

### #randomize(*definitions) ⇒ Object

This provides a way for a numeric value to be randomized based on a combination of two options: `:sign` and `:ratio`.

 ``` 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175``` ```# File 'dragon/numeric.rb', line 161 def randomize *definitions result = self if definitions.include?(:sign) result = rand_sign end if definitions.include?(:ratio) result = rand * result elsif definitions.include?(:int) result = (rand result) end result end```

### #remainder_of_divide(n) ⇒ Object

 ``` 186 187 188``` ```# File 'dragon/numeric.rb', line 186 def remainder_of_divide n mod n end```

### #right_of? ⇒ Object

 ``` 129``` ```# File 'dragon/numeric.rb', line 129 alias_method :right_of?, :>```

### #seconds ⇒ Object

Converts a numeric value representing seconds into frames.

 ``` 20 21 22``` ```# File 'dragon/numeric.rb', line 20 def seconds self * 60 end```

### #serialize ⇒ Object

 ``` 557 558 559``` ```# File 'dragon/numeric.rb', line 557 def serialize self end```

### #shift_down(i) ⇒ Object

 ``` 151 152 153 154 155``` ```# File 'dragon/numeric.rb', line 151 def shift_down i shift_up(i * -1) rescue Exception => e raise_immediately e, :shift_down, i end```

### #shift_left(i) ⇒ Object

 ``` 139 140 141 142 143``` ```# File 'dragon/numeric.rb', line 139 def shift_left i shift_right(i * -1) rescue Exception => e raise_immediately e, :shift_left, i end```

### #shift_right(i) ⇒ Object

 ``` 135 136 137``` ```# File 'dragon/numeric.rb', line 135 def shift_right i self + i end```

### #shift_up(i) ⇒ Object

 ``` 145 146 147 148 149``` ```# File 'dragon/numeric.rb', line 145 def shift_up i self + i rescue Exception => e raise_immediately e, :shift_up, i end```

### #subtract(i) ⇒ Object

 ``` 398 399 400``` ```# File 'dragon/numeric.rb', line 398 def subtract i self - i end```

### #ten ⇒ Object

 ``` 123 124 125``` ```# File 'dragon/numeric.rb', line 123 def ten 10 end```

### #times_with_index ⇒ Object

 ``` 473 474 475 476 477 478 479 480 481 482 483 484 485``` ```# File 'dragon/numeric.rb', line 473 def times_with_index unless block_given? raise <<-S * ERROR: A block is required for Numeric#times_with_index. S end self.to_i.times.with_index do |i| yield i end end```

### #to_byte ⇒ Object

 ``` 31 32 33``` ```# File 'dragon/numeric.rb', line 31 def to_byte clamp(0, 255).to_i end```

### #to_degrees ⇒ Object

Converts a number representing an angle in radians to degrees.

 ``` 262 263 264``` ```# File 'dragon/numeric.rb', line 262 def to_degrees self / Math::PI.fdiv(180) end```

Converts a number representing an angle in degrees to radians.

 ``` 255 256 257``` ```# File 'dragon/numeric.rb', line 255 def to_radians self * Math::PI.fdiv(180) end```

### #to_square(x, y, anchor_x = 0.5, anchor_y = nil) ⇒ Object

Given `self`, a rectangle primitive is returned.

Examples:

``5.to_square 100, 300 # returns [100, 300, 5, 5]``
 ``` 272 273 274``` ```# File 'dragon/numeric.rb', line 272 def to_square x, y, anchor_x = 0.5, anchor_y = nil GTK::Geometry.to_square(self, x, y, anchor_x, anchor_y) end```

### #towards(target, magnitude) ⇒ Object

Returns a numeric value that is a quantity `magnitude` closer to `self`. If the distance between `self` and `target` is less than the `magnitude` then `target` is returned.

 ``` 338 339 340 341 342 343 344``` ```# File 'dragon/numeric.rb', line 338 def towards target, magnitude return self if self == target delta = (self - target).abs return target if delta < magnitude return self - magnitude if self > target return self + magnitude end```

### #two ⇒ Object

 ``` 115 116 117``` ```# File 'dragon/numeric.rb', line 115 def two 2 end```

### #vector(max_value = 1) ⇒ Object

Returns a normal vector for a number that represents an angle in degrees.

 ``` 279 280 281``` ```# File 'dragon/numeric.rb', line 279 def vector max_value = 1 [vector_x(max_value), vector_y(max_value)] end```

### #vector_x(max_value = 1) ⇒ Object

Returns the x component of a normal vector for a number that represents an angle in degrees.

 ``` 293 294 295``` ```# File 'dragon/numeric.rb', line 293 def vector_x max_value = 1 max_value * Math.cos(self.to_radians) end```

### #vector_y(max_value = 1) ⇒ Object

Returns the y component of a normal vector for a number that represents an angle in degrees.

 ``` 286 287 288``` ```# File 'dragon/numeric.rb', line 286 def vector_y max_value = 1 max_value * Math.sin(self.to_radians) end```

### #x_vector(max_value = 1) ⇒ Object

 ``` 297 298 299``` ```# File 'dragon/numeric.rb', line 297 def x_vector max_value = 1 vector_x max_value end```

### #y_vector(max_value = 1) ⇒ Object

 ``` 301 302 303``` ```# File 'dragon/numeric.rb', line 301 def y_vector max_value = 1 vector_y max_value end```

### #zero ⇒ Object

 ``` 107 108 109``` ```# File 'dragon/numeric.rb', line 107 def zero 0 end```

### #zero? ⇒ Boolean

Returns:

• (Boolean)
 ``` 103 104 105``` ```# File 'dragon/numeric.rb', line 103 def zero? self == 0 end```

### #zmod?(n) ⇒ Boolean

Returns:

• (Boolean)
 ``` 313 314 315``` ```# File 'dragon/numeric.rb', line 313 def zmod? n (self % n) == 0 end```