Pixelz Pixelz - 1 year ago 70
Ruby Question

Premature ruby exit on RAM intensive program

I'm trying to make my Ruby file print every possible IP there can be.

However, it exits prematurely with no error.

require 'ipaddress' #Is Installed

#Changes How Much RAM Is Taken Every Request (Tested And Removed, Not The Problem)

#Exits After Here

ip = IPAddress "" #First Range
ip.to('') #End Range

#Exits Before This Point

out_file = File.new("out.txt", "w") #Create & OpenFile
out_file.puts("#{ip}") #Output Variable To File
out_file.close #Save And Exit

RAM is not a problem, due to having 8Gb worth, and the code exiting before hitting my RAM limit.

Whats causing the program to shutdown?

Answer Source

I think you're on the right track but you should use the gem in a slightly different way:

ip = IPAddress ""

File.open("out.txt", "w") do |file|
  ip.each do |ip_address|
    file.puts ip_address


This used a finite (and reasonable) amount of RAM on my laptop when I tested it. However, I did not write out to a file, just to the console. Hopefully you are prepared to deal with the size of file this will produce.