I'd like to read a file and output its contents (just to terminal is fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this function must already exist.
On Apr 27, 1:39 am, John Joyce <dangerwillrobinsondan...@gmail.com> wrote:
I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.
I'd like to read a file and output its contents (just to terminal is fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this function must already exist.
I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.
love,
John Joyce
ARGF.readlines.each_with_index{
> line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
···
On 4/27/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
John Joyce <dangerwillrobinsondanger@gmail.com> writes:
I'd like to read a file and output its contents (just to terminal is
fine for simplicity) with line numbers.
Is there a Ruby library method that handles this like unix cat?
Of course we can write a looping one-liner, but it seems to me this
function must already exist.
This .each_with_index seems to be an invaluable method... I'd like to know more about it. Can anyone give a little more detail on how each_with_index works? The pickaxe covered it too briefly for my feeble mind, but clearly it's got legs.
···
On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:
On 28.04.2007 09:06, Robert Dober wrote:
On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
> love,
> John Joyce
>
ARGF.readlines.each_with_index{
> line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!
Thank you! Btw, you can also do ARGF.each_with_index so you do not have to read the whole file into mem before printing it.
There's not much to it. It's defined in module Enumerable and yields the current element plus a count to the block. You can imagine it implemented like this but of course it's written in C:
module Enumerable
def ewi
c=0
each {|x| yield x, c; c+=1}
end
end
Kind regards
robert
···
On 28.04.2007 15:27, John Joyce wrote:
On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:
On 28.04.2007 09:06, Robert Dober wrote:
On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
> love,
> John Joyce
>
ARGF.readlines.each_with_index{
> line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!
Thank you! Btw, you can also do ARGF.each_with_index so you do not have to read the whole file into mem before printing it.
Kind regards
robert
This .each_with_index seems to be an invaluable method... I'd like to know more about it. Can anyone give a little more detail on how each_with_index works? The pickaxe covered it too briefly for my feeble mind, but clearly it's got legs.
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through the array, but additionally hands over a counter that is also incremented?! If this is the deal, then I'm gonna have to go to rehab because of that method. How useful it will be. I think for the last week I've been wanting exactly that in more places than I can think of. Where I had been building iterating blocks with some externally initialized counter. I knew I'd missed something golden. Hadn't looked into the Enumerator lib yet. Been forging through the File an IO stuff.
···
On Apr 29, 2007, at 1:10 AM, Robert Klemme wrote:
On 28.04.2007 15:27, John Joyce wrote:
On Apr 28, 2007, at 9:50 PM, Robert Klemme wrote:
On 28.04.2007 09:06, Robert Dober wrote:
On 4/28/07, Robert Dober <robert.dober@gmail.com> wrote:
> love,
> John Joyce
>
ARGF.readlines.each_with_index{
> line, idx |
puts "%3d %s" % [ idx.succ, line ]
}
HTH
Robert
But Robert's solution is nicer, I did not know about ARGF.lineno, and
have overlooked his solution, sorry!
Thank you! Btw, you can also do ARGF.each_with_index so you do not have to read the whole file into mem before printing it.
Kind regards
robert
This .each_with_index seems to be an invaluable method... I'd like to know more about it. Can anyone give a little more detail on how each_with_index works? The pickaxe covered it too briefly for my feeble mind, but clearly it's got legs.
There's not much to it. It's defined in module Enumerable and yields the current element plus a count to the block. You can imagine it implemented like this but of course it's written in C:
module Enumerable
def ewi
c=0
each {|x| yield x, c; c+=1}
end
end
On 4/28/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
>
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through
the array, but additionally hands over a counter that is also
incremented?! If this is the deal, then I'm gonna have to go to rehab
because of that method. How useful it will be. I think for the last
week I've been wanting exactly that in more places than I can think
of. Where I had been building iterating blocks with some externally
initialized counter. I knew I'd missed something golden. Hadn't
looked into the Enumerator lib yet. Been forging through the File an
IO stuff.
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through the array, but additionally hands over a counter that is also incremented?!
Right.
If this is the deal, then I'm gonna have to go to rehab because of that method. How useful it will be. I think for the last week I've been wanting exactly that in more places than I can think of.
Just be careful. In my experience the need for and index is often a hint that you haven't yet found the Ruby way to approach the problem. That's not always true, but often.
Where I had been building iterating blocks with some externally initialized counter.
See the standard generator library for doing this without a counter.
So let me see if I got this close to straight:
each_with_index is like each in that it alows you to iterate through the array, but additionally hands over a counter that is also incremented?!
Correct.
If this is the deal, then I'm gonna have to go to rehab because of that method. How useful it will be. I think for the last week I've been wanting exactly that in more places than I can think of. Where I had been building iterating blocks with some externally initialized counter. I knew I'd missed something golden. Hadn't looked into the Enumerator lib yet. Been forging through the File an IO stuff.
:-))
I suggest you take a very close look at Enumerable because that is one of the most often used pieces of Ruby and it's at the core of all the nice iterating functionality.
Then look into Enumerator which brings this functionality to all sorts of objects (if they provide any iterating method).
Finally, check out #inject - my personal favorite.
Sure enough it's in the Ruby Standard LIbrary, among the things the pickaxe glances over at the end....
I'll have to dig through that. Very interesting, but could lean towards more C++/Java-ish looking code perhaps.
Nonetheless, 1000 thanks to all.
···
On Apr 29, 2007, at 9:47 AM, James Edward Gray II wrote:
On Apr 28, 2007, at 7:11 PM, John Joyce wrote:
On Apr 29, 2007, at 4:49 AM, James Edward Gray II wrote:
See the standard generator library for doing this without a counter.