From: "Jesús Gabriel y Galán" Date: 2010-07-30T21:47:30+09:00 Subject: Re: my script just read one line? On Fri, Jul 30, 2010 at 2:18 PM, Junhui Liao wrote: > > > >> File.open("../original_data/test_2lines.tsv") do |file| >>   first_line = file.readline >>   first_line_times = first_line.chomp.split("\t").each_slice(2).map >> {|time,signal| time} >>   write_line_to_file first_line >>   file.each_line do |record| >>     line_data = record.chomp.split("\t") >>       write_line_to_file line_data, first_line_times >>   end >> end >> >> def write_line_to_file line, base_time = Hash.new(0) >>   line_data.each_slice(2).with_index do |(time,signal), index| >>     File.open("#{index}_debug_split"+".tsv" , "w") do |f| >>       f << "#{time.to_i - base_time[index]}\t#{signal}\n" >>     end >>   end >> end >> >> Hope this gives you an idea to explore, >> >> Jesus. > > > Dear Jesús Gabriel y Galán, > > Thanks a lot for your second comment. > > While to the following script, there are few problems > while running. > > 1, To this line " write_line_to_file first_line" and > this line "write_line_to_file line_data, first_line_times ", > the error message like following, > > in `block (2 levels) in ': undefined method > `write_line_to_file' for File_spliter:Class (NoMethodError) > > It seemed the "write_line_to_file" has no definition before calling. > I tried to make a class to "surround" your code, but same error > occurred. Sorry, the method write_line_to_file should be defined before the other block of code, so that it exists when it's called. > > > 2, I don't understand this two lines. > 2.1, "write_line_to_file first_line". Since the definition of > write_line_to_file has > two arguments, while here just only one, and I don't know the purpose of > this > line. It has two arguments, but the second is optional, and if not passed, it will be assigned a new Hash(0). See the def of the method. > > 2.2, "line_data.each_slice(2).with_index do |(time,signal), index|". > Why is not "line.each_slice(2).with_index do |(time,signal), index|" ? It's a mistake on my part. Copy/paste error. Jesus.