Ruby takes the doubled amount of RAM it should -
i trying things out homeserver , wrote little ruby program fills ram given amount. have halve amount of bytes want put ram. missing here or bug?
here code:
class ram def initialize @b = '' end def fill_ram(size) puts 'choose if want set size in bytes, megabytes or gigabytes.' answer = '' valid = ['bytes', 'megabytes', 'gigabytes'] until valid.include?(answer) answer = gets.chomp.downcase if answer == 'bytes' size = size * 0.5 elsif answer == 'megabytes' size = size * 1024 * 1024 * 0.5 elsif answer == 'gigabytes' size = size * 1024 * 1024 * 1024 * 0.5 else puts 'please choose between bytes, megabytes or gigabyte.' end end size1 = size if @b.bytesize != 0 size1 = size + @b.bytesize end until @b.bytesize == size1 @b << '0' * size end size = 0 end def clear_ram exit end def read_ram puts 'at moment program fills ' + @b.bytesize.to_s + ' bytes of ram' end end
just imagine "* 0.5"
@ each line wouldn't there.
i did test in irb , created new ram object , filled 1000 megabytes of data. in case filled ram 2000 megabytes of data, did add times 0.5
each line, can't solution.
when run get:
choose if want set size in bytes, megabytes or gigabytes. bytes @ moment program fills 512 bytes of ram
i think problem missing check encoding.
i ran test in us-ascii (one character = 1 byte).
if run in utf-16 have explanation problem.
can try following code check encoding:
p encoding.default_internal p encoding.default_external
after reading comment:
the result of script depends on parameter of ram.fill_ram
. how start script - , how call ram.fill_ram
?
please provide full code.
i called example with
r = ram.new r.fill_ram(1024) r.read_ram
Comments
Post a Comment