From: "Hal E. Fulton" Date: 2002-08-29T06:33:50+09:00 Subject: Re: Newbie converting brain from perl ----- Original Message ----- From: "William Pietri" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Wednesday, August 28, 2002 3:31 PM Subject: Newbie converting brain from perl > > Howdy! As a long time Perl, Java, and Objective-C developer, I'm quite taken > with Ruby, and I'm busily converting my brain over. But there's a Perl > idiom that I'm having trouble translating. In Perl it goes like this: > > $date = "28 Aug 2002"; > ($day, $month, $year) = $date =~ /(\d+) (\w+) (\d+)/; > > > In Ruby the best I've been able to do is this: > > date = "28 Aug 2002" > day, month, year = /(\d+) (\w+) (\d+)/.match(date)[1..3] > > But that final [1..3] thing seems inelegant, which makes me think I'm > barking up the wrong tree. What's the best Ruby-style way to do this? Hmm. It does seem inelegant, but I'm not sure how to improve on it. Things like \1 and \2 are only useful in doing substitutions, I suppose. One way, if you don't mind (the new) scanf, would be: day, month, year = date.scanf("%d %s %d") HTH, Hal