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

···

--
Posted via http://www.ruby-forum.com/.

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/

···

On Dec 28, 2007 7:49 PM, Mark Toth <mark.toth@telia.com> wrote:

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

Alle venerdì 28 dicembre 2007, Mark Toth ha scritto:

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

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

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

···

On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:

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
--
Posted viahttp://www.ruby-forum.com/.

Stefano Crocco wrote:

Alle venerdì 28 dicembre 2007, Mark Toth 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

···

--
Posted via http://www.ruby-forum.com/\.

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, 2007 3:20 PM, lrlebron@gmail.com <lrlebron@gmail.com> wrote:

On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> 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
> --
> 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

Just add a sFile.gets before the while.

···

On Dec 28, 2007 8:12 PM, Mark Toth <mark.toth@telia.com> wrote:

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

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

···

On Dec 28, 3:01 pm, Joe <qbpro...@gmail.com> wrote:

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, 2007 3:20 PM, lrleb...@gmail.com <lrleb...@gmail.com> wrote:

> On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> > 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
> > --
> > 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

Thomas Wieczorek wrote:

Stefano Crocco 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 Toth <mark.toth@telia.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

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
http://ruby-doc.org/core/classes/File.html would list all members.

Is the problem that http://ruby-doc.org/core/classes/File.html doesn't
list members of a superclass?

Joe

···

On Dec 28, 2007 4:35 PM, lrlebron@gmail.com <lrlebron@gmail.com> wrote:

On Dec 28, 3:01 pm, Joe <qbpro...@gmail.com> wrote:
> I don't see File.open here:http://ruby-doc.org/core/classes/File.html
> Am I looking in the wrong place?
>
> Also, how do you find out about globals like $. ?
>
> Joe
>

> On Dec 28, 2007 3:20 PM, lrleb...@gmail.com <lrleb...@gmail.com> wrote:
>
>
>
> > On Dec 28, 12:49 pm, Mark Toth <mark.t...@telia.com> wrote:
> > > 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
> > > --
> > > 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

Open is a kernel method
module Kernel - RDoc Documentation

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

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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 Gray II

···

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

Thomas Wieczorek wrote:

On Dec 28, 2007 8:12 PM, Mark Toth <mark.toth@telia.com> wrote:

Stefano Crocco 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?:

Thanks. That's what i was missing.

Joe

···

On Dec 28, 2007 8:45 PM, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826