What are the size limits of variables in Ruby?

9

I would like to know the size limit of variables of the following types:

  • String - What is the largest number of characters I can have in a single string ?
  • Integer and Float - What is the largest and smallest number that can be represented?
  • Array - How many indexes can I have in a single array ?
  • Hash - What is the largest number of pairs (key: value) I can have in a hash ?
asked by anonymous 26.05.2014 / 21:23

2 answers

12

Because sometimes it depends on your architecture (32 or 64 bits), I put the limits as defined in the language code (if any):

String:

  • 32 bits: 2 ** 31 - 1
  • 64 bits: 2 ** 63 - 1
  • Source: link

Integer:

  • Maximum: (2 ** (0.size * 8 -2) -1)
  • Minimum: - (2 ** (0.size * 8 -2))
  • Source: link

Float:

  • Maximum: Typically it is 1.7976931348623157e + 308
  • Minimum: Usually 2.2250738585072014e-308
  • Source: link

Array / Hash:

  • There is no limit set. The limit is the amount of memory available to the process.
  • Source: link
26.05.2014 / 22:14
13

String = > theoretical limit of 2 31 - 1 (32 bits) or 2 63 - 1 (64 bits). I want to see someone able to allocate a string of this size

Integer = > In Ruby's thesis can be changing the representation and have infinite values

Float = > Usually limited by architecture. In general between 1.7976931348623158e + 308 and 2.2250738585072014e-308

Array = > There is no theoretical limit. In practice you can not use nor near the limit. At 32 bits there is a total virtual memory limit of 4GB. You will not be able to create an array with 2 31 - 1 even though each element contains only and Ruby as everything else is reference, this is far from being possible. In 64 bits if you have 2 63 - 1 elements, you will need a lot underneath (certainly that volume is much larger) 250EB (Exabytes). Forget

Hash = > essentially the same embore needs even more memory. But again, do not worry so much about this limit, the practical limit comes first

    
26.05.2014 / 22:17