Saving hashes to file on Ruby

前端 未结 3 657
孤街浪徒
孤街浪徒 2020-12-20 21:28

I\'m just giving my first steps in programming. I have just finished another class in Code Academy. This time I was asked to create a small movie catalog. Here is my questio

3条回答
  •  悲哀的现实
    2020-12-20 21:34

    If they're simple hashes, a YAML file may be an easy way to do it.

    require 'yaml'
    
    # write hash out as a YAML file
    movies = { Memento: 1, Primer: 4, Ishtar: 1 }
    File.write('movies.yml', movies.to_yaml)
    
    # read back in from file
    from_file = YAML.load_file('movies.yml')
    
    # use it
    from_file[:Memento]
    # => 1 
    from_file[:Primer]
    # => 4 
    

提交回复
热议问题