# Kernel.open

**URL:** <https://www.ruby-forum.com/t/kernel-open/188150>\
**Category:** Ruby\
**Created:** [May 6, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/kernel-open/188150 "2010-05-06T16:38:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yungwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@yungwie](https://www.ruby-forum.com/u/yungwie)\
**Post date:** [May 6, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/kernel-open/188150/1 "2010-05-06T16:38:22Z")

</div>

Hi all,

I am trying to run an external commands with Kernel.open writing and  
reading its standard input / output several times. Unfortunately the  
script hangs when trying to read with IO.gets.

Code:  
fh = Kernel.open("|hunspell -m", “w+”)

fh.puts “ruby\n”  
output = fh.gets()  
puts output

fh.puts “rails\n”  
output = fh.gets()  
puts output

fh.close()

I’ve noticed that the script runs fine if I try to run hunspell without  
the ‘-m’ parameter (-m is for morhological analysis). What do I do wrong  
here?

Thanks!

---

<div class="post-metadata">

**Author:** ![yungwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@yungwie](https://www.ruby-forum.com/u/yungwie)\
**Post date:** [May 6, 2010, 8:07pm UTC](https://www.ruby-forum.com/t/kernel-open/188150/2 "2010-05-06T20:07:18Z")

</div>

Imre Farkas wrote:

> Hi all,
> 
> I am trying to run an external commands with Kernel.open writing and  
> reading its standard input / output several times. Unfortunately the  
> script hangs when trying to read with IO.gets.

Maybe because gets requires a closing newline?  
-r

---

<div class="post-metadata">

**Author:** ![yungwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@yungwie](https://www.ruby-forum.com/u/yungwie)\
**Post date:** [May 7, 2010, 10:02am UTC](https://www.ruby-forum.com/t/kernel-open/188150/3 "2010-05-07T10:02:05Z")

</div>

Roger P. wrote:

> Maybe because gets requires a closing newline?  
> -r

…and hunspell puts a closing newline after the output. So that  
shouldn’t be the problem.

---

<div class="post-metadata">

**Author:** ![yungwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@yungwie](https://www.ruby-forum.com/u/yungwie)\
**Post date:** [May 7, 2010, 11:07am UTC](https://www.ruby-forum.com/t/kernel-open/188150/4 "2010-05-07T11:07:23Z")

</div>

Imre Farkas wrote:

> Hi all,
> 
> I am trying to run an external commands with Kernel.open writing and  
> reading its standard input / output several times. Unfortunately the  
> script hangs when trying to read with IO.gets.
> 
> Code:  
> fh = Kernel.open("|hunspell -m", “w+”)
> 
> fh.puts “ruby\n”

Try here: fh.flush  
(or: fh.sync = true after opening it)

I would normally use IO.popen, but I don’t know how/if that differs from  
using open in the way you have.
