# File question

**URL:** <https://rubytalk.org/t/file-question/47407>\
**Category:** ruby-talk\
**Created:** [30 June 2008 01:16 UTC](https://rubytalk.org/t/file-question/47407 "2008-06-30T01:16:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [30 June 2008 01:16 UTC](https://rubytalk.org/t/file-question/47407/1 "2008-06-30T01:16:47Z")

</div>

Hi, just wondering how I would skip the first line of a file?

File.open('file.txt', 'r').each do |line|  
# start on the 2nd line  
end

Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [30 June 2008 01:29 UTC](https://rubytalk.org/t/file-question/47407/2 "2008-06-30T01:29:26Z")

</div>

# just wondering how I would skip the first line of a file?

then go, play w it using ruby 😉

# File.open('file.txt', 'r').each do |line|  
# # start on the 2nd line  
# end

as always, there are many ways (depennding on your taste),

eg given a file,

File.open('file.txt', 'r').each do |line|  
&nbsp;&nbsp;p line  
end  
"1234\n"  
"456\n"  
"4321\n"  
"654\n"  
"546\n"  
"3456\n"  
"5436\n"  
"9879\n"  
"1111\n"  
#=\> #\<File:file.txt\>

you can skip the line by referring to it's index,

File.open('file.txt', 'r').each\_with\_index do |line,index|  
&nbsp;&nbsp;next if index == 0  
&nbsp;&nbsp;p line  
end  
"456\n"  
"4321\n"  
"654\n"  
"546\n"  
"3456\n"  
"5436\n"  
"9879\n"  
"1111\n"

or just skip it by reading and discarding it

File.open('file.txt', 'r') do |file|  
&nbsp;&nbsp;file.readline  
&nbsp;&nbsp;file.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;p line  
&nbsp;&nbsp;end  
end  
"456\n"  
"4321\n"  
"654\n"  
"546\n"  
"3456\n"  
"5436\n"  
"9879\n"  
"1111\n"  
#=\> #\<File:file.txt (closed)\>

note i prefer the latter since its clearer (to me), and it does not annoy the loop 😉

kind regards -botp

> **···**
>
> From: Justin To [[mailto:tekmc@hotmail.com](mailto:tekmc@hotmail.com)]

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [30 June 2008 02:00 UTC](https://rubytalk.org/t/file-question/47407/3 "2008-06-30T02:00:34Z")

</div>

Thanks, helps a lot. How about if I am using FasterCSV?

require 'fastercsv'

File.open("file.txt', 'r') do |file|  
&nbsp;&nbsp;file.readline  
&nbsp;&nbsp;&nbsp;&nbsp;FasterCSV.foreach("#{file}") do |row|

&nbsp;&nbsp;&nbsp;&nbsp;end

end

Invalid argument #\<file...\>

Mmm... not sure how I would do it then! =)

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [30 June 2008 02:37 UTC](https://rubytalk.org/t/file-question/47407/4 "2008-06-30T02:37:24Z")

</div>

# require 'fastercsv'  
# File.open("file.txt', 'r') do |file|

file here is a File object. you can check by printing its class

# file.readline  
# FasterCSV.foreach("#{file}") do |row|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if you print "#{file}", you will know why this will not work

# end  
# end

again, there are many ways in ruby, eg, given a file,

File.open("csv.txt").each do |line|  
&nbsp;&nbsp;p line  
end  
"\"skip this\"\n"  
"\"123\",\"abc\"\n"  
"\"456\",\"def\"\n"  
"\"678\",\"zyx\"\n"  
#=\> #\<File:csv.txt\>

we can skip the first line, and then parse the rest of the lines using fastercsv like,

File.open "csv.txt" do |file|  
&nbsp;&nbsp;file.readline  
&nbsp;&nbsp;file.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;p FasterCSV.parse\_line(line)  
&nbsp;&nbsp;end  
end  
["123", "abc"]  
["456", "def"]  
["678", "zyx"]  
#=\> #\<File:csv.txt (closed)\>

or

you can just plainly open the file using fastercsv, and notifying fcsv that the first line is a header, like,

FasterCSV.foreach("csv.txt",{:headers=\>:first\_row}) do |line|  
&nbsp;&nbsp;puts line  
end  
123,abc  
456,def  
678,zyx  
#=\> nil

kind regards -botp

> **···**
>
> From: [tekmc@hotmail.com](mailto:tekmc@hotmail.com) [[mailto:tekmc@hotmail.com](mailto:tekmc@hotmail.com)]

---

<div class="post-metadata">

**Author:** ![Justin\_To](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Justin\_To](https://rubytalk.org/u/Justin_To)\
**Post date:** [30 June 2008 02:57 UTC](https://rubytalk.org/t/file-question/47407/5 "2008-06-30T02:57:27Z")

</div>

Terrific, thanks so much!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
