From: Phillip Gawlowski Date: 2008-04-09T09:36:59+09:00 Subject: Re: output of a method to file? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Zoe Phoenix wrote: | Maybe it would help if I posted the code, I'm not sure I understand... | For example..: | | | def cityList | c_list = ['Boston', 'Nashville', 'San Francisco', 'Houston', 'Portland', | 'Atlanta', 'Chicago', 'Indianapolis', 'Charleston'] | | bar = '|| ' | | | puts bar + c_list[0..2].join(' pets || ') + (' pets ||') | | puts bar + c_list[3..5].join(' pets || ') + (' pets ||') | | puts bar + c_list[6..8].join(' pets || ') + (' pets ||') | | end | | cityList | | | puts 'File completed. Save as..?' | save_as = gets.chomp | | | | File::open('c:\rubyfiles\citylist-' + save_as + '.txt', 'w') do |f| | | | *********** | | I need it to take the output of that method that I created and write it | to a file... I'm not sure I understand how to do that with the example | you gave. Since I know the code that I had after "File::open", etc. was | wrong, I just left it out. If it is allowed, I'll add some Ruby idioms (you'll find these often, and will find them useful, I hope). I saved your file as "saving.rb", and it now produces the following: File completed. Save as..? temp #that's the name I saved the file as C:\temp>less c:\rubyfiles\citylist-temp.txt # This is how the file looks. || Boston pets || Nashville pets || San Francisco pets || || Houston pets || Portland pets || Atlanta pets || || Chicago pets || Indianapolis pets || Charleston pets || This is the reworked script: def cityList ~ c_list = [ ~ 'Boston', ~ 'Nashville', ~ 'San Francisco', ~ 'Houston', ~ 'Portland', ~ 'Atlanta', ~ 'Chicago', ~ 'Indianapolis', ~ 'Charleston' ~ ] # I just changed that for readability. ~ bar = '|| ' # I left this, too. ~ temp_array = [] #This is a temporary array, #I use to store the results. ~ temp_array.push bar + c_list[0..2].join(' pets || ') + (' pets ||') + "\n" # This pushes the result of into the Array. It gets stored at the first index of the Array. You could access it via temp_array[0]. ~ temp_array.push bar + c_list[3..5].join(' pets || ') + (' pets ||') + "\n" #This does something similar, except that it is stored at the next Array index, temp_array[1] ~ temp_array.push bar + c_list[6..8].join(' pets || ') + (' pets ||') + "\n" # See above, except this time it is temp_array[2]. Notice that I added "\n" to your code. This adds a new line. Just delete it, if you don't want the whole output on a new line, or just leave the last one, if every iteration shall be on a new line. end puts 'File completed. Save as..?' save_as = gets.chomp File.open("c:/rubyfiles/citylist-#{save_as}.txt","w") do |f| # Here, I used string-interpolation, which only works with the double quotes ("). ~ f.write cityList #This is a bit complex. f.write takes the result of cityList, and writes it to a file. In Ruby, every method returns its last action, so to speak. In this case, it is the Array with all three lines. Also notice, that you don't have to use File::open, but that should work, too. end #ending the block again, and closing the file. I hope that helps (In case it's not really readable, just copy and paste the code into your favorite editor). | It would also be nice if I could have the method cycle through 3 items | in the array, then write 3 to a new line, the continue in increments of | 3 until it reaches the end of the array. But, I'm not sure if I should | write another topic to ask that or if someone will answer that here. Let me repeat, to see if I got that correctly: You want to do cityList three times, and write all of these iterations into 3 seperate files? In that case, you could just wrap the File.open[..] part into a loop. Like 3.times do File.open[...] end | Thanks for the help so far, though... I'm pretty new at this. We all were, at one time. :) I hope I can help you out. In case you haven't found it: Chris Pine's Learn to Program helped me a lot: pine.fm/LearnToProgram/ And _why's Piognant Guide to Ruby is good, too (although a bit.. strange): http://poignantguide.net/ruby/ - -- Phillip Gawlowski Twitter: twitter.com/cynicalryan ~ There's never enough time to do all the nothing you want. -- Calvin -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkf8D6YACgkQbtAgaoJTgL8QbACdFn8p3d/haXRpq/lyh41TYEQl s70AmgKb5gJaaLJSpsTkESefcjWUDXNb =TrpN -----END PGP SIGNATURE-----