0

I am creating a program in Aptana (ruby IDE) and doing a ton of math on a ruby array and often adding more math, creating variables from the results. Doing more math from there, ect. However each time I run my script it first has to re read my file (a few megabytes) and put it into the array. Which takes time.

Now I'm really new to programming so I'm still lacking some understanding to how programming in general works. But how would I fix this problem?

If I were to guess how this would be done. I would say maybe split my program up into 2 files and run the array one without it stopping. And the other ruby file would use the array file. I don't know how that would be done though.

Am I close to the solution?

Also I hope you noticed that I said I'm developing in aptana. I haven't even tried running my scripts normally. I'm assuming that I would have the exact same problem there.

user1594138
  • 1,003
  • 3
  • 11
  • 20
  • Uhm its really interesting to not get an answer yet. Surely this is some super easy stuff. I need my ruby program to function as a program, and not a script. – user1594138 Aug 16 '12 at 19:25
  • 1
    Sounds like you need a database of some sort. – mu is too short Aug 16 '12 at 19:33
  • 1
    It's really interesting to not get an answer in 12 minutes? You should have a thrilling experience with answers to your question then. You need to explain your code and data better. Is the data in a separate file or your source code? If its one file, move the data into a CSV or YAML file and read it. Can you reduce your data set to a small sampling so your load time is fast, get your code working, then load the full data set? And, Aptana is probably not the problem, it's how you're writing the code and testing it. – the Tin Man Aug 16 '12 at 19:38
  • My data is in a csv file. Then it loads to an array. If we forget the editing part and think about just ruby, I need to have my ruby script continually get additional data from the web and push it into the array. And doing this forever basically. So like I said I need to create a program, not a script. But that has to be possible and very easy, so its just my while editing question that isn't possible or at least troubling. Right? – user1594138 Aug 16 '12 at 19:56
  • @muistooshort wow I think you might be right. I'll check into that. Thanks! – user1594138 Aug 16 '12 at 20:19
  • You could use [`Marshal`](http://ruby-doc.org/core-1.9.3/Marshal.html) to cache a copy of the loaded data but you'd want to watch out for version issues. The standard [dbm](http://ruby-doc.org/stdlib-1.9.3/libdoc/dbm/rdoc/index.html) library might be another option depending on the nature of your data. SQLite is yet another option. – mu is too short Aug 16 '12 at 20:48

2 Answers2

0

You could probably do this using irb. That is, you start the ruby interpreter and, provided your array itself doesn't change, you load the array once. After that, you can run your operations against that array.

You can capture changes permanently by copying your code to a file as you go, or you can write your algorithms/math into a script file and repeatedly load the file into the interpreter to get access to the new or rewritten methods in the interpreter.

"loading documents on IRB ruby" might help you see how this is done.

Community
  • 1
  • 1
philosodad
  • 1,808
  • 14
  • 24
0

Any approach where you're keeping a process running and reading/writing to it from another process is going to be a whole lot more complicated than just reading in the data when the script starts and writing it out when the script finishes. Managing multiple processes is not really worth the trouble just to avoid reading and writing a few megabytes of data.

I'm not sure how you're currently writing out and reading in the file, but one good option would be to use Ruby's built-in YAML support.

Serialization:

require 'yaml'

arr = [1, 2, 3, 4]

arr.to_yaml(File.new('test.yml', 'w'))

Deserialization:

require 'yaml'

arr = YAML::load(File.new('test.yml', 'r'))

p arr

# => [1, 2, 3, 4]

Obviously you would still have to load the array every time, but at least you would not have to repeat all of your mathematical operations for the already-calculated values in the array.

Peter Roe
  • 446
  • 3
  • 6