-1

Alright, so what I have is a ruby file that takes an input, and writes it to another ruby file. I do not want to write it as a text file, because I am trying to insert this item into a Hash that can later be accessed in another run of the program, which can only be achieved by writing the info to a text file or another ruby file. In this case I want to write it into another ruby file.Here's the first file:

test_text=gets.chomp
to_write_to=File.open("rubylib.rb", "a")
test_text="hobby => #{test_test},"
to_write_to.puts test_text

This inserts the given info at the BOTTOM of the page. The other file is this: (rubylib.rb)

user_info={
  "name" => "bob",,
  "favorite_color" => "red"

}

I have a threefold question:

1) Is it possible to add test_text to the hash BEFORE the closing bracket?

2) using this method, will the rubylib.rb file, when run, parse the added text as code, or something else?

3)is there a better way to do this?

What I am trying to do is actually physically write the new data to the Hash so that it is still there the next time the file is run, to store data about the user. Because if I add it the normal way, it will be lost the next time the file is run. Is there a way to store data between runs of a ruby file without writing to a text file?

I've done the best I can to give you the info you need and explain the situation as best I can. If you need clarification or more info, please leave a comment and I'll try and get back to you by commenting on that.

Thanks for the help

  • What you're trying to do is extremely dangerous -- it is possible for a user to execute arbitrary ruby code by writing the output of gets to a ruby file. – hjing May 05 '15 at 00:26
  • Why are you storing information on disk? That won't scale at all. – the Tin Man May 05 '15 at 01:01
  • @theTinMan stop editing it I do not wish for you to change it. I have it the way I want it. –  May 05 '15 at 19:17
  • Welcome to Stack Overflow. Perhaps you missed the part of the introduction that said this is a community edited site. Stack Overflow aims to be a Wikipedia-like reference site. As such, there are certain goals for the content, such as proper grammar use, common-style, and clear and concise writing. Everyone here is responsible for ensuring this. Your use of terminology is wrong and confusing. I cleaned that up to make it more clear what you are trying to do in an attempt to get you more help. If you insist on reverting it then please fix the problems. – the Tin Man May 05 '15 at 19:22

2 Answers2

1

You should use YAML for this.

Here's how you could create a .yml file with the data you used in your example:

require "yaml"
user_info = { "name" => "bob", "favorite_color" => "red" }
File.write("user_info.yml", user_info.to_yaml)

This creates a file that looks like this:

---
name: bob
favorite_color: red

On a subsequent execution of your program, you can load the .yml file and you'll get back the same Hash that you started with:

user_info = YAML.load_file("user_info.yml")
# => { "name" => "bob", "favorite_color" => "red" }

And you can add new items to the Hash and save it again:

user_info["hobby"] = "fishing"
File.write("user_info.yml", user_info.to_yaml)

Now the file has these contents:

---
name: bob
favorite_color: red
hobby: fishing
Matt Brictson
  • 10,904
  • 1
  • 38
  • 43
  • 2
    Instead of `YAML.dump(user_info, open("user_info.yml", "w"))` use `File.write("user_info.yml", user_info.to_yaml)`. – the Tin Man May 05 '15 at 00:39
  • Thanks @theTinMan this is why I love Stack Overflow! Learned something new again today. :) I'll update my answer. – Matt Brictson May 05 '15 at 00:49
  • @MattBrictson thank you so much! I'll try this out soon and hopefully it'll work.where do i store the .yml file? with the rest of the files? –  May 05 '15 at 11:51
  • 1
    If you are writing a command-line script to be used on UNIX, then traditionally user preferences would be stored in a "dotfile" in the user's home directory. `File.expand_path("~/.myscriptname.yml")` – Matt Brictson May 05 '15 at 14:01
  • @MattBrictson Not sure what you mean by that but thanks. What's the YAML documentation? I should take a closer look –  May 05 '15 at 19:06
  • 1
    The wikipedia link in my answer is a good overview of the YAML file format. The Ruby API for YAML is implemented internally by a library called Psych. You can find its documentation here: http://www.rubydoc.info/gems/psych/2.0.13/Psych . In practice, `YAML.load`, `YAML.load_file`, and `to_yaml` will probably cover all of your needs. – Matt Brictson May 05 '15 at 19:23
  • @MattBrictson how do i download the yaml gem or whatever it is? i trie gem install yaml and gem install Psych but nothing worked –  May 05 '15 at 23:30
  • how can i close the YAML file using the yaml gem? –  May 19 '15 at 23:19
0

Use a database, even SQLite, and it'll let you store data for multiple sessions without any sort of encoding. Writing to a file as you are is really not scalable or practical. You'll slam into some real problems quickly with it.

I'd recommend looking at Sequel and its associated documentation for how to easily work with databases. That's a much more scalable approach and will save you a lot of headaches as you grow your code.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • almighty ill check it out. Can i install it using the standard gem install and then type sequel? –  May 05 '15 at 19:11