# File question

**URL:** <https://www.ruby-forum.com/t/file-question/141719>\
**Category:** Ruby\
**Created:** [June 30, 2008, 3:19am UTC](https://www.ruby-forum.com/t/file-question/141719 "2008-06-30T03:19:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 3:19am UTC](https://www.ruby-forum.com/t/file-question/141719/1 "2008-06-30T03:19:27Z")

</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!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 3:32am UTC](https://www.ruby-forum.com/t/file-question/141719/2 "2008-06-30T03:32:58Z")

</div>

From: Justin To [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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|  
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|  
next if index == 0  
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|  
file.readline  
file.each do |line|  
p line  
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

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 4:03am UTC](https://www.ruby-forum.com/t/file-question/141719/3 "2008-06-30T04:03:12Z")

</div>

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

require ‘fastercsv’

File.open(“file.txt’, ‘r’) do |file|  
file.readline  
FasterCSV.foreach(”#{file}") do |row|

```
end

```

end

Invalid argument #\<file…\>

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

Thanks

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 4:40am UTC](https://www.ruby-forum.com/t/file-question/141719/4 "2008-06-30T04:40:45Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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|

```
 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|  
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|  
file.readline  
file.each do |line|  
p FasterCSV.parse\_line(line)  
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|  
puts line  
end  
123,abc  
456,def  
678,zyx  
#=\> nil

kind regards -botp

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 5:00am UTC](https://www.ruby-forum.com/t/file-question/141719/5 "2008-06-30T05:00:08Z")

</div>

Terrific, thanks so much!
