C'è un limite. A String
può essere 2**31 - 1
(e di conseguenza 2**63 - 1
su 64 bit rubino). Si può vedere il limite con:
>> s = String.new("1" * (2**32))
RangeError: bignum too big to convert into `long'
from (irb):3:in `*'
from (irb):3
>> s = String.new("1" * (2**31))
RangeError: bignum too big to convert into `long'
from (irb):4:in `*'
from (irb):4
Detto questo, mentre si potrebbe tentare di allocare una stringa che grande è probabile che fallire (almeno su un sistema a 32 bit come in genere la quantità massima di memoria di un processo può allocare è compreso tra 2,5 e 3 GB e una stringa 2**31 - 1
lunghezza è quasi 2GB per sé) Come visto:.
>> "1" * (2**30)
NoMemoryError: failed to allocate memory
from /usr/lib/ruby/1.8/irb.rb:310:in `inspect'
from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:244:in `each_top_level_statement'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in `loop'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in `each_top_level_statement'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in `catch'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in `each_top_level_statement'
from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:71:in `start'
from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
from /usr/lib/ruby/1.8/irb.rb:70:in `start'
from /usr/bin/irb:13
Maybe IRB bug!!
non ritengo v'è alcun modo per catturare il NoMemoryError
.
Aggiornato per riflettere il commento da sepp2k
In realtà può essere grande come '2 ** 31 - 1' (e di conseguenza' 2 ** 63 - 1' a 64 bit rubino). – sepp2k
I caratteri '2 ** 31 - 1' o' 2 ** 31 -1 'byte? Inoltre, non sta rilevando un 'NoMemoryError' una questione di dire' rescue Exception' piuttosto che 'rescue' (quest'ultimo salva solo eccezioni relativamente lievi)? (Per non parlare di neversaydie ...) –
'2 ** 31 - 1' byte. Includendo 'rescue Exception' si cattura 'NoMemoryError' ma sembra che non esegua effettivamente l'allocazione (dato che ritorna immediatamente). – rjk