How do I write the contents of a hash to a file in Ruby? -


i have hash looks follows:

character = {"name: " => "#$name", "weapon: " => "#$weapon", "armor: " => "#$armor"} 

i want print each key , each value file looks this.

name: templar
weapon: sword
armor: heavy armor

i use basic method understand going on. i've read there modules you, such marshal, basic method involves beginner-level code.

this should work you. recommend reading more on files , iterating through hashes first though.

yourfile = "/some/path/file.txt" file.open(yourfile, 'w') |file|   character.each{ |k, v| file.write("#{k}: #{v}\n") } end 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -