How to tell if script running under rubyw vs ruby?

Discussion in 'Ruby' started by Joe Swatosh, May 10, 2008.

  1. Joe Swatosh

    Joe Swatosh Guest

    I spent some time poking around in Config, but so far this is the the
    only thing I've found that works:

    begin
    puts 'this is a test'
    # here is ruby
    rescue
    # here is rubyw
    end

    And I really don't love the side effect when using ruby. Please tell
    me there is a better way.

    Thanks,
    --
    Joe Swatosh
     
    Joe Swatosh, May 10, 2008
    #1
    1. Advertising

  2. On Sat, May 10, 2008 at 6:59 PM, Joe Swatosh <> wrote:
    > I spent some time poking around in Config, but so far this is the the
    > only thing I've found that works:
    >
    > begin
    > puts 'this is a test'
    > # here is ruby
    > rescue
    > # here is rubyw
    > end
    >
    > And I really don't love the side effect when using ruby. Please tell
    > me there is a better way.
    >
    > Thanks,
    > --
    > Joe Swatosh
    >
    >

    As you've found out, stdout doesn't work under rubyw because all three
    usual streams (stdout, stdin and stderr) are closed in a Windows exe.
    I don't have a Window box handy to test, but you could try checking
    the value of STDOUT under rubyw - it's probably nil or something like
    that or you may get .eof? == true. Only a heuristic, but should serve
    to distinguish between your two use cases.

    Regards,
    Sean
     
    Sean O'Halpin, May 10, 2008
    #2
    1. Advertising

  3. Sean O'Halpin wrote:
    > I don't have a Window box handy to test, but you could try checking
    > the value of STDOUT under rubyw - it's probably nil or something like
    > that or you may get .eof? == true.


    Alternatively you could check for STDOUT.closed? or !STDOUT.tty?
    One of those three should work at least.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, May 11, 2008
    #3
  4. Hi,

    At Sun, 11 May 2008 02:59:07 +0900,
    Joe Swatosh wrote in [ruby-talk:301402]:
    > I spent some time poking around in Config, but so far this is the the
    > only thing I've found that works:


    It depends on what you want to know. If it is

    a) whether running with or without a console window, you can't
    open "CONIN$" and "CONOUT$" without it.

    begin
    open("CONIN$") {}
    # here is ruby
    rescue
    # here is rubyw
    end

    b) whether running with opened STDIN/STDOUT/STDERR, STDIN.stat
    and so on fail with EBADF if it is not opened.

    c) or, whether running executable is linked in GUI mode or CUI
    mode, you might have to parse the exe header. (imagehlp.dll
    or something may be needed.)

    --
    Nobu Nakada
     
    Nobuyoshi Nakada, May 11, 2008
    #4
  5. Joe Swatosh

    Joe Swatosh Guest

    On Sun, May 11, 2008 at 8:06 AM, Nobuyoshi Nakada <> wrote:
    > Hi,
    >
    > At Sun, 11 May 2008 02:59:07 +0900,
    > Joe Swatosh wrote in [ruby-talk:301402]:
    > > I spent some time poking around in Config, but so far this is the the
    > > only thing I've found that works:

    >
    > It depends on what you want to know. If it is
    >
    > a) whether running with or without a console window, you can't
    > open "CONIN$" and "CONOUT$" without it.
    >
    > begin
    > open("CONIN$") {}
    > # here is ruby
    > rescue
    > # here is rubyw
    > end
    >
    > b) whether running with opened STDIN/STDOUT/STDERR, STDIN.stat
    > and so on fail with EBADF if it is not opened.
    >
    > c) or, whether running executable is linked in GUI mode or CUI
    > mode, you might have to parse the exe header. (imagehlp.dll
    > or something may be needed.)
    >
    > --
    > Nobu Nakada
    >
    >


    Thanks Sean.
    I should have mentioned that testing for defined?, nil?, and eof?
    weren't helpful.

    Thanks Sebastian.
    No joy on .tty? or .closed? either.

    Nobu rocks again!
    It is b) We are using some gems that might puts or warn when they are
    loaded, so when running under rubyw we are planning to redirect them
    to a log file, but we don't want to mess with anything when running
    under ruby.

    Thanks again everyone.

    --
    Joe
     
    Joe Swatosh, May 12, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathaniel Talbott

    Detecting rubyw

    Nathaniel Talbott, Nov 17, 2004, in forum: Ruby
    Replies:
    6
    Views:
    135
    Nathaniel Talbott
    Nov 17, 2004
  2. Matt Mower
    Replies:
    6
    Views:
    270
    Shashank Date
    Feb 25, 2005
  3. ctyu
    Replies:
    4
    Views:
    143
  4. Mike A
    Replies:
    17
    Views:
    280
    Dr John Stockton
    Nov 19, 2003
  5. Steven D'Aprano

    How do I tell if I'm running under IDLE?

    Steven D'Aprano, Apr 5, 2013, in forum: Python
    Replies:
    6
    Views:
    102
    Dave Angel
    Apr 7, 2013
Loading...

Share This Page