Help with lineno

Hi, so I'm not clear with what lineno does. I'm trying to print the number
of lines in the file 'testfile'. However it says. "Undefined method
'lineno' for 105:Fixnum (NoMethodError)

Can someone help me out with that? This is what I've written. Please
forgive my ignorance.

fo = File.open("testfile.odt", "w") do |file|
file.write("So basically we're just testing this motherfucking thing. This
is the second line. And this is the third.")
end

line = fo.lineno

puts line

···

--
Himel Sarkar,
*himelsarkarforever.blogspot.com <http://himelsarkarforever.blogspot.com>*

* Here's to the crazy ones. The misfits. The rebels. The troublemakers. The
round pegs in the square holes. The ones who see things differently.
They're not fond of rules. And they have no respect for the status quo. You
can quote them, disagree with them, glorify or vilify them. About the only
thing you can't do is ignore them. Because they change things. They push
the human race forward. And while some may see them as the crazy ones, we
see genius. Because the people who are crazy enough to think they can
change the world are the ones who do.*

fo = File.open("testfile.odt", "w") do |file|
file
.write("So basically we're just testing this motherfucking thing. This is
the second line. And this is the third.")
end

Well the reason you are getting this error is because. fo doesn't has the
file object instead it has the value return from the block you ran. For the
function lineno to work you need do these two operation separately.

line = fo.lineno

The right code would be:

fo = File.open("testfile.odt", "w")
fo
.write("So basically we're just testing this motherfucking thing. This is
the second line. And this is the third.")
line = fo.lineno

puts line

http://csnipp.com/coderhs/83

···

On Sat, Feb 8, 2014 at 4:51 PM, Himel Sarkar <hsarkar.007@gmail.com> wrote:

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

Harisankar P S wrote in post #1136024:

The right code would be:

fo = File.open("testfile.odt", "w")
fo
.write("lines of text")
line = fo.lineno #<~~ It will give an error, as you opened it for **write**

mode.

···

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

Thank you for taking the time to reply. However now I'm getting the error
'lineno': not opened for reading (IOError)

···

On Sat, Feb 8, 2014 at 5:03 PM, Harisankar P S <mailme@hsps.in> wrote:

On Sat, Feb 8, 2014 at 4:51 PM, Himel Sarkar <hsarkar.007@gmail.com>wrote:

fo = File.open("testfile.odt", "w") do |file|
file
.write("So basically we're just testing this motherfucking thing. This is
the second line. And this is the third.")
end

Well the reason you are getting this error is because. fo doesn't has the
file object instead it has the value return from the block you ran. For the
function lineno to work you need do these two operation separately.

line = fo.lineno

The right code would be:

fo = File.open("testfile.odt", "w")
fo
.write("So basically we're just testing this motherfucking thing. This is
the second line. And this is the third.")
line = fo.lineno

puts line

Csnipp.com -

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

--
Himel Sarkar,
*himelsarkarforever.blogspot.com <http://himelsarkarforever.blogspot.com>*

* Here's to the crazy ones. The misfits. The rebels. The troublemakers. The
round pegs in the square holes. The ones who see things differently.
They're not fond of rules. And they have no respect for the status quo. You
can quote them, disagree with them, glorify or vilify them. About the only
thing you can't do is ignore them. Because they change things. They push
the human race forward. And while some may see them as the crazy ones, we
see genius. Because the people who are crazy enough to think they can
change the world are the ones who do.*

Oh sorry i missed out an important thing. The 'w' attribute in the file
open means the file is open only for writing, we need to open for reading.
So just update the code as so

fo = File.open('test.txt', 'r')

···

On Sat, Feb 8, 2014 at 5:09 PM, Himel Sarkar <hsarkar.007@gmail.com> wrote:

Thank you for taking the time to reply. However now I'm getting the error
'lineno': not opened for reading (IOError)

On Sat, Feb 8, 2014 at 5:03 PM, Harisankar P S <mailme@hsps.in> wrote:

On Sat, Feb 8, 2014 at 4:51 PM, Himel Sarkar <hsarkar.007@gmail.com>wrote:

fo = File.open("testfile.odt", "w") do |file|
file
.write("So basically we're just testing this motherfucking thing. This
is the second line. And this is the third.")
end

Well the reason you are getting this error is because. fo doesn't has the
file object instead it has the value return from the block you ran. For the
function lineno to work you need do these two operation separately.

line = fo.lineno

The right code would be:

fo = File.open("testfile.odt", "w")
fo
.write("So basically we're just testing this motherfucking thing. This is
the second line. And this is the third.")
line = fo.lineno

puts line

Csnipp.com -

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

--
Himel Sarkar,
*himelsarkarforever.blogspot.com <http://himelsarkarforever.blogspot.com>*

* Here's to the crazy ones. The misfits. The rebels. The troublemakers.
The round pegs in the square holes. The ones who see things differently.
They're not fond of rules. And they have no respect for the status quo. You
can quote them, disagree with them, glorify or vilify them. About the only
thing you can't do is ignore them. Because they change things. They push
the human race forward. And while some may see them as the crazy ones, we
see genius. Because the people who are crazy enough to think they can
change the world are the ones who do. *

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

Harisankar P S wrote in post #1136028:

Oh sorry i missed out an important thing. The 'w' attribute in the file
open means the file is open only for writing, we need to open for
reading.
So just update the code as so

fo = File.open('test.txt', 'r')

Still an error you would get, as you didn't close it. It will be as
below :

fo = File.open("testfile.odt", "w")
fo.write("foo/nbar/nbaz/n")
fo.close

fo = File.open("testfile.odt", "r")
line = fo.lineno

puts line

···

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

Thank you so much!

It's working now! :slight_smile:

···

On Sat, Feb 8, 2014 at 5:17 PM, Harisankar P S <mailme@hsps.in> wrote:

Oh sorry i missed out an important thing. The 'w' attribute in the file
open means the file is open only for writing, we need to open for reading.
So just update the code as so

fo = File.open('test.txt', 'r')

On Sat, Feb 8, 2014 at 5:09 PM, Himel Sarkar <hsarkar.007@gmail.com>wrote:

Thank you for taking the time to reply. However now I'm getting the error
'lineno': not opened for reading (IOError)

On Sat, Feb 8, 2014 at 5:03 PM, Harisankar P S <mailme@hsps.in> wrote:

On Sat, Feb 8, 2014 at 4:51 PM, Himel Sarkar <hsarkar.007@gmail.com>wrote:

fo = File.open("testfile.odt", "w") do |file|
file
.write("So basically we're just testing this motherfucking thing. This
is the second line. And this is the third.")
end

Well the reason you are getting this error is because. fo doesn't has
the file object instead it has the value return from the block you ran. For
the function lineno to work you need do these two operation separately.

line = fo.lineno

The right code would be:

fo = File.open("testfile.odt", "w")
fo
.write("So basically we're just testing this motherfucking thing. This
is the second line. And this is the third.")
line = fo.lineno

puts line

Csnipp.com -

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

--
Himel Sarkar,
*himelsarkarforever.blogspot.com <http://himelsarkarforever.blogspot.com>*

* Here's to the crazy ones. The misfits. The rebels. The troublemakers.
The round pegs in the square holes. The ones who see things differently.
They're not fond of rules. And they have no respect for the status quo. You
can quote them, disagree with them, glorify or vilify them. About the only
thing you can't do is ignore them. Because they change things. They push
the human race forward. And while some may see them as the crazy ones, we
see genius. Because the people who are crazy enough to think they can
change the world are the ones who do. *

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

--
Himel Sarkar,
*himelsarkarforever.blogspot.com <http://himelsarkarforever.blogspot.com>*

* Here's to the crazy ones. The misfits. The rebels. The troublemakers. The
round pegs in the square holes. The ones who see things differently.
They're not fond of rules. And they have no respect for the status quo. You
can quote them, disagree with them, glorify or vilify them. About the only
thing you can't do is ignore them. Because they change things. They push
the human race forward. And while some may see them as the crazy ones, we
see genius. Because the people who are crazy enough to think they can
change the world are the ones who do.*