Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > perl to ruby

Reply
Thread Tools

perl to ruby

 
 
Marcin Kulisz
Guest
Posts: n/a
 
      03-12-2007
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/.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      03-12-2007
T24gMy8xMy8wNywgTWFyY2luIEt1bGlzeiA8bWFyY2luLmt1bG lzekBnbWFpbC5jb20+IHdyb3Rl
Ogo+IGhpIGFsbCwKPiB0b2RheSBpcyBteSBmaXJzdCBkYXkgd2 l0aCBydWJ5IGFuZCBJJ20gdHJ5
aW5nIHRvIHJld3JpdGUgYSBmZXcgb2YgbXkKPiBzY3JpcHRzIG Zyb20gcGVybCB0byBydWJ5ICYg
aXQncyBub3QgdG9vIGVhc3kuIGNhbiBhbnlvbmUgaGVscCBtZS B3aXRoCj4gc2NyaXB0IGJlbG93
LiBJJ3ZlIGRvbmUgYSBmZXcgdGhpbmtzIGJ1dCB3aGVuIG9uZS BpcyB3b3JraW5nIGFub3RoZXIK
PiBkb24ndC4KCiMhL3Vzci9iaW4vcnVieSAtdwoKdXNyID0gQV JHVlswXQppZCA9IGBjYXQgL2V0
Yy9wYXNzd2R8Z3JlcCAje3Vzcn18Y3V0IC1kXCI6XCIgLWYzYC 50b19pCmlmIGlkPj0xMDAwCiAg
ICB0ZXh0PWBmaW5nZXIgI3t1c3J9YAogICAgdGV4dC5nc3ViIS gvXG4vLCAiPGJyPiIpCiAgICBw
dXRzIHRleHQKZWxzZQogICAgcHV0cyAid2FsIHNp6iBuYSByeW ogerNvZHppZWp1IgplbmQKCmFs
dGVybmF0aXZlbHkgeW91IGNhbiBkbzoKICAgIHRleHQ9YGZpbm dlciAje3Vzcn1gLmdzdWIoL1xu
LywgIjxicj4iKQppbnN0ZWFkIG9mIHRob3NlIHR3byBsaW5lcy 4KCnB1dHMgYXBwZW5kcyBcbiwg
aWYgeW91IGRvbid0IGxpa2UgaXQsIHVzZSBwcmludAo=

 
Reply With Quote
 
 
 
 
Marcin Kulisz
Guest
Posts: n/a
 
      03-13-2007
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/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-13-2007
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
 
Reply With Quote
 
Marcin Kulisz
Guest
Posts: n/a
 
      03-13-2007
Robert Klemme wrote:
> Also look at this:
>
> http://raa.ruby-lang.org/project/etc/


hehe thx, I read about it but sometimes my memory is too short

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-13-2007
On 3/13/07, Jan Svitok <(E-Mail Removed)> wrote:
> On 3/13/07, Marcin Kulisz <(E-Mail Removed)> 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

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-13-2007
On 3/13/07, Bruce Woodward <(E-Mail Removed)> 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

 
Reply With Quote
 
Eckie Silapaswang
Guest
Posts: n/a
 
      03-13-2007
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/.

 
Reply With Quote
 
Chad Perrin
Guest
Posts: n/a
 
      03-13-2007
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."

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-13-2007
On 3/14/07, Chad Perrin <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments