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)
- #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)
- #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)
- #kilobytes_in_bytes ⇒ Object (also: #kilobyte_in_bytes)
- #kilograms_in_grams ⇒ Object (also: #kilogram_in_grams)
- #kilometers_in_meters ⇒ Object (also: #kilometer_in_meters)
- #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)
- #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)
- #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 |
#divide(n) ⇒ Object
102 103 104 |
# File 'lib/active_object/numeric.rb', line 102 def divide(n) self / n end |
#exabytes_in_bytes ⇒ Object Also known as: exabyte_in_bytes
106 107 108 |
# File 'lib/active_object/numeric.rb', line 106 def exabytes_in_bytes self * EXABYTE end |
#feet_in_inches ⇒ Object Also known as: foot_in_inches
112 113 114 |
# File 'lib/active_object/numeric.rb', line 112 def feet_in_inches self * FEET end |
#gigabytes_in_bytes ⇒ Object Also known as: gigabyte_in_bytes
118 119 120 |
# File 'lib/active_object/numeric.rb', line 118 def gigabytes_in_bytes self * GIGABYTE end |
#grams_in_grams ⇒ Object Also known as: gram_in_grams
124 125 126 |
# File 'lib/active_object/numeric.rb', line 124 def grams_in_grams self end |
#hectograms_in_grams ⇒ Object Also known as: hectogram_in_grams
130 131 132 |
# File 'lib/active_object/numeric.rb', line 130 def hectograms_in_grams self * HECTO end |
#hectometers_in_meters ⇒ Object Also known as: hectometer_in_meters
136 137 138 |
# File 'lib/active_object/numeric.rb', line 136 def hectometers_in_meters self * HECTO end |
#hours_in_seconds ⇒ Object Also known as: hour_in_seconds
142 143 144 |
# File 'lib/active_object/numeric.rb', line 142 def hours_in_seconds self * HOUR end |
#inches_in_inches ⇒ Object Also known as: inch_in_inches
148 149 150 |
# File 'lib/active_object/numeric.rb', line 148 def inches_in_inches self end |
#kilobytes_in_bytes ⇒ Object Also known as: kilobyte_in_bytes
154 155 156 |
# File 'lib/active_object/numeric.rb', line 154 def kilobytes_in_bytes self * KILOBYTE end |
#kilograms_in_grams ⇒ Object Also known as: kilogram_in_grams
166 167 168 |
# File 'lib/active_object/numeric.rb', line 166 def kilograms_in_grams self * KILO end |
#kilometers_in_meters ⇒ Object Also known as: kilometer_in_meters
160 161 162 |
# File 'lib/active_object/numeric.rb', line 160 def kilometers_in_meters self * KILO end |
#megabytes_in_bytes ⇒ Object Also known as: megabyte_in_bytes
178 179 180 |
# File 'lib/active_object/numeric.rb', line 178 def megabytes_in_bytes self * MEGABYTE end |
#meters_in_meters ⇒ Object Also known as: meter_in_meters
184 185 186 |
# File 'lib/active_object/numeric.rb', line 184 def meters_in_meters self end |
#metric_tons_in_grams ⇒ Object Also known as: metric_ton_in_grams
172 173 174 |
# File 'lib/active_object/numeric.rb', line 172 def metric_tons_in_grams self * METRIC_TON end |
#miles_in_inches ⇒ Object Also known as: mile_in_inches
190 191 192 |
# File 'lib/active_object/numeric.rb', line 190 def miles_in_inches self * MILE end |
#millenniums_in_seconds ⇒ Object Also known as: millennium_in_seconds
196 197 198 |
# File 'lib/active_object/numeric.rb', line 196 def millenniums_in_seconds self * MILLENNIUM end |
#milligrams_in_grams ⇒ Object Also known as: milligram_in_grams
202 203 204 |
# File 'lib/active_object/numeric.rb', line 202 def milligrams_in_grams self * MILLI end |
#millimeters_in_meters ⇒ Object Also known as: millimeter_in_meters
208 209 210 |
# File 'lib/active_object/numeric.rb', line 208 def millimeters_in_meters self * MILLI end |
#minutes_in_seconds ⇒ Object Also known as: minute_in_seconds
214 215 216 |
# File 'lib/active_object/numeric.rb', line 214 def minutes_in_seconds self * MINUTE end |
#multiple_of?(number) ⇒ Boolean
225 226 227 |
# File 'lib/active_object/numeric.rb', line 225 def multiple_of?(number) number != 0 ? modulo(number).zero? : zero? end |
#multiply(n) ⇒ Object
220 221 222 |
# File 'lib/active_object/numeric.rb', line 220 def multiply(n) self * n end |
#nautical_miles_in_inches ⇒ Object Also known as: nautical_mile_in_inches
230 231 232 |
# File 'lib/active_object/numeric.rb', line 230 def nautical_miles_in_inches self * NAUTICAL_MILE end |
#negative? ⇒ Boolean
236 237 238 |
# File 'lib/active_object/numeric.rb', line 236 def negative? self < 0 end |
#ordinal ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/active_object/numeric.rb', line 241 def ordinal abs_number = abs if (11..13).include?(abs_number % 100) "th" else case abs_number % 10 when 1; "st" when 2; "nd" when 3; "rd" else "th" end end end |
#ordinalize ⇒ Object
258 259 260 |
# File 'lib/active_object/numeric.rb', line 258 def ordinalize "#{self}#{self.ordinal}" end |
#ounces_in_ounces ⇒ Object Also known as: ounce_in_ounces
263 264 265 |
# File 'lib/active_object/numeric.rb', line 263 def ounces_in_ounces self end |
#pad(options = {}) ⇒ Object
269 270 271 272 273 274 |
# File 'lib/active_object/numeric.rb', line 269 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
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/active_object/numeric.rb', line 276 def pad_precision(={}) pad_number = .fetch(:pad_number, 0) precision = .fetch(:precision, 2) separator = .fetch(:separator, ".") string = to_s string << separator unless string.include?(separator) ljust_count = string.split(separator).first.size ljust_count += (string.count(separator) + precision) if precision > 0 num_count = string.size 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
289 290 291 |
# File 'lib/active_object/numeric.rb', line 289 def petabytes_in_bytes self * PETABYTE end |
#positive? ⇒ Boolean
295 296 297 |
# File 'lib/active_object/numeric.rb', line 295 def positive? self > 0 end |
#pounds_in_ounces ⇒ Object Also known as: pound_in_ounces
299 300 301 |
# File 'lib/active_object/numeric.rb', line 299 def pounds_in_ounces self * POUND end |
#power(n) ⇒ Object
305 306 307 |
# File 'lib/active_object/numeric.rb', line 305 def power(n) self ** n end |
#root(n) ⇒ Object
309 310 311 |
# File 'lib/active_object/numeric.rb', line 309 def root(n) self ** (1.0 / n) end |
#seconds_in_seconds ⇒ Object Also known as: second_in_seconds
313 314 315 |
# File 'lib/active_object/numeric.rb', line 313 def seconds_in_seconds self end |
#stones_in_ounces ⇒ Object Also known as: stone_in_ounces
319 320 321 |
# File 'lib/active_object/numeric.rb', line 319 def stones_in_ounces self * STONE end |
#subtract(n) ⇒ Object
325 326 327 |
# File 'lib/active_object/numeric.rb', line 325 def subtract(n) self - n end |
#terabytes_in_bytes ⇒ Object Also known as: terabyte_in_bytes
329 330 331 |
# File 'lib/active_object/numeric.rb', line 329 def terabytes_in_bytes self * TERABYTE end |
#to_byte(from, to) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/active_object/numeric.rb', line 335 def to_byte(from, to) valid_keys = [ :byte, :bytes, :kilobyte, :kilobytes, :megabyte, :megabytes, :gigabyte, :gigabytes, :terabyte, :terabytes, :petabyte, :petabytes, :exabyte, :exabytes ] unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): form: #{from.inspect} and to: #{to.inspect}, Valid keys are: #{valid_keys.map(&:inspect).join(', ')}" end to_f * 1.send("#{from}_in_bytes") / 1.send("#{to}_in_bytes") end |
#to_currency(options = {}) ⇒ Object
354 355 356 357 358 |
# File 'lib/active_object/numeric.rb', line 354 def to_currency(={}) unit = .fetch(:unit, "$") "#{unit}#{pad_precision(options.only(:precision))}" end |
#to_length(from, to) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/active_object/numeric.rb', line 360 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 ] unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): form: #{from.inspect} and to: #{to.inspect}, Valid keys are: #{valid_keys.map(&:inspect).join(', ')}" 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") / 1.send("#{to}_in_meters") else to_f * ((1.send("#{from}_in_inches") * 0.0254) / 1.send("#{to}_in_meters")) 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") * 39.3701) / 1.send("#{to}_in_inches")) else to_f * 1.send("#{from}_in_inches") / 1.send("#{to}_in_inches") end end end |
#to_mass(from, to) ⇒ Object
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 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/active_object/numeric.rb', line 399 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 ] unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): form: #{from.inspect} and to: #{to.inspect}, Valid keys are: #{valid_keys.map(&:inspect).join(', ')}" 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") / 1.send("#{to}_in_grams") else to_f * ((1.send("#{from}_in_ounces") * 28.3495) / 1.send("#{to}_in_grams")) 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) / 1.send("#{to}_in_ounces")) else to_f * 1.send("#{from}_in_ounces") / 1.send("#{to}_in_ounces") end end end |
#to_nearest_value(values = []) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/active_object/numeric.rb', line 438 def to_nearest_value(values=[]) return(self) if values.size.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
452 453 454 455 456 |
# File 'lib/active_object/numeric.rb', line 452 def to_percentage(={}) unit = .fetch(:unit, "%") "#{pad_precision(options.only(:precision))}#{unit}" end |
#to_temperature(from, to) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/active_object/numeric.rb', line 458 def to_temperature(from, to) valid_keys = [:celsius, :fahrenheit, :kelvin] unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): form: #{from.inspect} and to: #{to.inspect}, Valid keys are: #{valid_keys.map(&:inspect).join(', ')}" end case to when from self when :celsius from == :kelvin ? (to_f - 273.15) : ((to_f - 32.0) * 5.0 / 9.0) when :fahrenheit from == :kelvin ? (1.8 * (to_f - 273.15) + 32.0) : ((to_f * 9.0 / 5.0) + 32.0) when :kelvin from == :celsius ? (to_f + 273.15) : (((to_f - 32.0) * 5.0 / 9.0) + 273.15) end end |
#to_time(from, to) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/active_object/numeric.rb', line 478 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 ] unless valid_keys.include?(from) && valid_keys.include?(to) raise ArgumentError, "Unknown key(s): form: #{from.inspect} and to: #{to.inspect}, Valid keys are: #{valid_keys.map(&:inspect).join(', ')}" end to_f * 1.send("#{from}_in_seconds") / 1.send("#{to}_in_seconds") end |
#tons_in_ounces ⇒ Object Also known as: ton_in_ounces
499 500 501 |
# File 'lib/active_object/numeric.rb', line 499 def tons_in_ounces self * TON end |
#weeks_in_seconds ⇒ Object Also known as: week_in_seconds
505 506 507 |
# File 'lib/active_object/numeric.rb', line 505 def weeks_in_seconds self * WEEK end |
#yards_in_inches ⇒ Object Also known as: yard_in_inches
511 512 513 |
# File 'lib/active_object/numeric.rb', line 511 def yards_in_inches self * YARD end |
#years_in_seconds ⇒ Object Also known as: year_in_seconds
517 518 519 |
# File 'lib/active_object/numeric.rb', line 517 def years_in_seconds self * YEAR end |