From: Martin DeMello Date: 2007-03-13T17:36:20+09:00 Subject: Re: perl to ruby 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