perl to ruby

Discussion in 'Ruby' started by Marcin Kulisz, Mar 12, 2007.

  1. hi all,
    today is my first day with ruby and I'm trying to rewrite a few of my
    scripts from perl to ruby & it's not too easy. can anyone help me with
    script below. I've done a few thinks but when one is working another
    don't.

    > #!/usr/bin/perl -w
    >
    > $usr=$ARGV[0];
    > $id=`cat /etc/passwd|grep $usr|cut -d\":\" -f3`;
    > if ($id>=1000){
    > $text=`finger $usr`;
    > $text=~s/\n/\<br\>/g;
    > print $text;
    > } else {
    > print "wal się na ryj złodzieju";
    > }


    --
    Posted via http://www.ruby-forum.com/.
     
    Marcin Kulisz, Mar 12, 2007
    #1
    1. Advertising

  2. Marcin Kulisz

    Jan Svitok Guest

    T24gMy8xMy8wNywgTWFyY2luIEt1bGlzeiA8bWFyY2luLmt1bGlzekBnbWFpbC5jb20+IHdyb3Rl
    Ogo+IGhpIGFsbCwKPiB0b2RheSBpcyBteSBmaXJzdCBkYXkgd2l0aCBydWJ5IGFuZCBJJ20gdHJ5
    aW5nIHRvIHJld3JpdGUgYSBmZXcgb2YgbXkKPiBzY3JpcHRzIGZyb20gcGVybCB0byBydWJ5ICYg
    aXQncyBub3QgdG9vIGVhc3kuIGNhbiBhbnlvbmUgaGVscCBtZSB3aXRoCj4gc2NyaXB0IGJlbG93
    LiBJJ3ZlIGRvbmUgYSBmZXcgdGhpbmtzIGJ1dCB3aGVuIG9uZSBpcyB3b3JraW5nIGFub3RoZXIK
    PiBkb24ndC4KCiMhL3Vzci9iaW4vcnVieSAtdwoKdXNyID0gQVJHVlswXQppZCA9IGBjYXQgL2V0
    Yy9wYXNzd2R8Z3JlcCAje3Vzcn18Y3V0IC1kXCI6XCIgLWYzYC50b19pCmlmIGlkPj0xMDAwCiAg
    ICB0ZXh0PWBmaW5nZXIgI3t1c3J9YAogICAgdGV4dC5nc3ViISgvXG4vLCAiPGJyPiIpCiAgICBw
    dXRzIHRleHQKZWxzZQogICAgcHV0cyAid2FsIHNp6iBuYSByeWogerNvZHppZWp1IgplbmQKCmFs
    dGVybmF0aXZlbHkgeW91IGNhbiBkbzoKICAgIHRleHQ9YGZpbmdlciAje3Vzcn1gLmdzdWIoL1xu
    LywgIjxicj4iKQppbnN0ZWFkIG9mIHRob3NlIHR3byBsaW5lcy4KCnB1dHMgYXBwZW5kcyBcbiwg
    aWYgeW91IGRvbid0IGxpa2UgaXQsIHVzZSBwcmludAo=
     
    Jan Svitok, Mar 12, 2007
    #2
    1. Advertising

  3. Jan Svitok wrote:

    > #!/usr/bin/ruby -w
    >
    > usr = ARGV[0]

    ...
    > puts appends \n, if you don't like it, use print


    thx a lot, I was very close to it but had probles with grep, first I
    tired to do it with command "system" but it doesn't working then change
    my mind & tired with method ".grep" & it doesn't work too but your
    solution is easy & lovely
    again thx for help

    --
    Posted via http://www.ruby-forum.com/.
     
    Marcin Kulisz, Mar 13, 2007
    #3
  4. On 13.03.2007 08:26, Marcin Kulisz wrote:
    > Jan Svitok wrote:
    >
    >> #!/usr/bin/ruby -w
    >>
    >> usr = ARGV[0]

    > ..
    >> puts appends \n, if you don't like it, use print

    >
    > thx a lot, I was very close to it but had probles with grep, first I
    > tired to do it with command "system" but it doesn't working then change
    > my mind & tired with method ".grep" & it doesn't work too but your
    > solution is easy & lovely
    > again thx for help


    Also look at this:

    http://raa.ruby-lang.org/project/etc/

    Kind regards

    robert
     
    Robert Klemme, Mar 13, 2007
    #4
  5. Marcin Kulisz, Mar 13, 2007
    #5
  6. On 3/13/07, Jan Svitok <> wrote:
    > On 3/13/07, Marcin Kulisz <> wrote:
    > > hi all,
    > > today is my first day with ruby and I'm trying to rewrite a few of my
    > > scripts from perl to ruby & it's not too easy. can anyone help me with
    > > script below. I've done a few thinks but when one is working another
    > > don't.

    >
    > #!/usr/bin/ruby -w
    >
    > usr = ARGV[0]
    > id = `cat /etc/passwd|grep #{usr}|cut -d\":\" -f3`.to_i


    And in pure ruby,

    id = IO.readlines('/etc/passwd').grep(/#{usr}/).first.split(/:/).at(2).to_i

    or for efficiency

    File.open("/etc/passwd", "r") {|f| id =
    f.grep(/#{usr}/).first.split(/:/).at(2).to_i}

    which doesn't read the whole file into an array first.

    Also, ideally grep(/#{usr}/) should be
    grep(Regexp.new(Regexp.quote(usr))), in case usr contains
    special-to-the-regexp-engine characters.

    martin
     
    Martin DeMello, Mar 13, 2007
    #6
  7. On 3/13/07, Bruce Woodward <> wrote:
    > For efficiency, wouldn't using the Etc module would be best?
    >
    > require 'etc'
    > user = ARGV[0] or abort "missing user name"
    > puts (if (Etc.getpwnam(user)).uid >= 1000


    touche' :)

    martin
     
    Martin DeMello, Mar 13, 2007
    #7
  8. Marcin Kulisz wrote:
    > Jan Svitok wrote:
    >
    >> #!/usr/bin/ruby -w
    >>
    >> usr = ARGV[0]

    > ...
    >> puts appends \n, if you don't like it, use print

    >
    > thx a lot, I was very close to it but had probles with grep, first I
    > tired to do it with command "system" but it doesn't working then change
    > my mind & tired with method ".grep" & it doesn't work too but your
    > solution is easy & lovely
    > again thx for help


    Just for future reference (and I found this out the hard way by having
    it not working on me either) the
    system("insert_your_shell_command_here") method will return a boolean if
    your shell can execute the command (1 or 0, true or false). You can use
    the grep command, but as you saw in the solution, you use backticks ` `
    to have system commands in your ruby script.

    Best regards,

    Eckie


    --
    Posted via http://www.ruby-forum.com/.
     
    Eckie Silapaswang, Mar 13, 2007
    #8
  9. Marcin Kulisz

    Chad Perrin Guest

    On Tue, Mar 13, 2007 at 05:36:20PM +0900, Martin DeMello wrote:
    >
    > or for efficiency
    >
    > File.open("/etc/passwd", "r") {|f| id =
    > f.grep(/#{usr}/).first.split(/:/).at(2).to_i}


    . . or for more readability:

    File.open("/etc/passwd", "r") do |f|
    id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
    end

    I'm sure greater readability can be had by rewriting that entirely, but
    I found the multiline braces approach suboptimal, personally. YMMV.

    One could also split up that string of methods into more than one line
    via assignment to variables, but I'm lazy enough to prefer to spend
    twice as much effort talking about why I didn't.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    Leon Festinger: "A man with a conviction is a hard man to change. Tell
    him you disagree and he turns away. Show him facts and figures and he
    questions your sources. Appeal to logic and he fails to see your point."
     
    Chad Perrin, Mar 13, 2007
    #9
  10. On 3/14/07, Chad Perrin <> wrote:
    > On Tue, Mar 13, 2007 at 05:36:20PM +0900, Martin DeMello wrote:
    > >
    > > or for efficiency
    > >
    > > File.open("/etc/passwd", "r") {|f| id =
    > > f.grep(/#{usr}/).first.split(/:/).at(2).to_i}

    >
    > . . . or for more readability:
    >
    > File.open("/etc/passwd", "r") do |f|
    > id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
    > end
    >
    > I'm sure greater readability can be had by rewriting that entirely, but
    > I found the multiline braces approach suboptimal, personally. YMMV.


    And the fact that my paste put a line break where it did didn't help
    :) that was supposed to be on one line - didn't realise how long it
    had gotten.

    m.
     
    Martin DeMello, Mar 13, 2007
    #10
  11. Marcin Kulisz

    Chad Perrin Guest

    On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:
    > On 3/14/07, Chad Perrin <> wrote:
    > >On Tue, Mar 13, 2007 at 05:36:20PM +0900, Martin DeMello wrote:
    > >>
    > >> or for efficiency
    > >>
    > >> File.open("/etc/passwd", "r") {|f| id =
    > >> f.grep(/#{usr}/).first.split(/:/).at(2).to_i}

    > >
    > >. . . or for more readability:
    > >
    > > File.open("/etc/passwd", "r") do |f|
    > > id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
    > > end
    > >
    > >I'm sure greater readability can be had by rewriting that entirely, but
    > >I found the multiline braces approach suboptimal, personally. YMMV.

    >
    > And the fact that my paste put a line break where it did didn't help
    > :) that was supposed to be on one line - didn't realise how long it
    > had gotten.


    Hmm. It should have occurred to me that was meant to be all one line.

    I think lines that long should usually be broken up for readability
    anyway. Usually.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "The first rule of magic is simple. Don't waste your time waving your
    hands and hopping when a rock or a club will do." - McCloctnick the Lucid
     
    Chad Perrin, Mar 13, 2007
    #11
  12. On Mar 13, 2007, at 3:59 PM, Chad Perrin wrote:

    > On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:
    >> On 3/14/07, Chad Perrin <> wrote:
    >>> On Tue, Mar 13, 2007 at 05:36:20PM +0900, Martin DeMello wrote:
    >>>>
    >>>> or for efficiency
    >>>>
    >>>> File.open("/etc/passwd", "r") {|f| id =
    >>>> f.grep(/#{usr}/).first.split(/:/).at(2).to_i}
    >>>
    >>> . . . or for more readability:
    >>>
    >>> File.open("/etc/passwd", "r") do |f|
    >>> id = f.grep(/#{usr}).first.split(/:/).at(2).to_i
    >>> end
    >>>
    >>> I'm sure greater readability can be had by rewriting that
    >>> entirely, but
    >>> I found the multiline braces approach suboptimal, personally. YMMV.

    >>
    >> And the fact that my paste put a line break where it did didn't help
    >> :) that was supposed to be on one line - didn't realise how long it
    >> had gotten.

    >
    > Hmm. It should have occurred to me that was meant to be all one line.
    >
    > I think lines that long should usually be broken up for readability
    > anyway. Usually.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > "The first rule of magic is simple. Don't waste your time waving your
    > hands and hopping when a rock or a club will do." - McCloctnick the
    > Lucid
    >


    File.open("/etc/passwd", "r") do |f|
    id = f.detect {|line| line =~ /\A#{usr}:/}.split(/:/)[2].to_i
    end

    using grep will read all the lines, but detect will stop after
    finding the first line for which the block is true. I also suggest
    bracketing your regexp with \A (beginning of string) and
    ':' (separator in /etc/passwd) so searching for, say, "0", doesn't
    give you the root account.

    You probably also want to rescue exceptions that will arise if the
    usr is not found.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Mar 13, 2007
    #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. dpackwood
    Replies:
    3
    Views:
    1,809
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    305
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    690
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    301
    PerlFAQ Server
    Feb 27, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    697
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page