Building Unix Tools with Ruby
Subject:   "more Rubyish" isn't relevant in this case
Date:   2003-09-21 05:13:39
From:   dblack
Response to: "It's code like this that causes unrest."

> "while gets {}" for example. What nonsense.
> "$stdin.readlines.each {}" is much more Rubyish.

These two things are not equivalent. gets returns one line. readlines reads all the lines into an array. There may be cases when these two techniques appear to do the same thing, but in fact they don't. If you try them with keyboard input, you'll see what I mean.

Since they are different, determining which one is "more Rubyish" is secondary to deciding which one you actually want to do. Moreover, if one needs the gets version, one can then do:

while (line = gets)

to get rid of $_. (You'll find this technique used in several places in the Ruby distribution.)

1 to 1 of 1
  1. "more Rubyish" isn't relevant in this case
    2003-09-23 08:55:57  anonymous2 [View]

1 to 1 of 1