# Command help

**URL:** <https://www.ruby-forum.com/t/command-help/71059>\
**Category:** Ruby\
**Created:** [October 5, 2006, 1:57am UTC](https://www.ruby-forum.com/t/command-help/71059 "2006-10-05T01:57:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [October 5, 2006, 1:57am UTC](https://www.ruby-forum.com/t/command-help/71059/1 "2006-10-05T01:57:33Z")

</div>

Found a buddy who used something like:

hostname = open("|hostname").gets

And…

I see that it works, but I don’t find any of the documentation that  
makes sense.

So – it seems to be IO class, but how? There’s no IO declaration  
there.  
Also, I can’t find anything that says you can open a command with a  
preceding  
pipe and get the output of the executed command.

I’m familiar with perls open: open(FH, “hostname |”) with a suffixed  
pipe –  
which is consistent with unix pipe usage.

But can someone explain to me what’s going on here so my brain doesn’t  
implode?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [October 5, 2006, 2:03am UTC](https://www.ruby-forum.com/t/command-help/71059/2 "2006-10-05T02:03:33Z")

</div>

On 10/5/06, Tom A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> pipe and get the output of the executed command.
> 
> I’m familiar with perls open: open(FH, “hostname |”) with a suffixed pipe –  
> which is consistent with unix pipe usage.
> 
> But can someone explain to me what’s going on here so my brain doesn’t implode?

[http://ruby-doc.org/core/classes/Kernel.html#M001989](http://ruby-doc.org/core/classes/Kernel.html#M001989)

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [October 5, 2006, 3:15am UTC](https://www.ruby-forum.com/t/command-help/71059/3 "2006-10-05T03:15:37Z")

</div>

Jan S. wrote:

> > So – it seems to be IO class, but how? There’s no IO declaration there.
> 
> [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M001989)

OK, I missed the “Kernel#open” in the list of where I can use open.  
NOTE: the  
list from ‘ri’ could be better formatted.

What’s with the pipe being at the wrong end?  
What’s the thinking behind that?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [October 5, 2006, 7:27am UTC](https://www.ruby-forum.com/t/command-help/71059/4 "2006-10-05T07:27:53Z")

</div>

On Thu, 5 Oct 2006, Tom A. wrote:

> What’s with the pipe being at the wrong end?  
> What’s the thinking behind that?

" open a pipe to the command X "

just guessing…

-a

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [October 5, 2006, 9:16am UTC](https://www.ruby-forum.com/t/command-help/71059/5 "2006-10-05T09:16:06Z")

</div>

Tom A. schrieb:

> > > hostname = open("|hostname").gets
> 
> What’s with the pipe being at the wrong end?  
> What’s the thinking behind that?

You can find a short description in ri or the Pickaxe:

C:\tmp\>ri IO  
-------------------------------------------------------------- Class: IO  
Class +IO+ is the basis for all input and output in Ruby. An I/O  
stream may be _duplexed_ (that is, bidirectional), and so may use  
more than one native operating system stream.

```
  Many of the examples in this section use class +File+, the only
  standard subclass of +IO+. The two classes are closely associated.

  As used in this section, _portname_ may take any of the following
  forms.

  * A plain string represents a filename suitable for the
      underlying operating system.

```

> > > > >

```
  * A string starting with ``+|+'' indicates a subprocess. The
      remainder of the string following the ``+|+'' is invoked as a
      process with appropriate input/output channels connected to 

```

it.

> > > > >

```
  * A string equal to ``+|-+'' will create another Ruby instance 

```

as  
a subprocess.

Regards,  
Pit
