CodeSampleX

示例

set 1.1.3: Demonstrate that Set#<=> implements subset partial ordering rather than total ordering, returning nil for incomparable sets and causing Array#sort to raise ArgumentError.

已验证示例 — gem set 1.1.3: Demonstrate that Set#<=> implements subset partial ordering rather than total ordering, returning nil for incomparable sets and…

sha256:72cc01c64895659755d1fa04f846f49f48adf91a79aee92dd664dde5076fe8ed

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 ruby linux x64 ruby ruby bundler

验证运行环境

环境 契约 阶段 运行日期
ruby 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-17
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-18

案例

HOW
目标
Demonstrate that Set#<=> implements subset partial ordering rather than total ordering, returning nil for incomparable sets and causing Array#sort to raise ArgumentError.
包
符号
  • set
  • Set
  • Set#<=>
环境
ruby
创建时间
2026-08-17T07:23:19Z

契约

  1. Set#<=> returns nil when neither set is a subset of the other, which causes Array#sort on incomparable sets to raise ArgumentError.
  2. Set#<=> returns -1, 0, or 1 only when sets are in a proper subset, equal, or proper superset relationship.
  3. Array#sort on an array of sets succeeds only when all elements can be ordered by subset inclusion.

文件

  • Gemfile
  • Gemfile.lock
  • NOTES.md
  • csx.json
  • test/contract.rb

下载源代码构件 (tar.gz)

源代码

Gemfile
source "https://rubygems.org"

gem "set", "1.1.3"
Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    set (1.1.3)

PLATFORMS
  ruby
  x64-mingw-ucrt

DEPENDENCIES
  set (= 1.1.3)

CHECKSUMS
  bundler (4.0.18) sha256=02d9a17429de1847b4e0c9f27a9ee4b20c0a74c0a641b4e77195d6019e3618ac
  set (1.1.3) sha256=12bbbd31c8b177d55ac0314bcc7f4c473276b06e8a2dcfdc14326e73ce7769ce

BUNDLED WITH
  4.0.18
NOTES.md
# Set Behavior Notes

## Known Solutions Search

A search via `search_known_solution` for `set` returned existing published samples:
- `sha256:f6a3b9fffc2feae50a88bbf1e67fb2df72ee881835f753c7b1e0dd1960834188`: preloading removed `sorted_set` via `RUBYOPT=-rsorted_set` fails startup.
- `sha256:c3ebad077aa01c12e583e57f127c53912b03d9730d22c45a627de462968ed9b7`: element hash mutation causing stale Set membership recoverable with `Set#reset`.
- `sha256:be568ae4dfa6ca70997019d57f8927ef41a1281a77737965d777cc3fee8ad853`: `Set#replace` resetting `compare_by_identity` state.

To avoid duplicating existing coverage, this sample targets the comparison semantics of `Set#<=>` and its interaction with `Array#sort`.

## Pinned Release

- Gem: `set`
- Version: `1.1.3`

## Observed Behavior

`Set#<=>` implements a partial ordering based strictly on subset/superset relations:
- It returns `-1` if the receiver is a proper subset of the argument (`Set[1, 2] <=> Set[1, 2, 3]`).
- It returns `1` if the receiver is a proper superset of the argument (`Set[1, 2, 3] <=> Set[1, 2]`).
- It returns `0` if both sets contain the same elements (`Set[1, 2] <=> Set[2, 1]`).
- It returns `nil` when comparing against a non-Set object or when neither set is a subset of the other (`Set[1, 2] <=> Set[1, 4]` or `Set[1, 2] <=> Set[3]`), even when their sizes differ.

## Failure Mode

When code naively assumes `Set#<=>` provides a total ordering across arbitrary sets (e.g., sorting an array of sets with `[Set[1, 2], Set[1, 4]].sort`), execution fails loudly with:
`ArgumentError: comparison of Set with Set failed`

Only collections of sets that form a strict subset hierarchy can be sorted with `Array#sort`.
csx.json
{"case":{"believed":"Set#\u003c=\u003e provides a total ordering across any sets so arrays of sets can be sorted with Array#sort.","caseId":"case:sha256:7ae5594678c9ea03fac069feca35dc721d33f5fbb5bbeaeb72d08778c84135eb","contract":["Set#\u003c=\u003e returns nil when neither set is a subset of the other, which causes Array#sort on incomparable sets to raise ArgumentError.","Set#\u003c=\u003e returns -1, 0, or 1 only when sets are in a proper subset, equal, or proper superset relationship.","Array#sort on an array of sets succeeds only when all elements can be ordered by subset inclusion."],"goal":"Demonstrate that Set#\u003c=\u003e implements subset partial ordering rather than total ordering, returning nil for incomparable sets and causing Array#sort to raise ArgumentError.","kind":"HOW","packages":["pkg:gem/set@1.1.3"],"schemaVersion":1,"symbols":["set","Set","Set#\u003c=\u003e"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"bundler","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/set@1.1.3"],"schemaVersion":1,"symbols":["set","Set","Set#\u003c=\u003e"],"verifierAdapter":"rubygems@1"}
test/contract.rb
# frozen_string_literal: true

require "set"

def assert(condition, message)
  raise "ASSERTION FAILED: #{message}" unless condition
end

# Verify resolved version
version = Gem.loaded_specs["set"]&.version&.to_s || Set::VERSION
assert(version == "1.1.3", "expected set gem version 1.1.3, got #{version}")

# Set#<=> defines a partial order based on subset/superset relations:
# - proper subset: -1
# - proper superset: 1
# - equal: 0
# - incomparable: nil
s1 = Set[1, 2]
s2 = Set[1, 2, 3]
s3 = Set[2, 1]
s4 = Set[1, 4]
s5 = Set[3]

# 1. Proper subset and superset comparisons return -1 and 1
assert((s1 <=> s2) == -1, "subset <=> superset should return -1")
assert((s2 <=> s1) == 1, "superset <=> subset should return 1")

# 2. Equal sets return 0 regardless of insertion order
assert((s1 <=> s3) == 0, "equal sets should return 0")

# 3. Incomparable sets (neither is a subset of the other) return nil
assert((s1 <=> s4).nil?, "partially overlapping sets should return nil")
assert((s1 <=> s5).nil?, "disjoint sets of different sizes should return nil")

# 4. Comparison against non-Set objects returns nil
assert((s1 <=> [1, 2]).nil?, "comparing Set with Array should return nil")

# 5. Sorting sets that form a subset chain succeeds
chain = [Set[1, 2, 3], Set[1], Set[1, 2]]
sorted_chain = chain.sort
assert(sorted_chain == [Set[1], Set[1, 2], Set[1, 2, 3]], "subset chain should sort by inclusion")

# 6. Sorting incomparable sets fails loudly with ArgumentError
sort_error = nil
begin
  [s1, s4].sort
rescue ArgumentError => e
  sort_error = e
end

assert(!sort_error.nil?, "sorting incomparable sets must raise ArgumentError")
assert(sort_error.message.include?("comparison of Set with Set failed"),
       "expected 'comparison of Set with Set failed' error message")

puts "contract passed"

原始种子者

csx-seed