Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does #tty? on STDIN mean / do in ruby?

Tags:

io

ruby

Reading the ruby docs isn't overly helpful here:

Returns true if ios is associated with a terminal device (tty), false otherwise.

I was hoping to get some additional resources or explanation to help me understand this better.

For context, I'm writing a little command line program that accepts either a file path or piped content into the ruby executable and am using #tty? to determine what is coming in.

like image 213
daino3 Avatar asked Jun 25 '16 18:06

daino3


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does a real fox say?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.

How can I identify a song by sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.


1 Answers

Seems like http://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines supplies the most concise description of what #tty? does:

Ruby's IO#isatty method (aliased as IO#tty?) will tell you whether or not the IO in question is attached to a terminal. Calling it on $stdout, for instance, when it's being piped will return false.

Here is some relevant info that you may find useful:

  • Detecting stdin content in Ruby
  • How can you check for STDIN input in a Ruby script?
  • https://stackoverflow.com/a/273605
  • https://gist.github.com/timuruski/6072363

Background meaning via What do pty and tty mean?:

In UNIX, /dev/tty* is any device that acts like a "teletype", ie, terminal. (Called teletype because that's what we had for terminals in those benighted days.)

In the spirit of the question, here's an example of writing to /dev/tty from http://zetcode.com/lang/rubytutorial/io/:

#!/usr/bin/ruby

fd = IO.sysopen "/dev/tty", "w"
ios = IO.new(fd, "w")
ios.puts "ZetCode"
ios.close
like image 125
SoAwesomeMan Avatar answered Sep 22 '22 16:09

SoAwesomeMan