Issue #14413 has been updated by josh.cheek (Josh Cheek).

File Screen Shot 2020-04-03 at 2.58.41 PM.png added

I saw flipflop was readded and was wanting to explain to people how to use =
it. I feel like if people knew how to use it, they wouldn't have wanted to =
remove it. So, I made this example (shell is fish, not sure if this example=
 would work in bash):

``` shell
# a csv with a header
> cat ~/Downloads/Casos1' (3)'.csv | head -2
"ID de caso","Fecha de diagn=F3stico","Ciudad de ubicaci=F3n","Departamento=
 o Distrito","Atenci=F3n**","Edad","Sexo","Tipo*","Pa=EDs de procedencia"
"1","06/03/2020","Bogot=E1","Bogot=E1 D.C.","Recuperado","19","F","Importad=
o","Italia"


> cat ~/Downloads/Casos1' (3)'.csv |

# `-n` iterates over each line of input
# `-e` means the argument is the program, not a filename
ruby -ne '
  # `print` will print the current line.
  # `2..` is a flipflop beginning at 2, with no upper bound.
  # It will flip to true on line 2, and never flip back to false.
  # Hence, this prints all lines except for the first.
  print if 2..
' |

head -1
"1","06/03/2020","Bogot=E1","Bogot=E1 D.C.","Recuperado","19","F","Importad=
o","Italia"
Traceback (most recent call last):
	2: from -e:6:in `<main>'
	1: from -e:6:in `print'
-e:6:in `write': Broken pipe @ io_write - <STDOUT> (Errno::EPIPE)
```

It does the correct behaviour (essentially `sed 1d`), but this example isn'=
t compelling, because the error message means you would have to redirect st=
derr to `/dev/null`, which is hacky, and would also swallow real errors.


Perhaps another way to show this:

```shell
# desired
> seq 1000000 | sed -n '12,$p' | head -2
12
13

# actual
> seq 1000000 | ruby -ne 'print if 12..' | head -2
12
13
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `print'
-e:1:in `write': Broken pipe @ io_write - <STDOUT> (Errno::EPIPE)
```

----------------------------------------
Bug #14413: `-n` and `-p` flags break when stdout is closed
https://bugs.ruby-lang.org/issues/14413#change-84905

* Author: josh.cheek (Josh Cheek)
* Status: Feedback
* Priority: Normal
* ruby -v: ruby 2.5.0preview1 (2017-10-10 trunk 60153) [x86_64-darwin16]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
Ruby generally works well within a pipeline. The `-n` and `-p` flags are in=
credibly useful. However, it is common practice to use programs like `head`=
 and `sed`, which will close the pipe after completing their job. This is c=
onvenient, because often it limits an expensive amount of output to a worka=
ble subset. I can figure out the pipeline for a subset of input, and then r=
emove the limiting function.

However, Ruby explodes with `-e:1:in `write': Broken pipe @ io_write - <STD=
OUT> (Errno::EPIPE)`, when it writes the current line to stdout. When in a =
line oriented mode, and stdout closes, I think it should exit successfully =
(so it doesn't break bash scripts with `pipe fail`set) and silently (no err=
or message), hence marking this a bug report rather than a feature request.

I've attached a screenshot to show that this is how every other program wor=
ks, that I tried.

~~~
git clone https://github.com/jquery/esprima
cd esprima/test/fixtures/
ls                                                                         =
    | head -1 # ls
find . -type f -name '*json'                                               =
    | head -1 # find
find . -type f -name '*json' | head -1 | xargs cat | jq .                  =
    | head -1 # jq
find . -type f -name '*json' | grep -v JSX | grep -v tokenize              =
    | head -1 # grep
find . -type f -name '*json' | sed -E '/JSX|tokenize/d'                    =
    | head -1 # sed
find . -type f -name '*json' | awk '/JSX|tokenize/ { next }; { print }'    =
    | head -1 # awk
find . -type f -name '*json' | perl -e 'while(<>) { /JSX|tokenize/ || print=
 }' | head -1 # perl
find . -type f -name '*json' | ruby -ne 'print unless /JSX|tokenize/'      =
    | head -1 # ruby :(
~~~

---Files--------------------------------
Screen Shot 2018-01-27 at 7.27.49 PM.png (458 KB)
Screen Shot 2020-04-03 at 2.58.41 PM.png (158 KB)


-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>