Calling ruby scripts

Discussion in 'Ruby' started by Robert Peirce, Mar 5, 2004.

  1. --Apple-Mail-8--791440180
    Content-Transfer-Encoding: quoted-printable
    Content-Type: text/plain;
    charset=ISO-8859-1;
    format=flowed

    Usually I can put

    #! /usr/bin/env ruby

    in the first line of a script and it works fine. However, this doesn't=20=

    work:

    #! /usr/bin/env ruby

    class Hello
    attr_reader :msg
    def initialize
    @msg =3D "Hello, World"
    end
    end

    h =3D Hello.new
    puts h.msg

    $ hello
    : No such file or directory

    However,

    ruby hello

    works fine. What am I missing?


    Bob Peirce Venetia, PA =20=

    724-941-6883
    [HOME (Mac)]
    [OFFICE]

    There is=A0 only one basic human=A0 right, the=A0 right to do as you =
    damn well
    please.=A0 And with it comes the only basic human duty, the duty to take
    the consequences.=A0 -- P.J. O'Rourke

    --Apple-Mail-8--791440180--
     
    Robert Peirce, Mar 5, 2004
    #1
    1. Advertising

  2. Robert Peirce

    Joe Mason Guest

    In article <>, Robert Peirce wrote:
    > in the first line of a script and it works fine. However, this doesn't=20=
    >
    > work:
    >
    > #! /usr/bin/env ruby


    <snip>

    > However,
    >
    > ruby hello
    >
    > works fine. What am I missing?


    What happens if you type "/usr/bin/env ruby --version" on the command
    line? Is it possible you've seen it work on other computers, but env
    isn't installed on this particular one?

    Joe
     
    Joe Mason, Mar 5, 2004
    #2
    1. Advertising

  3. if you are on linux/unix

    > works fine. What am I missing?


    the ./ (dot slash) to indicate where you executable resides
    $chmod +x hello
    $./hello

    Robert Peirce wrote:

    > Usually I can put
    >
    > #! /usr/bin/env ruby
    >
    > in the first line of a script and it works fine. However, this
    > doesn't work:
    >
    > #! /usr/bin/env ruby
    >
    > class Hello
    > attr_reader :msg
    > def initialize
    > @msg = "Hello, World"
    > end
    > end
    >
    > h = Hello.new
    > puts h.msg
    >
    > $ hello
    > : No such file or directory
    >
    > However,
    >
    > ruby hello
    >
    >
    >
    >
    > Bob Peirce Venetia,
    > PA 724-941-6883
    > [HOME (Mac)]
    > [OFFICE]
    >
    > There is only one basic human right, the right to do as you damn well
    > please. And with it comes the only basic human duty, the duty to take
    > the consequences. -- P.J. O'Rourke
    >
    >


    --
    General Electric - CIAT
    Advanced Engineering Center
    ________________________________________
    Rodrigo Bermejo
    Information Technologies.
    Special Applications
    Dial-comm : *879-0644
    Phone :(+52) 442-196-0644
     
    Bermejo, Rodrigo, Mar 5, 2004
    #3
  4. On Mar 4, 2004, at 7:56 PM, Bermejo, Rodrigo wrote:

    > if you are on linux/unix
    >
    >> works fine. What am I missing?

    >
    > the ./ (dot slash) to indicate where you executable resides
    > $chmod +x hello
    > $./hello
    >

    That's not it. I have hello and tst1 through tst7. They are all
    executable and all have the same first line. All but hello work with a
    direct call. There is something about this script that is causing a
    problem:

    $ cat hello
    #! /usr/bin/env ruby

    class Hello
    attr_reader :msg
    def initialize
    @msg = "Hello, World"
    end
    end

    h = Hello.new
    puts h.msg
     
    Robert Peirce, Mar 5, 2004
    #4
  5. On Mar 4, 2004, at 8:09 PM, Joe Mason wrote:

    > In article <>,
    > Robert Peirce wrote:
    >
    > What happens if you type "/usr/bin/env ruby --version" on the command
    > line? Is it possible you've seen it work on other computers, but env
    > isn't installed on this particular one?
    >

    That's not it. I am using the same first line in several other scripts.

    $ /usr/bin/env ruby --version
    ruby 1.6.8 (2002-12-24) [powerpc-darwin7.0]
     
    Robert Peirce, Mar 5, 2004
    #5
  6. Robert Peirce

    Guest

    Hi,

    At Fri, 5 Mar 2004 09:44:55 +0900,
    Robert Peirce wrote in [ruby-talk:94276]:
    > $ hello
    > : No such file or directory


    I vote line ending code issue.

    --
    Nobu Nakada
     
    , Mar 5, 2004
    #6
  7. On Mar 4, 2004, at 8:33 PM, wrote:

    > Hi,
    >
    > At Fri, 5 Mar 2004 09:44:55 +0900,
    > Robert Peirce wrote in [ruby-talk:94276]:
    >> $ hello
    >> : No such file or directory

    >
    > I vote line ending code issue.
    >
    > --
    > Nobu Nakada


    That was it!! Hello came from a Windows machine where lines end in
    \n\r. I was trying to run it on a Unix machine where lines end in \n.
    I ran 'cat hello | tr -d '\r' > tst8', made tst8 executable and it ran.
    Thanks.

    Apparently, ruby itself ignores this, which is why 'ruby hello' worked.
     
    Robert Peirce, Mar 5, 2004
    #7
  8. Robert Peirce

    Guest

    Hi,

    At Fri, 5 Mar 2004 10:44:13 +0900,
    Robert Peirce wrote in [ruby-talk:94288]:
    > Apparently, ruby itself ignores this, which is why 'ruby hello' worked.


    Yes, but the OS kernel doesn't nor env doesn't strip it. You
    can put a space at end of the line.

    --
    Nobu Nakada
     
    , Mar 5, 2004
    #8
  9. Robert Peirce <> wrote:
    > I ran 'cat hello | tr -d '\r' > tst8', made tst8 executable and it ran.


    Have a look at the command "recode", which is availiable on most linux
    systems, see http://recode.progiciels-bpi.ca/manual/IBM-PC.html.

    "recode pc hello" should do for your case.

    Cheers
    Sascha
     
    Sascha D?rdelmann, Mar 5, 2004
    #9
  10. On Mar 5, 2004, at 3:49 AM, Sascha D?rdelmann wrote:

    > Robert Peirce <> wrote:
    >> I ran 'cat hello | tr -d '\r' > tst8', made tst8 executable and it
    >> ran.

    >
    > Have a look at the command "recode", which is availiable on most linux
    > systems, see http://recode.progiciels-bpi.ca/manual/IBM-PC.html.
    >
    > "recode pc hello" should do for your case.


    I'm running BSD on a Mac PowerBook G4. However, Uwin (Unix for
    Windows) has the -d and -D flags to cat that can add/remove '\r'.

    I suppose it would be easy to write a shell script, called recode that
    would do these things. I already have one called rename that allows,
    for example, changing file1..file9 to file01..file09.

    One of the beauties of Unix and its many variants is that it makes text
    manipulation easy. Ruby seems to have similar, maybe even more
    powerful, capabilities in this area, which is one of the reasons I am
    trying to learn it.
     
    Robert Peirce, Mar 5, 2004
    #10
  11. Converting Line ends using Ruby (Was: Calling ruby scripts)

    * Robert Peirce; Fri, 5 Mar 2004 10:44:13 +0900

    > On Mar 4, 2004, at 8:33 PM, wrote:
    > I ran 'cat hello | tr -d '\r' > tst8', made tst8 executable and it
    > ran.


    Any to any conversion:

    DOS -> UNIX: ruby -p -i.bak -e '$_.sub!(/\r$/,"")' file_name
    DOS -> Mac: ruby -n -i.bak -e 'print $_.chomp+"\r"'

    UNIX -> DOS: ruby -p -i.bak -e '$_.sub!(/$/,"\r")' file_name
    UNIX -> Mac: ruby -n -i.bak -e 'print $_.chomp+"\r"'

    Mac -> UNIX: ruby -p -i.bak -e '$_.gsub!(/\r/,"\n")' file_name
    Mac -> DOS: ruby -p -i.bak -e '$_.gsub!(/\r/,"\r\n")'

    Josef 'Jupp' Schugt
     
    Josef 'Jupp' Schugt, Mar 7, 2004
    #11
  12. Re: Converting Line ends using Ruby (Was: Calling ruby scripts)

    il 7 Mar 2004 00:36:04 GMT, Josef 'Jupp' Schugt <> ha
    scritto::


    >Any to any conversion:
    >
    >DOS -> UNIX: ruby -p -i.bak -e '$_.sub!(/\r$/,"")' file_name
    >DOS -> Mac: ruby -n -i.bak -e 'print $_.chomp+"\r"'
    >
    >UNIX -> DOS: ruby -p -i.bak -e '$_.sub!(/$/,"\r")' file_name
    >UNIX -> Mac: ruby -n -i.bak -e 'print $_.chomp+"\r"'
    >
    >Mac -> UNIX: ruby -p -i.bak -e '$_.gsub!(/\r/,"\n")' file_name
    >Mac -> DOS: ruby -p -i.bak -e '$_.gsub!(/\r/,"\r\n")'
    >


    you can safely omit $_, just use Kernel#gsub that is $_.gsub by itself
     
    gabriele renzi, Mar 7, 2004
    #12
    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. Jan Danielsson
    Replies:
    8
    Views:
    666
    Mike Meyer
    Jul 22, 2005
  2. Jp Calderone
    Replies:
    0
    Views:
    481
    Jp Calderone
    Jul 21, 2005
  3. davidj411
    Replies:
    0
    Views:
    578
    davidj411
    Jun 27, 2008
  4. Sparky Mat
    Replies:
    5
    Views:
    115
    Sparky Mat
    Feb 26, 2009
  5. Replies:
    13
    Views:
    580
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page