How to jump over the first line in a file? (newbie)

I have this code:

file = “filename.txt”
File.open(file) do |sFile|
while line = sFile.gets
row = line.split("\t")
a_product = Product.new(
:sku => row[0] ,
:name => row[1] ,
:price => row[2] ,
a_product.save
end
end

What if I want to jump over the first line of the file so it won´t be
imported?

Thank you,

Mark

Alle venerdì 28 dicembre 2007, Mark T. ha scritto:

    :price => row[2] ,

Mark
A possibility is to do a call to sFile.gets before the while cycle. This
way,
the first line of the file is read but not used.

Stefano

On Dec 28, 2007 7:49 PM, Mark T. [email protected] wrote:

What if I want to jump over the first line of the file so it won´t be
imported?

You could use FasterCSV with the :header option set to true.
It is a pretty fast gem to read X separated files:
http://fastercsv.rubyforge.org/

Stefano C. wrote:

Alle venerdì 28 dicembre 2007, Mark T. ha scritto:

    :price => row[2] ,

Mark
A possibility is to do a call to sFile.gets before the while cycle. This
way,
the first line of the file is read but not used.

Stefano

Hmmm, it sounds like a great idea! Can you please send me some code for
it?

/Mark

Thomas W. wrote:

On Dec 28, 2007 8:12 PM, Mark T. [email protected] wrote:

Stefano C. wrote:
Hmmm, it sounds like a great idea! Can you please send me some code for
it?

Just add a sFile.gets before the while.

Okeeeey, so you mean like this?:

sFile.gets
file = "filename.txt"
File.open(file) do |sFile|
while line = sFile.gets
    row = line.split("\t")
  a_product = Product.new(
    :sku => row[0] ,
    :name => row[1] ,
    :price => row[2] ,
  a_product.save
  end
end

On Dec 28, 2007 8:12 PM, Mark T. [email protected] wrote:

Stefano C. wrote:
Hmmm, it sounds like a great idea! Can you please send me some code for
it?

Just add a sFile.gets before the while.

On Dec 28, 12:49 pm, Mark T. [email protected] wrote:

  a_product.save

Posted viahttp://www.ruby-forum.com/.
The $. variable holds the line number so you could do something like
this:

File.open(“filename.txt”) do |aFile|
aFile.each_line do |line|
if $. > 1
#processs data here

end

end
end

Luis

File#foreach anyone?

You might want to check it out, using gets before the while loop will do
the job too, though.

Regards,
Lee

I don’t see File.open here: class File - RDoc Documentation
Am I looking in the wrong place?

Also, how do you find out about globals like $. ?

Joe

On Dec 28, 3:01 pm, Joe [email protected] wrote:

    :price => row[2] ,


end
end
end

Luis

Open is a kernel method
http://ruby-doc.org/core/classes/Kernel.html#M005990

For variables like $. see Programming Ruby 2ed Chapter 22 (page 334
for $.)

Luis

Joe wrote:

I would think
class File - RDoc Documentation would list all members.

Is the problem that class File - RDoc Documentation doesn’t
list members of a superclass?

Exactly. It does however list which class is the superclass (IO) and if
you
click on that you will see the method IO.open which is the same as
File.open.

HTH,
Sebastian

How would I know that File.open exists if I didn’t look at Kernel?
The way Mark was using it, it made it look like a member of the File
class. Is this just something you have to know, or am I missing
something in ruby doc? I would think
class File - RDoc Documentation would list all members.

Is the problem that class File - RDoc Documentation doesn’t
list members of a superclass?

Joe

On Dec 28, 2007, at 1:59 PM, Mark T. wrote:

Okeeeey, so you mean like this?:
No. To jump over a line do:

#!/usr/bin/env ruby -wKU

File.open(FILE) do |file| # open this code file
file.gets # read and skip one line
file.each do |line|
puts line # replace this with the needed code
end
end

END

James Edward G. II

Thanks. That’s what i was missing.

Joe