PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Sort and a multiline block
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sort and a multiline block

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 21h42   #1
Raymond O'Connor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sort and a multiline block

Using sort with a multiline block doesn't seem to work as expected.

Single line blocks work fine:

[3, 7, 4, 1, 8, 2, 8, 9].sort { |a, b| a <=> b }
#returns [1, 2, 3, 4, 7, 8, 8, 9]

[3, 7, 4, 1, 8, 2, 8, 9].sort { |a, b| b <=> a }
#returns [9, 8, 8, 7, 4, 3, 2, 1]



Multiline blocks ALWAYS sort in ascending order for some reason:

[3, 7, 4, 1, 8, 2, 8, 9].sort do |a, b|
a <=> b
end
#returns [1, 2, 3, 4, 7, 8, 8, 9]

[3, 7, 4, 1, 8, 2, 8, 9].sort do |a, b|
b <=> a
end
#returns [1, 2, 3, 4, 7, 8, 8, 9]


In fact even when I code the comparison by hand, it's still always
ascending:

[3, 7, 4, 1, 8, 2, 8, 9].sort do |a, b|
if a < b
1
elsif a > b
-1
else
0
end
end
#returns [1, 2, 3, 4, 7, 8, 8, 9]




Does anyone know what's going wrong with this?

Thanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 21h53   #2
Ilan Berci
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

Raymond O'Connor wrote:
> Using sort with a multiline block doesn't seem to work as expected.
>
> Single line blocks work fine:
>
> [3, 7, 4, 1, 8, 2, 8, 9].sort { |a, b| a <=> b }
> #returns [1, 2, 3, 4, 7, 8, 8, 9]
>
> [3, 7, 4, 1, 8, 2, 8, 9].sort { |a, b| b <=> a }
> #returns [9, 8, 8, 7, 4, 3, 2, 1]
>


works for me.. perhaps it's something else in your code

irb(main):009:0> a = [3,7,4,1,8,2,8,9]
=> [3, 7, 4, 1, 8, 2, 8, 9]
irb(main):010:0> a.sort do |b,c|
irb(main):011:1* b <=> c
irb(main):012:1> end
=> [1, 2, 3, 4, 7, 8, 8, 9]
irb(main):013:0> a.sort do |b,c|
irb(main):014:1* c <=> b
irb(main):015:1> end
=> [9, 8, 8, 7, 4, 3, 2, 1]

ilan
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 06/11/2007, 22h44   #3
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

On Nov 6, 2:42 pm, Raymond O'Connor <nappin...@yahoo.com> wrote:
> [3, 7, 4, 1, 8, 2, 8, 9].sort do |a, b|
> b <=> a
> end
> #returns [1, 2, 3, 4, 7, 8, 8, 9]


I do not get this result; I get what you expected.

What OS are you running, including version?
What version of Ruby? Did you hand compile it, or use an installer.

Can you show 'proof' in the form of a single program that does that
above, showing that you are for sure getting this result with exactly
this code?

  Réponse avec citation
Vieux 06/11/2007, 23h42   #4
Raymond O'Connor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

I'm having the problem using

Ruby 1.8.5 with Mac OS X 10.4.10
Ruby 1.8.6 with Leopard


The interesting thing is when I run the commands on irb, I get the
correct results, but when I run them through Textmate, the result is
always sorted ascending.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 00h19   #5
Collin VanDyck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

On Nov 6, 6:42 pm, Raymond O'Connor <nappin...@yahoo.com> wrote:
> I'm having the problem using
>
> Ruby 1.8.5 with Mac OS X 10.4.10
> Ruby 1.8.6 with Leopard
>
> The interesting thing is when I run the commands on irb, I get the
> correct results, but when I run them through Textmate, the result is
> always sorted ascending.
> --
> Posted viahttp://www.ruby-forum.com/.


I'm on Leopard, and this program:

#!/usr/bin/env ruby

result = [3, 7, 4, 1, 8, 2, 8, 9].sort do |a, b|
b <=> a
end
p result

Run from within TextMate gives me

[9, 8, 8, 7, 4, 3, 2, 1]

I think that you've probably got something else going on in your
program. As someone else said, post a short program in its entirety
that will illustrate what you're seeing.




  Réponse avec citation
Vieux 07/11/2007, 01h09   #6
Raymond O'Connor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

When I store the result of the sort, everything is fine:

Correct
result = [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
b <=> a
end

p result #[9, 8, 8, 7, 4, 3, 2, 1]




But when just do a p on the sort, the problem occurs

Incorrect
p [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
b <=> a
end #[1, 2, 3, 4, 7, 8, 8, 9]

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 03h22   #7
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

On 11/6/07, Raymond O'Connor <nappin713@yahoo.com> wrote:
> When I store the result of the sort, everything is fine:
>
> Correct
> result = [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
> b <=> a
> end
>
> p result #[9, 8, 8, 7, 4, 3, 2, 1]
>
>
>
>
> But when just do a p on the sort, the problem occurs
>
> Incorrect
> p [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
> b <=> a
> end #[1, 2, 3, 4, 7, 8, 8, 9]


May have something with how the parser sees do/end, (), and {}
precedence. Interesting.

p [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
b <=> a
end #[1, 2, 3, 4, 7, 8, 8, 9]

p [2, 4, 3, 7, 1, 8, 9, 8].sort { |a, b|
b <=> a
} #[9, 8, 8, 7, 4, 3, 2, 1]

p( [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
b <=> a
end
) #[9, 8, 8, 7, 4, 3, 2, 1]

You can see the same behavior with...

p [1, 2, 3].map do |i|
i + 1
end #[1, 2, 3]

p [ 1, 2, 3].map { |i|
i + 1
} #[2, 3, 4]

Todd

  Réponse avec citation
Vieux 07/11/2007, 04h41   #8
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

On Nov 6, 8:22 pm, Todd Benson <caduce...@gmail.com> wrote:
> On 11/6/07, Raymond O'Connor <nappin...@yahoo.com> wrote:
> > When I store the result of the sort, everything is fine:

>
> > Correct
> > result = [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
> > b <=> a
> > end

>
> > p result #[9, 8, 8, 7, 4, 3, 2, 1]

>
> > But when just do a p on the sort, the problem occurs

>
> > Incorrect
> > p [2, 4, 3, 7, 1, 8, 9, 8].sort do |a, b|
> > b <=> a
> > end #[1, 2, 3, 4, 7, 8, 8, 9]

>
> May have something with how the parser sees do/end, (), and {}
> precedence. Interesting.


Exactly.

This line:
p foo { ... }
is interpreted as
p( foo{ ... } )

While this line:
p foo do ... end
is interpreted as
p(foo) do ... end

The lower precedence of do/end is causing it to be bound to the result
of p( foo ), or in your case p( array.sort )



  Réponse avec citation
Vieux 07/11/2007, 09h26   #9
Peña, Botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

From: Phrogz [mailto:phrogz@mac.com]=20
# This line:
# p foo { ... }
# is interpreted as
# p( foo{ ... } )
#=20
# While this line:
# p foo do ... end
# is interpreted as
# p(foo) do ... end
#=20
# The lower precedence of do/end is causing it to be bound to the result
# of p( foo ), or in your case p( array.sort )

indeed, but the sad part really is ruby does not check blocks, like as =
if they float silently like shadows =20

eg

> p{|x| p x}

=3D> nil

> puts{|x| p x}


=3D> nil

> "sdf".upcase{|x| p x}

=3D> "SDF"

> def m
> end

=3D> nil

> m{|x| p x}

=3D> nil

i would consider those as bugs though

i just wish blocks were like clear and visible proc params.

kind regards -botp



  Réponse avec citation
Vieux 07/11/2007, 09h43   #10
Raymond O'Connor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort and a multiline block

Gavin Kistner wrote:
> This line:
> p foo { ... }
> is interpreted as
> p( foo{ ... } )
>
> While this line:
> p foo do ... end
> is interpreted as
> p(foo) do ... end
>
> The lower precedence of do/end is causing it to be bound to the result
> of p( foo ), or in your case p( array.sort )


Really interesting. I never realized that do/end had lower precedence
than {}. Thanks for the everyone!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13989 seconds with 18 queries