Article View: pl.comp.lang.ruby
Article #214Re: iteracja po tablicy kazdy z kazdym "ruby-way"
From: Przemysław Kowalczyk
Date: Mon, 02 Oct 2006 20:37
Date: Mon, 02 Oct 2006 20:37
38 lines
1788 bytes
1788 bytes
On Mon, 02 Oct 2006 22:27:02 +0200, Marcin Mielżyński <lopx@gazeta.pl>
wrote:
> 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?
>
> 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
sweet. uzyles conajmniej 3 idiomow Ruby'ego (rozszerzanie klasy
standardowej, metoda upto na liczbie, closure). po prostu piekne. jestem
pod wrazeniem (-:
pzdr
szeryf
Technical Details
Message-ID:
<op.tgs9732ihwe0a4@negative-361apc>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.task.gda.pl!not-for-mailReferences:
<op.tgs6t3unhwe0a4@negative-361apc> <efrsmo$9d5$1@news.onet.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!newsfeed.gazeta.pl!news.task.gda.pl!not-for-mail From: Przemysław Kowalczyk <szeryf@negativeiq.pl> Newsgroups: pl.comp.lang.ruby Subject: Re: iteracja po tablicy kazdy z kazdym "ruby-way" Date: Mon, 02 Oct 2006 22:37:05 +0200 Organization: CI TASK http://www.task.gda.pl/ Lines: 38 Message-ID: <op.tgs9732ihwe0a4@negative-361apc> References: <op.tgs6t3unhwe0a4@negative-361apc> <efrsmo$9d5$1@news.onet.pl> NNTP-Posting-Host: xdsl-10757.wroclaw.dialog.net.pl Mime-Version: 1.0 X-Trace: news.task.gda.pl 1159821478 15922 84.40.237.5 (2 Oct 2006 20:37:58 GMT) X-Complaints-To: abuse@news.task.gda.pl NNTP-Posting-Date: Mon, 2 Oct 2006 20:37:58 +0000 (UTC) User-Agent: Opera Mail/9.02 (Win32) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit