From: Fabian Streitel Date: 2009-07-12T02:05:48+09:00 Subject: Re: one line sorting --0015174c14224a85c7046e7119b8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable you mean like [1, 2, 3, 4].find { |x| x >=3D 3 }.odd? (which would return true if the first number found in the array >=3D 3 is o= dd) you don't need to put parentheses around those things, ruby is smart enough to figure out that you want to call #odd? on the result of #find, which uses that block. But you have to put parenthesis around the arguments to find if you use { ... }: [1,2,3,4].inject(0) { some code } # will work [1,2,3,4].inject 0 { some code } # will not Greetz! 2009/7/11 Haris Bogdanovi=C4=87 > Well, I saw one line Haskell sort and thought, maybe it's possible in Rub= y. > I'm just trying out functional programming in Ruby. > Could you just tell me how to do methods on my expression which has block > {} > and evaluates to number object; > do I put () parenthesses around the whole expression and dot after them o= r > somethin else ? > > Thanks > > > "Fabian Streitel" wrote in message > news:2569cf860907110914udbd2981w70e553eb6c74f492@mail.gmail.com... > Well, you could just do > > [7,4,9,55,98,11,42].sort > > ;-) That's much more optimized than anything else you can do in one line.= .. > > What you described is called a "Selection sort" ( > http://en.wikipedia.org/wiki/Selection_sort) > If you really must implement it yourself (why in one line?) > You could try something like this: > > 0.upto(n.size-1) do |i| > lowest_idx =3D i + n[i..-1].index(n[i..-1].min) > n[lowest_idx], n[i] =3D n[i], n[lowest_idx] > end > > Which is already pretty hackishly short and inefficient. ;-) > Greetz! > > > > --0015174c14224a85c7046e7119b8--