Article View: pl.comp.lang.ruby
Article #213Re: iteracja po tablicy kazdy z kazdym "ruby-way"
From: Marcin Mielżyński
Date: Mon, 02 Oct 2006 20:27
Date: Mon, 02 Oct 2006 20:27
38 lines
1556 bytes
1556 bytes
Przemysław Kowalczyk wrote:
> Witam,
>
> mam taki akademicki problem: jak zrobic jakas operacje na kazdej parze
> elementow z jakiejs listy (tzn. kazdy z kazdym)? na razie robie to tak:
>
> for i in 0..lst.size - 1
> for j in i + 1..lst.size - 1
> do_something(lst [i], lst [j])
> end
> end
>
> ale to wyglada dosc malo rubistycznie. jakies pomysly?
>
> pzdr
> szeryf
class Array
def each_pair
self.each_index do |i|
(i+1).upto(self.size-1) do |j|
yield self[i],self[j]
end
end
end
end
lst = *(1..10)
lst.each_pair do |a,b|
puts "#{a} #{b}"
end
lopex
Technical Details
Message-ID:
<efrsmo$9d5$1@news.onet.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!not-for-mailReferences:
<op.tgs6t3unhwe0a4@negative-361apc>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.onet.pl!not-for-mail From: Marcin Mielżyński <lopx@gazeta.pl> Newsgroups: pl.comp.lang.ruby Subject: Re: iteracja po tablicy kazdy z kazdym "ruby-way" Date: Mon, 02 Oct 2006 22:27:02 +0200 Organization: Onet.pl Lines: 38 Message-ID: <efrsmo$9d5$1@news.onet.pl> References: <op.tgs6t3unhwe0a4@negative-361apc> NNTP-Posting-Host: chello087207016023.chello.pl Mime-Version: 1.0 X-Trace: news.onet.pl 1159820824 9637 87.207.16.23 (2 Oct 2006 20:27:04 GMT) X-Complaints-To: usenet@news.onet.pl NNTP-Posting-Date: Mon, 2 Oct 2006 20:27:04 +0000 (UTC) X-Sender: 5Da2qcG7AWj7WISFX859Php/OS6q4VnB In-Reply-To: <op.tgs6t3unhwe0a4@negative-361apc> User-Agent: Thunderbird 1.5.0.7 (Windows/20060909) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit