# How to open a console and ruby a ruby script from ruby?

**URL:** <https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775>\
**Category:** Ruby\
**Created:** [July 1, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775 "2006-07-01T20:46:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 1, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/1 "2006-07-01T20:46:36Z")

</div>

I know I can do

system “ruby script.rb”

But I want to open a console to run it in.

Thanks.  
T

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 1, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/2 "2006-07-01T20:58:50Z")

</div>

On Sun, 2 Jul 2006 removed\_email\_address@domain.invalid wrote:

> I know I can do
> 
> system “ruby script.rb”
> 
> But I want to open a console to run it in.

you mean a pty? check out ext/pty, there’s an example in there.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 1, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/3 "2006-07-01T21:05:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Sun, 2 Jul 2006 removed\_email\_address@domain.invalid wrote:
> 
> > I know I can do
> > 
> > system “ruby script.rb”
> > 
> > But I want to open a console to run it in.
> 
> you mean a pty? check out ext/pty, there’s an example in there.

I want a window to come up with a shell propmt (or dos on windows).

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 1, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/4 "2006-07-01T22:06:41Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > > > But I want to open a console to run it in.
> > > 
> > > you mean a pty? check out ext/pty, there’s an example in there.
> > 
> > I want a window to come up with a shell propmt (or dos on windows).
> 
> you probably want something like

Thanks. It worked!!! Check out Ballyhoo balloon 😉

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/5 "2006-07-01T21:32:49Z")

</div>

On Sun, 2 Jul 2006 removed\_email\_address@domain.invalid wrote:

> > you mean a pty? check out ext/pty, there’s an example in there.
> 
> I want a window to come up with a shell propmt (or dos on windows).

you probably want something like

fortytwo :~ \> cat a.rb  
require ‘rbconfig’  
require ‘tempfile’

def console command  
arch = Config::CONFIG[‘arch’]

```
 unless arch =~ %r/win/i
   tmp = Tempfile.new Process.pid.to_s
   tmp << command
   tmp << "\n"
   tmp << "bash"
   tmp << "\n"
   tmp.close
   begin
     system "xterm -e 'bash -l -i #{ tmp.path }'"
   ensure
     tmp.close!
   end
 else
   raise 'windoze'
 end

```

end

console ‘date’

i’ll leave the windoze bit to you 😉

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 2:31am UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/6 "2006-07-02T02:31:43Z")

</div>

Windoze equivalent inserted below 🙂

On 7/1/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> tmp << command
> tmp << "\n"
> tmp << "bash"
> tmp << "\n"
> tmp.close
> begin
> system "xterm -e 'bash -l -i #{ tmp.path }'"
> ensure
> tmp.close!
> end
> else
> 
> ```

```
      system "start cmd /k #{command}"

```

> ```
> end
> 
> ```
> 
> end
> 
> console ‘date’

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/7 "2006-07-02T17:46:51Z")

</div>

Beware that you’re technically detecting both windows and mac (darwin)  
with /win/. You might want to check for win32 (or, i suppose, win64?)  
instead.

- james

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/8 "2006-07-02T19:59:01Z")

</div>

James A. wrote:

> Beware that you’re technically detecting both windows and mac (darwin)  
> with /win/. You might want to check for win32 (or, i suppose, win64?)  
> instead.

Thanks. I’ll change that. How does one do this on mac btw?

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/9 "2006-07-02T20:23:57Z")

</div>

On Jul 2, 2006, at 2:02 PM, removed\_email\_address@domain.invalid wrote:

> > Thanks. I’ll change that. How does one do this on mac btw?
> 
> - h.h. the 14th dali lama

Eh, yeah but it’s not ideal (You’ll have to wait for X11 to start  
usually, which it may or may not do automatically, I’m not sure. i  
think it might if opened via the Finder but not from the command  
line).

What I would do is

IO.popen(“osascript”, “w”) do |io|  
io.puts \<\<APPLESCRIPT  
tell Application “Terminal”  
activate  
do script “#{command\_here}”  
end tell  
APPLESCRIPT  
end

This has the advantage of not opening a new instance of terminal if  
one is already open, (but it will if necessary) and not requiring the  
user to a) have X11 installed and b) have it running.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/10 "2006-07-02T20:48:06Z")

</div>

Logan C. wrote:

> activate  
> do script “#{command\_here}”  
> end tell  
> APPLESCRIPT  
> end
> 
> This has the advantage of not opening a new instance of terminal if  
> one is already open, (but it will if necessary) and not requiring the  
> user to a) have X11 installed and b) have it running.

Thanks. I added it to my balloon. Maybe it works?

[http://balloon.hobix.com/ballyhoo](http://balloon.hobix.com/ballyhoo)

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/11 "2006-07-02T20:05:29Z")

</div>

On Mon, 3 Jul 2006 removed\_email\_address@domain.invalid wrote:

> James A. wrote:
> 
> > Beware that you’re technically detecting both windows and mac (darwin)  
> > with /win/. You might want to check for win32 (or, i suppose, win64?)  
> > instead.
> 
> Thanks. I’ll change that. How does one do this on mac btw?
> 
> T.

the xterm command should work fine.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 2, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/how-to-open-a-console-and-ruby-a-ruby-script-from-ruby/96775/12 "2006-07-02T21:28:52Z")

</div>

On Jul 2, 2006, at 2:45 PM, removed\_email\_address@domain.invalid wrote:

> > ```
> > io.puts <<APPLESCRIPT
> > 
> > ```
> 
> Thanks. I added it to my balloon. Maybe it works?
> 
> [http://balloon.hobix.com/ballyhoo](http://balloon.hobix.com/ballyhoo)
> 
> T.

Well, it doesn’t work but not because of terminals not opening. You  
have a syntax error in the actual page

io.puts \<\< APPLESCRIPT

should be

io.puts \<\<APPLESCRIPT # it’s a heredoc
