Ruby launching system apps?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Ruby launching system apps?

    I have a UNIX machine and I want a ruby app that can launch UNIX commands
    like awk or sed...how would I implement this?

    use Shell.new?


    Dan Guest

  2. Similar Questions and Discussions

    1. Not launching -- unable to continue because of hardware or system error
      What would cause Photoshop 6.0.1 to give me an error message, right after the loading fonts initializing screen, that says "Unable to continue...
    2. all .NET apps cause System.Security.Policy.PolicyException
      I was testing some configuration options with Framework Configuration tool and eventually something went wrong in .NET security. Now cannot do...
    3. Launching Apps
      I have read through the blurb on opening an exe from director, however, it gives me a script error when trying this. I have tried all of the...
    4. Launching Explorer 5.1.7 crashes my system
      In article <dennmac-ya02408000R0410031745380001@NNTP.InfoAve.Net>, Dennis McGee <dennmac@InfoAveObstacle.Net> wrote: If you have Disk Warrior,...
    5. launching apps from expansion cards
      Are there any 3rd party launchers which allow launching apps from ANY directory on expansion cards and not only from the default /Palm/Launcher...
  3. #2

    Default Re: Ruby launching system apps?

    The simplest way is to use "system" or `` (you can use Shell too, of
    course). ôhe best way is to implement in Ruby all you do with awk and sed,
    as it has (almost) everything those utilities offer.

    Before I found Ruby, my favorite combination was sh/awk/sed. Now I do
    everything of that kind in Ruby, however knowledge and long experience with
    those utilities help me a lot, especially with regexp stuff.

    Gennady.

    ----- Original Message -----
    From: "Dan" <falseflyboy@yahoo.comNONO>
    Newsgroups: comp.lang.ruby
    To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
    Sent: Monday, August 25, 2003 10:25 AM
    Subject: Ruby launching system apps?

    > I have a UNIX machine and I want a ruby app that can launch UNIX commands
    > like awk or sed...how would I implement this?
    >
    > use Shell.new?
    >
    >
    >
    >

    Gennady Guest

  4. #3

    Default Re: Ruby launching system apps?

    Dan wrote:
    >I have a UNIX machine and I want a ruby app that can launch UNIX commands
    >like awk or sed...how would I implement this?
    >
    >use Shell.new?
    >
    >
    `cat some_file` # returns string of stdout
    system 'cat some_file' # returns success (true/false)
    exec 'cat some_file' # replaces current process
    popen 'cat some_file' # returns IO object connected to stdin and stdout

    require 'open3'
    Open3.popen3 'cat some_file' # returns [in,out,err]



    mgarriss Guest

  5. #4

    Default Re: Ruby launching system apps?


    On Monday, August 25, 2003, at 01:25 PM, Dan wrote:
    > I have a UNIX machine and I want a ruby app that can launch UNIX
    > commands
    > like awk or sed...how would I implement this?
    >
    > use Shell.new?
    I've found that when you have a program that makes use of UNIX commands
    within various classes and methods that system and `` calls can lead to
    synchronization problems -- and that such problems can be solved by
    using the Shell#transact method.

    Here's an example:

    class Document

    Shell.def_system_command("lynx")

    attr_reader :path

    def initialize(path)
    @path = path
    end

    def html_to_text_stdout
    # the following line is needed because @path is not in the scope of the
    transact block below
    path = @path
    html_to_text = String.new
    shell = Shell.new
    shell.transact do
    html_to_text = (shell.lynx("-dump", path)).to_s
    end
    html_to_text
    end

    end

    I agree with Gennady that you will frequently be better off using
    native Ruby classes and methods for many things, particularly in-memory
    string manipulation, even if comparable tools are available through a
    Unix command.

    Regards,

    Mark


    Mark Wilson Guest

  6. #5

    Default Re: Ruby launching system apps?

    On Monday, August 25, 2003, at 01:25 PM, Dan wrote:
    > I have a UNIX machine and I want a ruby app that can launch UNIX
    > commands like awk or sed...how would I implement this?
    The Shell module lets you invoke UNIX commands, but bear in mind
    that doing so is inefficient, not portable to non-UNIX machines,
    and likely to cause thread synchronization issues. Ruby itself can
    do everything awk and sed can do without having to resort to
    running external programs.

    -Mark
    Mark J. Reed Guest

  7. #6

    Default Re: Ruby launching system apps?


    On Monday, August 25, 2003, at 05:05 PM, Mark J. Reed wrote:
    > On Monday, August 25, 2003, at 01:25 PM, Dan wrote:
    >> I have a UNIX machine and I want a ruby app that can launch UNIX
    >> commands like awk or sed...how would I implement this?
    >
    > The Shell module lets you invoke UNIX commands, but bear in mind
    > that doing so is inefficient,
    Avoid premature optimization. If the inefficiency is a problem,
    implement in Ruby. If it is still a problem, extend Ruby with C.
    > not portable to non-UNIX machines,
    True. The universe of Unix machines is pretty large (including Mac OS
    X). I don't know about Windows with cygwin.
    > and likely to cause thread synchronization issues.
    The Shell#transact method will handle synchronization.
    > Ruby itself can
    > do everything awk and sed can do without having to resort to
    > running external programs.
    > [snip]
    True. 'awk' and 'sed' are the least likely candidates, in my opinion,
    for wrapping in the Shell class. However, if you have a shell script
    with functionality you want to use in a Ruby program that you don't
    want to spend the time reimplementing in Ruby, the Shell class may be
    your best bet, at least initially.

    Regards,

    Mark


    Mark Wilson Guest

  8. #7

    Default Re: Ruby launching system apps?

    Hi --

    On Tue, 26 Aug 2003, Lothar Scholz wrote:
    >
    > > Avoid premature optimization. If the inefficiency is a problem,
    >
    > I hear this a thousand times, but this makes it not more truth.
    > You should only avoid optimization if it results in a more complex
    > structure. This is only in a few cases the reality. Often an optimal
    > datastructure doesn't take longer to debug or to write.
    Then it isn't premature :-)


    David

    --
    David Alan Black
    home: [email]dblack@superlink.net[/email]
    work: [email]blackdav@shu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  9. #8

    Default Re: Ruby launching system apps?



    >
    >
    >>
    >>
    >> I agree with Gennady that you will frequently be better off using
    >> native Ruby classes and methods for many things, particularly
    >> in-memory string manipulation, even if comparable tools are available
    >> through a Unix command.
    >>
    >> Regards,
    >>
    >> Mark
    >>
    > on ruby 1.6.8
    >
    > time ruby -p -e 'gsub("sed","ruby")' file_with_1000000_sed_lines
    > 7.61s user 2.02s system 35% cpu 27.161 total
    >
    > time sed s/sed/ruby/g file_with_1000000_sed_lines
    > 2.05s user 1.71s system 32% cpu 11.571 total
    >
    > ...I expected other results...but (I hope) it's improved on 1.8 (
    > I could not test it now on 1.8 )
    >
    > -r.
    >
    >
    >
    >
    >
    >


    Bermejo, Rodrigo Guest

  10. #9

    Default Re: Ruby launching system apps?

    On 2003-08-26 23:03:55 +0900, Bermejo, Rodrigo wrote:
    > on ruby 1.6.8
    >
    > time ruby -p -e 'gsub("sed","ruby")' file_with_1000000_sed_lines
    > 7.61s user 2.02s system 35% cpu 27.161 total
    >
    > time sed s/sed/ruby/g file_with_1000000_sed_lines
    > 2.05s user 1.71s system 32% cpu 11.571 total
    >
    > ...I expected other results...but (I hope) it's improved on 1.8 (
    > I could not test it now on 1.8 )
    I don't think that it is fair to compare ruby with sed because Ruby's
    regular expressions are better than extended REs (ERE) while sed seems
    to support at most basic regular expressions (BRE). If you only need a
    subset it's no wonder that sed is faster and a good choice for
    optimization. Compared to Perl's RE Ruby is not too bad and I suppose
    that Perl's RE are very highly optimized for speed.

    [flori@lambda big](130)$ echo sed: ; time sed s/sed/ruby/g sed.txt > /dev/null ; echo ruby: ; time ruby -p -e 'gsub(/sed/,%q/ruby/)' sed.txt > /dev/null ; echo perl: ; time perl -p -e 's/sed/ruby/g' sed.txt > /dev/null
    sed:

    real 0m1.793s
    user 0m1.670s
    sys 0m0.016s
    ruby:

    real 0m13.141s
    user 0m11.953s
    sys 0m0.131s
    perl:

    real 0m9.773s
    user 0m9.109s
    sys 0m0.104s

    [flori@lambda flori](0)$ ruby -e 'puts "%.2f" % ((11.953 / 9.109 - 1) * 100)'
    31.22

    Perl is ca. 30% faster than Ruby, that's not too much. Given the fact
    that in Ruby regular expressions are first class citizens and full
    flexed objects after beeing created, while you have to use work arounds
    like qr/.../ if you want to pass them around in Perl.

    --
    It is by the goodness of God that in our country we have those three
    unspeakably precious things: freedom of speech, freedom of conscience, and
    the prudence never to practice either.
    -- Mark Twain, More Tramps Abroad

    Florian Frank Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139