# How to use string variables in output files?

**URL:** <https://www.ruby-forum.com/t/how-to-use-string-variables-in-output-files/185979>\
**Category:** Ruby\
**Created:** [March 30, 2010, 2:58am UTC](https://www.ruby-forum.com/t/how-to-use-string-variables-in-output-files/185979 "2010-03-30T02:58:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [March 30, 2010, 2:58am UTC](https://www.ruby-forum.com/t/how-to-use-string-variables-in-output-files/185979/1 "2010-03-30T02:58:36Z")

</div>

hi,i want to prase the web  
‘[行情中心\_网易财经](http://quotes.money.163.com/corp/1034/code=601398.html)’  
there are 27 tables in the web,  
i made a for loop to get every table in the web,  
/home/pt/test/table0 is the output of tables[0],  
/home/pt/test/table1 is the output of tables[1],  
,  
/home/pt/test/table26 is the output of tables[27],

here is my programme,

require ‘hpricot’  
require ‘open-uri’  
require ‘iconv’  
data =  
open(‘[行情中心\_网易财经](http://quotes.money.163.com/corp/1034/code=601398.html)’).read  
html = Iconv.iconv(“UTF-8//IGNORE”,“GB2312//IGNORE”,data)  
u8doc = Hpricot(html[0])  
tables = u8doc.search(“table”)  
i=tables.size  
puts i  
for j in 0…i-1  
File.open(‘/home/pt/test/table’+j.to\_s+‘.html’, ‘w’) {|f|  
f.write(‘tables[’+j.to\_s+‘]’.inner\_html) }  
end

it can’t work,can you revise it for me?  
think you

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [March 30, 2010, 3:37am UTC](https://www.ruby-forum.com/t/how-to-use-string-variables-in-output-files/185979/2 "2010-03-30T03:37:01Z")

</div>

Hi,

do you want something like this?:

require ‘hpricot’  
require ‘open-uri’  
require ‘iconv’

data =  
open(‘[http://quotes.money.163.com/corp/1034/code=601398.html](http://quotes.money.163.com/corp/1034/code=601398.html)’).read

html = Iconv.iconv(“UTF-8//IGNORE”,“GB2312//IGNORE”,data)

u8doc = Hpricot(html[0])

tables = u8doc.search(“table”)

i=tables.size

puts i

for j in 0…i do

output = File.new(“/Users/jbw/table” + j.to\_s + “.html”, “w”)  
output.puts tables[j]  
output.close

end

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [March 30, 2010, 1:16pm UTC](https://www.ruby-forum.com/t/how-to-use-string-variables-in-output-files/185979/3 "2010-03-30T13:16:33Z")

</div>

think for your help  
the right code is :  
require ‘hpricot’  
require ‘open-uri’  
require ‘iconv’  
data  
=open(‘[http://quotes.money.163.com/corp/1034/code=601398.html](http://quotes.money.163.com/corp/1034/code=601398.html)’).read  
html = Iconv.iconv(“UTF-8//IGNORE”,“GB2312//IGNORE”,data)  
u8doc = Hpricot(html[0])  
tables = u8doc.search(“table”)  
i=tables.size  
for j in 0…i-1  
output = File.new(“/home/pt/test/table” + j.to\_s + “.html”, “w”)  
output.puts tables[j].inner\_html  
output.close  
end  
i still have some problems:  
1\when i got the table’s html,for example tables[2],  
can i get row size or column size of the tables[2].  
2\can i use array to load the data in the tables[2]??
