6 Replies - 944 Views - Last Post: 14 February 2017 - 08:27 PM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

A bit help on integrating Hashes into files

Posted 11 February 2017 - 09:17 PM

Ok, I am trying to put a hash into a file but I'm a bit confused on how to do it. In the end I will be able to have the user input a command that says view and it will give them a list of products with a format of Item # Description Price
I'm just having trouble with the first part. Do I create the hash inside of ruby and write it to the .txt file? Or does anyone know where I start? Here is what I have so far just to view what is in the txt file.

products_file = File.open('products.txt', 'r+')
products_file.each_line do |item, description, price|
  puts item, description, price
end
products_file.close



Then it prints out what I have in the .txt file and I have that in Hash format but I'm not sure if it will work that way or not. Just a bit confused on this. Here is what is in the .txt file:

products_file = {124=>["shoes",59.99], 352=>["shirt",19.99], 456=>["socks", 3.99]}

Is This A Good Question/Topic? 0
  • +

Replies To: A bit help on integrating Hashes into files

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,227
  • Joined: 14-September 07

Re: A bit help on integrating Hashes into files

Posted 12 February 2017 - 09:02 PM

The text file contains the string literal of a Ruby hash.to_s? Use eval:

file = File.open("sample_file.txt", "r")
file.each_line do |line|
  h = eval(line)
  puts h #i am now a hash
  h.each do |hash_item| #print all my hashiness
    puts hash_item
  end
  puts h[352] #print one item, notice it prints each array item on a seperate line
end



As for what to write, it really depends on who is going to read it and in what manner. Writing to a text file is simply serialization and as the programmer you can dictate how you want that data to look like. Having the output be "Item # Description Price" can be done independently of how the data is written to the flat file.

This post has been edited by KYA: 12 February 2017 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: A bit help on integrating Hashes into files

Posted 13 February 2017 - 07:27 AM

Just as a note - eval *can* work here, but that approach opens you up to arbitrary code injection as well as odd error messages.

Instead, you should be serializing and deserializing in some standard format that you can read in and out. JSON is popular: https://ruby-doc.org.../rdoc/JSON.html

You could also write your own - something like outputting your values separated by some character, then when you read, you split each line by that character and put those values into the new Hash. In general, using an existing format is better.
Was This Post Helpful? 0
  • +
  • -

#4 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: A bit help on integrating Hashes into files

Posted 13 February 2017 - 10:14 PM

Thanks for your help, I am currently working on the menu options and it looks messy but I'm testing different things out to try to get what I need through trial and error, I need help on some of it though, it is printing out something I really don't want in there but I know it has to do with the adding it in the method even if I am not telling it to print it still does, I am not sure how to fix it, here is the code and I'll also add what it is printing out and maybe someone can help, thanks in advance!

user_input = 0

def viewMenu
  products = {124=>["shoes",59.99], 352=>["shirt",19.99], 456=>["socks", 3.99]}
  puts "Item" + " " + "Description" + " " + "Price "
  puts "-" * 4 + " " + "-" * 11 + " " + "-" * 5
  products.each {|key, value| puts "#{key.to_s.ljust(4)}" + " " + " " +  " " + " " + "#{value[0].to_s.center(4)}" + " " + " " +  " " + " " + "#{value[1].to_s.rjust(4)}" }

end
menu = [     "1. View all products ",
              "2. Add a new product ",
              "3. Delete a product ",
              "4. Update a product ",
              "5. View highest priced product ",
              "6. View lowest priced product ",
              "7. View sum of all product prices ",
              "8. Exit "
]
puts menu
puts "Please Enter a Choice from 1-8 "
user_input = gets.chomp.to_i

if user_input == 1
  puts viewMenu
end


and it is printing out
1. View all products
2. Add a new product
3. Delete a product
4. Update a product
5. View highest priced product
6. View lowest priced product
7. View sum of all product prices
8. Exit
Please Enter a Choice from 1-8
1
Item Description Price
---- ----------- -----
124 shoes 59.99
352 shirt 19.99
456 socks 3.99
{124=>["shoes", 59.99], 352=>["shirt", 19.99], 456=>["socks", 3.99]} ###I don't want this line of code but I have no clue where to put it in the method that it will still recognize it but not print it...
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: A bit help on integrating Hashes into files

Posted 14 February 2017 - 06:31 AM

You're calling puts viewMenu when the user selects "1". However, the viewMenu method itself performs output. The reason that last line outputs that hash is because Ruby methods implicitly return the result of the last line. So here's what happens:

puts viewMenu -> evaluate viewMenu -> output the table you build -> return the hash -> print the hash.

Just call viewMenu instead of calling puts viewMenu.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: A bit help on integrating Hashes into files

Posted 14 February 2017 - 06:31 AM

You're calling puts viewMenu when the user selects "1". However, the viewMenu method itself performs output. The reason that last line outputs that hash is because Ruby methods implicitly return the result of the last line. So here's what happens:

puts viewMenu -> evaluate viewMenu -> output the table you build -> return the hash -> print the hash.

Just call viewMenu instead of calling puts viewMenu.
Was This Post Helpful? 1
  • +
  • -

#7 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: A bit help on integrating Hashes into files

Posted 14 February 2017 - 08:27 PM

Thank you, can't believe I didn't spot that error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1