[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

perl to ruby

kuLa mk

3/12/2007 11:20:00 PM

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-....

11 Answers

Jano Svitok

3/12/2007 11:52:00 PM

0

On 3/13/07, Marcin Kulisz <marcin.kulisz@gmail.com> 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 -wusr = ARGV[0]id = `cat /etc/passwd|grep #{usr}|cut -d\":\" -f3`.to_iif id>=1000 text=`finger #{usr}` text.gsub!(/\n/, "<br>") puts textelse puts "wal sie na ryj zlodzieju"endalternatively you can do: text=`finger #{usr}`.gsub(/\n/, "<br>")instead of those two lines.puts appends \n, if you don't like it, use print

kuLa mk

3/13/2007 7:26:00 AM

0

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-....

Robert Klemme

3/13/2007 8:06:00 AM

0

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/pr...

Kind regards

robert

kuLa mk

3/13/2007 8:22:00 AM

0

Robert Klemme wrote:
> Also look at this:
>
> http://raa.ruby-lang.org/pr...

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

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

Martin DeMello

3/13/2007 8:36:00 AM

0

On 3/13/07, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 3/13/07, Marcin Kulisz <marcin.kulisz@gmail.com> 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

3/13/2007 8:54:00 AM

0

On 3/13/07, Bruce Woodward <bruce.woodward@gmail.com> 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

Eckie Silapaswang

3/13/2007 4:36:00 PM

0

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-....

Chad Perrin

3/13/2007 7:44:00 PM

0

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.ap... ]
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."

Martin DeMello

3/13/2007 7:47:00 PM

0

On 3/14/07, Chad Perrin <perrin@apotheon.com> 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.

Chad Perrin

3/13/2007 7:59:00 PM

0

On Wed, Mar 14, 2007 at 04:46:43AM +0900, Martin DeMello wrote:
> On 3/14/07, Chad Perrin <perrin@apotheon.com> 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.ap... ]
"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