cmp different from the string representation

Đang mở
#41 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
ruby
Lĩnh vực
operating-systems

Hướng nghiên cứu

Đọc phần triển khai cmp trong ext/pathname/pathname.c và các trường hợp liên quan trong test/pathname/test_pathname.rb, sau đó tái hiện các kết quả khác nhau của Pathname#<=> và thứ tự chuỗi được mô tả trong issue. Được xem là hoàn tất khi hành vi sắp xếp dự kiến đã được xác lập và các bài kiểm thử hoặc tài liệu liên quan phản ánh rõ ràng kết luận đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

There's an ordering difference when using the built-in <=> vs the string representation one:

> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort.first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expectation_chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/message_chains.rb>]
> Pathname.new(File.expand_path('.')).glob('**/*.rb').sort_by(&:to_s).first(5)
=> [#<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb>,
 #<Pathname:/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb>]

And the latter corresponds to the classic alternative:

> Dir[File.expand_path('**/*.rb')].sort.first(5)
=> ["/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/chain.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/error_generator.rb",
 "/Users/pirj/source/rspec-dev/repos/rspec-mocks/lib/rspec/mocks/any_instance/expect_chain_chain.rb"]

Just by looking at the cmp implementation and tests I couldn't understand, is this the designed behaviour, or the behaviour is undefined?

Semantically, it would make sense to load rspec/mocks.rb first, and then rspec/mocks/any_instance.rb, not the other way around.
Some discussion in rspec-rails regarding the issues it may cause.
rspec-rails PR to change the helper template to use Pathname#glob instead of Dir.[].

A workaround rspec-rails plans to take is to sort_by(&:to_s) to get back to the string representation-like ordering.

(semi-)Related cops:

Ngôn ngữ chính
Ruby
Star
32
Fork
24
Merge trung bình
13 giờ 2 phút
Pull request đã merge (30 ngày)
1

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ruby/pathname

Tất cả issue của ruby/pathname

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.