Class: Numeric
Constant Summary collapse
- MILLI =
0.001
- CENTI =
MILLI * 10
- DECI =
CENTI * 10
- DECA =
10- HECTO =
DECA * 10
- KILO =
HECTO * 10
- KILOBYTE =
1024- MEGABYTE =
KILOBYTE * 1024
- GIGABYTE =
MEGABYTE * 1024
- TERABYTE =
GIGABYTE * 1024
- PETABYTE =
TERABYTE * 1024
- EXABYTE =
PETABYTE * 1024
- FEET =
12- YARD =
FEET * 3
- MILE =
YARD * 1760
- NAUTICAL_MILE =
MILE * 1.15078
- METRIC_TON =
KILO * 1000
- POUND =
16- STONE =
POUND * 14
- TON =
POUND * 2000
- MINUTE =
60- HOUR =
MINUTE * 60
- DAY =
HOUR * 24
- WEEK =
DAY * 7
- YEAR =
DAY * 365.25
- DECADE =
YEAR * 10
- CENTURY =
DECADE * 10
- MILLENNIUM =
CENTURY * 10
Instance Method Summary collapse
- #add(n) ⇒ Object
- #bytes_in_bytes ⇒ Object (also: #byte_in_bytes)
- #centigrams_in_grams ⇒ Object (also: #centigram_in_grams)
- #centimeters_in_meters ⇒ Object (also: #centimeter_in_meters)
- #centuries_in_seconds ⇒ Object (also: #century_in_seconds)
- #days_in_seconds ⇒ Object (also: #day_in_seconds)
- #decades_in_seconds ⇒ Object (also: #decade_in_seconds)
- #decagrams_in_grams ⇒ Object (also: #decagram_in_grams)
- #decameters_in_meters ⇒ Object (also: #decameter_in_meters)
- #decigrams_in_grams ⇒ Object (also: #decigram_in_grams)
- #decimeters_in_meters ⇒ Object (also: #decimeter_in_meters)
- #distance(n) ⇒ Object
- #divide(n) ⇒ Object
- #exabytes_in_bytes ⇒ Object (also: #exabyte_in_bytes)
- #feet_in_inches ⇒ Object (also: #foot_in_inches)
- #gigabytes_in_bytes ⇒ Object (also: #gigabyte_in_bytes)
- #grams_in_grams ⇒ Object (also: #gram_in_grams)
- #greater_than?(n) ⇒ Boolean
- #greater_than_or_equal_to?(n) ⇒ Boolean
- #hectograms_in_grams ⇒ Object (also: #hectogram_in_grams)
- #hectometers_in_meters ⇒ Object (also: #hectometer_in_meters)
- #hours_in_seconds ⇒ Object (also: #hour_in_seconds)
- #inches_in_inches ⇒ Object (also: #inch_in_inches)
- #inside?(start, finish) ⇒ Boolean
- #kilobytes_in_bytes ⇒ Object (also: #kilobyte_in_bytes)
- #kilograms_in_grams ⇒ Object (also: #kilogram_in_grams)
- #kilometers_in_meters ⇒ Object (also: #kilometer_in_meters)
- #less_than?(n) ⇒ Boolean
- #less_than_or_equal_to?(n) ⇒ Boolean
- #megabytes_in_bytes ⇒ Object (also: #megabyte_in_bytes)
- #meters_in_meters ⇒ Object (also: #meter_in_meters)
- #metric_tons_in_grams ⇒ Object (also: #metric_ton_in_grams)
- #miles_in_inches ⇒ Object (also: #mile_in_inches)
- #millenniums_in_seconds ⇒ Object (also: #millennium_in_seconds)
- #milligrams_in_grams ⇒ Object (also: #milligram_in_grams)
- #millimeters_in_meters ⇒ Object (also: #millimeter_in_meters)
- #minutes_in_seconds ⇒ Object (also: #minute_in_seconds)
- #multiple_of?(number) ⇒ Boolean
- #multiply(n) ⇒ Object
- #nautical_miles_in_inches ⇒ Object (also: #nautical_mile_in_inches)
- #negative? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #ounces_in_ounces ⇒ Object (also: #ounce_in_ounces)
- #outside?(start, finish) ⇒ Boolean
- #pad(options = {}) ⇒ Object
- #pad_precision(options = {}) ⇒ Object
- #petabytes_in_bytes ⇒ Object (also: #petabyte_in_bytes)
- #positive? ⇒ Boolean
- #pounds_in_ounces ⇒ Object (also: #pound_in_ounces)
- #power(n) ⇒ Object
- #root(n) ⇒ Object
- #seconds_in_seconds ⇒ Object (also: #second_in_seconds)
- #stones_in_ounces ⇒ Object (also: #stone_in_ounces)
- #subtract(n) ⇒ Object
- #terabytes_in_bytes ⇒ Object (also: #terabyte_in_bytes)
- #to_byte(from, to) ⇒ Object
- #to_currency(options = {}) ⇒ Object
- #to_length(from, to) ⇒ Object
- #to_mass(from, to) ⇒ Object
- #to_nearest_value(values = []) ⇒ Object
- #to_percentage(options = {}) ⇒ Object
- #to_temperature(from, to) ⇒ Object
- #to_time(from, to) ⇒ Object
- #tons_in_ounces ⇒ Object (also: #ton_in_ounces)
- #weeks_in_seconds ⇒ Object (also: #week_in_seconds)
- #within?(number, epsilon = 0.01) ⇒ Boolean
- #yards_in_inches ⇒ Object (also: #yard_in_inches)
- #years_in_seconds ⇒ Object (also: #year_in_seconds)
Instance Method Details
#add(n) ⇒ Object
38 39 40 |
# File 'lib/active_object/numeric.rb', line 38 def add(n) self + n end |
#bytes_in_bytes ⇒ Object Also known as: byte_in_bytes
42 43 44 |
# File 'lib/active_object/numeric.rb', line 42 def bytes_in_bytes self end |
#centigrams_in_grams ⇒ Object Also known as: centigram_in_grams
48 49 50 |
# File 'lib/active_object/numeric.rb', line 48 def centigrams_in_grams self * CENTI end |
#centimeters_in_meters ⇒ Object Also known as: centimeter_in_meters
54 55 56 |
# File 'lib/active_object/numeric.rb', line 54 def centimeters_in_meters self * CENTI end |
#centuries_in_seconds ⇒ Object Also known as: century_in_seconds
60 61 62 |
# File 'lib/active_object/numeric.rb', line 60 def centuries_in_seconds self * CENTURY end |
#days_in_seconds ⇒ Object Also known as: day_in_seconds
66 67 68 |
# File 'lib/active_object/numeric.rb', line 66 def days_in_seconds self * DAY end |
#decades_in_seconds ⇒ Object Also known as: decade_in_seconds
72 73 74 |
# File 'lib/active_object/numeric.rb', line 72 def decades_in_seconds self * DECADE end |
#decagrams_in_grams ⇒ Object Also known as: decagram_in_grams
78 79 80 |
# File 'lib/active_object/numeric.rb', line 78 def decagrams_in_grams self * DECA end |
#decameters_in_meters ⇒ Object Also known as: decameter_in_meters
84 85 86 |
# File 'lib/active_object/numeric.rb', line 84 def decameters_in_meters self * DECA end |
#decigrams_in_grams ⇒ Object Also known as: decigram_in_grams
90 91 92 |
# File 'lib/active_object/numeric.rb', line 90 def decigrams_in_grams self * DECI end |
#decimeters_in_meters ⇒ Object Also known as: decimeter_in_meters
96 97 98 |
# File 'lib/active_object/numeric.rb', line 96 def decimeters_in_meters self * DECI end |
#distance(n) ⇒ Object
102 103 104 |
# File 'lib/active_object/numeric.rb', line 102 def distance(n) (self - n).abs end |
#divide(n) ⇒ Object
106 107 108 |
# File 'lib/active_object/numeric.rb', line 106 def divide(n) self / n end |
#exabytes_in_bytes ⇒ Object Also known as: exabyte_in_bytes
110 111 112 |
# File 'lib/active_object/numeric.rb', line 110 def exabytes_in_bytes self * EXABYTE end |
#feet_in_inches ⇒ Object Also known as: foot_in_inches
116 117 118 |
# File 'lib/active_object/numeric.rb', line 116 def feet_in_inches self * FEET end |
#gigabytes_in_bytes ⇒ Object Also known as: gigabyte_in_bytes
122 123 124 |
# File 'lib/active_object/numeric.rb', line 122 def gigabytes_in_bytes self * GIGABYTE end |
#grams_in_grams ⇒ Object Also known as: gram_in_grams
128 129 130 |
# File 'lib/active_object/numeric.rb', line 128 def grams_in_grams self end |
#greater_than?(n) ⇒ Boolean
134 135 136 |
# File 'lib/active_object/numeric.rb', line 134 def greater_than?(n) self > n end |
#greater_than_or_equal_to?(n) ⇒ Boolean
138 139 140 |
# File 'lib/active_object/numeric.rb', line 138 def greater_than_or_equal_to?(n) self >= n end |
#hectograms_in_grams ⇒ Object Also known as: hectogram_in_grams
142 143 144 |
# File 'lib/active_object/numeric.rb', line 142 def hectograms_in_grams self * HECTO end |
#hectometers_in_meters ⇒ Object Also known as: hectometer_in_meters
148 149 150 |
# File 'lib/active_object/numeric.rb', line 148 def hectometers_in_meters self * HECTO end |
#hours_in_seconds ⇒ Object Also known as: hour_in_seconds
154 155 156 |
# File 'lib/active_object/numeric.rb', line 154 def hours_in_seconds self * HOUR end |
#inches_in_inches ⇒ Object Also known as: inch_in_inches
160 161 162 |
# File 'lib/active_object/numeric.rb', line 160 def inches_in_inches self end |
#inside?(start, finish) ⇒ Boolean
166 167 168 |
# File 'lib/active_object/numeric.rb', line 166 def inside?(start, finish) (start < self) && (self < finish) end |
#kilobytes_in_bytes ⇒ Object Also known as: kilobyte_in_bytes
170 171 172 |
# File 'lib/active_object/numeric.rb', line 170 def kilobytes_in_bytes self * KILOBYTE end |
#kilograms_in_grams ⇒ Object Also known as: kilogram_in_grams
182 183 184 |
# File 'lib/active_object/numeric.rb', line 182 def kilograms_in_grams self * KILO end |
#kilometers_in_meters ⇒ Object Also known as: kilometer_in_meters
176 177 178 |
# File 'lib/active_object/numeric.rb', line 176 def kilometers_in_meters self * KILO end |
#less_than?(n) ⇒ Boolean
188 189 190 |
# File 'lib/active_object/numeric.rb', line 188 def less_than?(n) self < n end |
#less_than_or_equal_to?(n) ⇒ Boolean
192 193 194 |
# File 'lib/active_object/numeric.rb', line 192 def less_than_or_equal_to?(n) self <= n end |
#megabytes_in_bytes ⇒ Object Also known as: megabyte_in_bytes
202 203 204 |
# File 'lib/active_object/numeric.rb', line 202 def megabytes_in_bytes self * MEGABYTE end |
#meters_in_meters ⇒ Object Also known as: meter_in_meters
208 209 210 |
# File 'lib/active_object/numeric.rb', line 208 def meters_in_meters self end |
#metric_tons_in_grams ⇒ Object Also known as: metric_ton_in_grams
196 197 198 |
# File 'lib/active_object/numeric.rb', line 196 def metric_tons_in_grams self * METRIC_TON end |
#miles_in_inches ⇒ Object Also known as: mile_in_inches
214 215 216 |
# File 'lib/active_object/numeric.rb', line 214 def miles_in_inches self * MILE end |
#millenniums_in_seconds ⇒ Object Also known as: millennium_in_seconds
220 221 222 |
# File 'lib/active_object/numeric.rb', line 220 def millenniums_in_seconds self * MILLENNIUM end |
#milligrams_in_grams ⇒ Object Also known as: milligram_in_grams
226 227 228 |
# File 'lib/active_object/numeric.rb', line 226 def milligrams_in_grams self * MILLI end |
#millimeters_in_meters ⇒ Object Also known as: millimeter_in_meters
232 233 234 |
# File 'lib/active_object/numeric.rb', line 232 def millimeters_in_meters self * MILLI end |
#minutes_in_seconds ⇒ Object Also known as: minute_in_seconds
238 239 240 |
# File 'lib/active_object/numeric.rb', line 238 def minutes_in_seconds self * MINUTE end |
#multiple_of?(number) ⇒ Boolean
249 250 251 |
# File 'lib/active_object/numeric.rb', line 249 def multiple_of?(number) number != 0 ? modulo(number).zero? : zero? end |
#multiply(n) ⇒ Object
244 245 246 |
# File 'lib/active_object/numeric.rb', line 244 def multiply(n) self * n end |
#nautical_miles_in_inches ⇒ Object Also known as: nautical_mile_in_inches
254 255 256 |
# File 'lib/active_object/numeric.rb', line 254 def nautical_miles_in_inches self * NAUTICAL_MILE end |
#negative? ⇒ Boolean
260 261 262 |
# File 'lib/active_object/numeric.rb', line 260 def negative? self < 0 end |
#ordinal ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/active_object/numeric.rb', line 265 def ordinal abs_number = abs if (11..13).cover?(abs_number % 100) 'th'.freeze else case abs_number % 10 when 1; 'st'.freeze when 2; 'nd'.freeze when 3; 'rd'.freeze else 'th'.freeze end end end |
#ordinalize ⇒ Object
282 283 284 |
# File 'lib/active_object/numeric.rb', line 282 def ordinalize "#{self}#{self.ordinal}" end |
#ounces_in_ounces ⇒ Object Also known as: ounce_in_ounces
287 288 289 |
# File 'lib/active_object/numeric.rb', line 287 def ounces_in_ounces self end |
#outside?(start, finish) ⇒ Boolean
293 294 295 |
# File 'lib/active_object/numeric.rb', line 293 def outside?(start, finish) (self < start) || (finish < self) end |
#pad(options = {}) ⇒ Object
297 298 299 300 301 302 |
# File 'lib/active_object/numeric.rb', line 297 def pad(={}) pad_number = .fetch(:pad_number, 0) precision = .fetch(:precision, 3) to_s.rjust(precision, pad_number.to_s) end |
#pad_precision(options = {}) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/active_object/numeric.rb', line 304 def pad_precision(={}) pad_number = .fetch(:pad_number, 0) precision = .fetch(:precision, 2) separator = .fetch(:separator, '.'.freeze) string = to_s string << separator unless string.include?(separator) ljust_count = string.split(separator).first.length ljust_count += (string.count(separator) + precision) if precision > 0 num_count = string.length ljust_count >= num_count ? string.ljust(ljust_count, pad_number.to_s) : string[0..(ljust_count - 1)] end |
#petabytes_in_bytes ⇒ Object Also known as: petabyte_in_bytes
317 318 319 |
# File 'lib/active_object/numeric.rb', line 317 def petabytes_in_bytes self * PETABYTE end |
#positive? ⇒ Boolean
323 324 325 |
# File 'lib/active_object/numeric.rb', line 323 def positive? self > 0 end |
#pounds_in_ounces ⇒ Object Also known as: pound_in_ounces
327 328 329 |
# File 'lib/active_object/numeric.rb', line 327 def pounds_in_ounces self * POUND end |
#power(n) ⇒ Object
333 334 335 |
# File 'lib/active_object/numeric.rb', line 333 def power(n) self ** n end |
#root(n) ⇒ Object
337 338 339 |
# File 'lib/active_object/numeric.rb', line 337 def root(n) self ** (1.0 / n) end |
#seconds_in_seconds ⇒ Object Also known as: second_in_seconds
341 342 343 |
# File 'lib/active_object/numeric.rb', line 341 def seconds_in_seconds self end |
#stones_in_ounces ⇒ Object Also known as: stone_in_ounces
347 348 349 |
# File 'lib/active_object/numeric.rb', line 347 def stones_in_ounces self * STONE end |
#subtract(n) ⇒ Object
353 354 355 |
# File 'lib/active_object/numeric.rb', line 353 def subtract(n) self - n end |
#terabytes_in_bytes ⇒ Object Also known as: terabyte_in_bytes
357 358 359 |
# File 'lib/active_object/numeric.rb', line 357 def terabytes_in_bytes self * TERABYTE end |
#to_byte(from, to) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/active_object/numeric.rb', line 363 def to_byte(from, to) valid_keys = [ :byte, :bytes, :kilobyte, :kilobytes, :megabyte, :megabytes, :gigabyte, :gigabytes, :terabyte, :terabytes, :petabyte, :petabytes, :exabyte, :exabytes ].freeze unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}" end to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f end |
#to_currency(options = {}) ⇒ Object
382 383 384 385 386 |
# File 'lib/active_object/numeric.rb', line 382 def to_currency(={}) unit = .fetch(:unit, '$'.freeze) "#{unit}#{pad_precision(options.only(:precision))}" end |
#to_length(from, to) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/active_object/numeric.rb', line 388 def to_length(from, to) valid_keys = [ :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter, :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers, :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles ].freeze unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}" end case to when from self when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter, :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers if valid_keys.first(14).include?(from) to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f else to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f) end when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles if valid_keys.first(14).include?(from) to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f) else to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f end end end |
#to_mass(from, to) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/active_object/numeric.rb', line 427 def to_mass(from, to) valid_keys = [ :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams, :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton, :metric_tons, :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons ].freeze unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}" end case to when from self when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams, :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton, :metric_tons if valid_keys.first(16).include?(from) to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f else to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f) end when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons if valid_keys.first(16).include?(from) to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f) else to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f end end end |
#to_nearest_value(values = []) ⇒ Object
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/active_object/numeric.rb', line 466 def to_nearest_value(values=[]) return(self) if values.length.zero? value = values.first difference = (self - value).abs values.each do |v| if (self - v).abs < difference difference = (self - v).abs value = v end end value end |
#to_percentage(options = {}) ⇒ Object
482 483 484 485 486 |
# File 'lib/active_object/numeric.rb', line 482 def to_percentage(={}) unit = .fetch(:unit, '%'.freeze) "#{pad_precision(options.only(:precision))}#{unit}" end |
#to_temperature(from, to) ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
# File 'lib/active_object/numeric.rb', line 488 def to_temperature(from, to) valid_keys = [:celsius, :fahrenheit, :kelvin].freeze unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}" end number = to_f case to when from self when :celsius from == :kelvin ? (number - 273.15) : ((number - 32.0) * 5.0 / 9.0) when :fahrenheit from == :kelvin ? (1.8 * (number - 273.15) + 32.0) : ((number * 9.0 / 5.0) + 32.0) when :kelvin from == :celsius ? (number + 273.15) : (((number - 32.0) * 5.0 / 9.0) + 273.15) end end |
#to_time(from, to) ⇒ Object
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/active_object/numeric.rb', line 510 def to_time(from, to) valid_keys = [ :second, :seconds, :minute, :minutes, :hour, :hours, :day, :days, :week, :weeks, :year, :years, :decade, :decades, :century, :centuries, :millennium, :millenniums ].freeze unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', '.freeze)}" end to_f * 1.send("#{from}_in_seconds").to_f / 1.send("#{to}_in_seconds").to_f end |
#tons_in_ounces ⇒ Object Also known as: ton_in_ounces
531 532 533 |
# File 'lib/active_object/numeric.rb', line 531 def tons_in_ounces self * TON end |
#weeks_in_seconds ⇒ Object Also known as: week_in_seconds
537 538 539 |
# File 'lib/active_object/numeric.rb', line 537 def weeks_in_seconds self * WEEK end |
#within?(number, epsilon = 0.01) ⇒ Boolean
543 544 545 546 547 548 549 |
# File 'lib/active_object/numeric.rb', line 543 def within?(number, epsilon=0.01) return(self == number) if epsilon.zero? a, b = to_f, number.to_f (a.zero? || b.zero?) ? ((a - b).abs < epsilon) : ((a / b - 1).abs < epsilon) end |
#yards_in_inches ⇒ Object Also known as: yard_in_inches
551 552 553 |
# File 'lib/active_object/numeric.rb', line 551 def yards_in_inches self * YARD end |
#years_in_seconds ⇒ Object Also known as: year_in_seconds
557 558 559 |
# File 'lib/active_object/numeric.rb', line 557 def years_in_seconds self * YEAR end |