How to define a petabyte constant in Ruby

1 Answer

0 votes
# Ruby integers have arbitrary precision.
# This means they can grow as large as needed, limited only by memory.
#
# A petabyte fits easily:
#
#   1 PiB = 2**50  = 1,125,899,906,842,624 bytes
#   1 PB  = 10**15 = 1,000,000,000,000,000 bytes
#
# Ruby constants are simply variables with uppercase names.

# Binary petabyte (pebibyte), using a bit shift.
# 1 << 50 = 2**50 bytes.
PETABYTE_PIB = 1 << 50 # not a constant - just don't change it 

# Decimal petabyte (SI petabyte), using a readable numeric literal.
PETABYTE_PB = 1_000_000_000_000_000

puts "1 PiB = #{PETABYTE_PIB} bytes"
puts "1 PB  = #{PETABYTE_PB} bytes"


# run:
#
# 1 PiB = 1125899906842624 bytes
# 1 PB  = 1000000000000000 bytes
#

 



answered May 3 by avibootz
...