Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > equivalent idiom for ruby " perl -pe 's/(\d+)/localtime($1)/e'"

Reply
Thread Tools

equivalent idiom for ruby " perl -pe 's/(\d+)/localtime($1)/e'"

 
 
Peņa, Botp
Guest
Posts: n/a
 
      03-21-2005
Hi,

forgive my perl ignorance, but how do you convert the ff perl idiom to
ruby(way):

perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

thank you
-botp


 
Reply With Quote
 
 
 
 
Jim Freeze
Guest
Posts: n/a
 
      03-21-2005
* "Peņa, Botp" <(E-Mail Removed)> [2005-03-21 12:14:31 +0900]:

> Hi,
>
> forgive my perl ignorance, but how do you convert the ff perl idiom to
> ruby(way):
>
> perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log


I think it would be

ruby -pe '$_.gsub!(/(\d+)/, Time.local("\\1".to_i).to_s)' /var..access.log

--
Jim Freeze
Code Red. Code Ruby



 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      03-21-2005
On Mar 20, 2005, at 9:14 PM, Peņa, Botp wrote:

> Hi,
>
> forgive my perl ignorance, but how do you convert the ff perl idiom to
> ruby(way):
>
> perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log


ruby -pe 'gsub!(/\d+/) { |t| Time.at(t.to_i) }'
/var/log/squid/access.log

I think.

James Edward Gray II




 
Reply With Quote
 
Jason Sweat
Guest
Posts: n/a
 
      03-21-2005
On Mon, 21 Mar 2005 12:51:43 +0900, James Edward Gray II
<(E-Mail Removed)> wrote:
> On Mar 20, 2005, at 9:14 PM, Peņa, Botp wrote:
>
> > Hi,
> >
> > forgive my perl ignorance, but how do you convert the ff perl idiom to
> > ruby(way):
> >
> > perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

>
> ruby -pe 'gsub!(/\d+/) { |t| Time.at(t.to_i) }'
> /var/log/squid/access.log
>
> I think.
>
> James Edward Gray II
>
>


cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
Time.at($1.to_i).to_s)'

seemed to work for me

Regards,
Jason
http://blog.casey-sweat.us/



 
Reply With Quote
 
Michael Campbell
Guest
Posts: n/a
 
      03-21-2005
> cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
> Time.at($1.to_i).to_s)'


No need for "cat" there.


 
Reply With Quote
 
Jason Sweat
Guest
Posts: n/a
 
      03-21-2005
On Mon, 21 Mar 2005 14:04:10 +0900, Michael Campbell
<(E-Mail Removed)> wrote:
> > cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
> > Time.at($1.to_i).to_s)'

>
> No need for "cat" there.


Yes, I started from what Jim Freeze posted above, and switched to the
"cat" style during testing of the Time stuff, never switched it back.
I see that

ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)' /var/log/squid/access.log

works as well.

Regards,
Jason


 
Reply With Quote
 
WATANABE Hirofumi
Guest
Posts: n/a
 
      03-21-2005
Hi,

Jason Sweat <(E-Mail Removed)> writes:

> I see that
>
> ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)' /var/log/squid/access.log
>
> works as well.


$1 becomes a result of the match last time, beause $1 is
evaluated before gsub! is called.

% ruby -e '1111111111.upto(1111111115){|i| puts i}' |\
ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)'
Thu Jan 01 09:00:00 JST 1970
Fri Mar 18 10:58:31 JST 2005
Fri Mar 18 10:58:32 JST 2005
Fri Mar 18 10:58:33 JST 2005
Fri Mar 18 10:58:34 JST 2005

So you should use a block.

% ruby -e '1111111111.upto(1111111115){|i| puts i}' |\
ruby -pe '$_.gsub!(/^(\d+)/){Time.at($1.to_i).to_s}'
Fri Mar 18 10:58:31 JST 2005
Fri Mar 18 10:58:32 JST 2005
Fri Mar 18 10:58:33 JST 2005
Fri Mar 18 10:58:34 JST 2005
Fri Mar 18 10:58:35 JST 2005

Golf:

% echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at($&.to_i)}'
Fri Mar 18 10:58:31 JST 2005

--
eban


 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      03-21-2005
WATANABE Hirofumi wrote:

> Golf:
>
> % echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at($&.to_i)}'
> Fri Mar 18 10:58:31 JST 2005


Hard to do much here, but here's two characters:

> % echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at$_.to_i}'

 
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
Ruby idiom equivalent to Perl "$asdf = $ARGV[0] || die 'argrequired'"? Giles Bowkett Ruby 15 12-05-2007 03:37 AM
Does RUBY have the equivalent of PERL's formats? joe@via.net Ruby 1 11-26-2005 08:02 AM
Equivalent ruby for this perl? Ezra Zygmuntowicz Ruby 2 08-01-2005 07:24 PM
Ruby on Rails or Perl's Maypole..is there a Python equivalent Gary Nutbeam Python 12 03-07-2005 04:40 PM
perl pos function equivalent in ruby? John W. Long Ruby 4 04-13-2004 04:01 PM



Advertisments