CodeSampleX

Beispiel

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.

Verifiziertes Beispiel für gem set 1.1.3: Demonstrate that Set#<=> implements subset partial ordering rather than total ordering, returning nil for…

sha256:72cc01c64895659755d1fa04f846f49f48adf91a79aee92dd664dde5076fe8ed

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
2
Deklarierte Umgebung ruby linux x64 ruby ruby bundler

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
Demonstrate that Set#<=> implements subset partial ordering rather than total ordering, returning nil for incomparable sets and causing Array#sort to raise ArgumentError.
Pakete
Symbole
  • set
  • Set
  • Set#<=>
Umgebung
ruby
Erstellt
2026-08-17T07:23:19Z

Contract

  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.

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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"

Ursprungs-Seeder

csx-seed