CodeSampleX

Exemplo

set 1.1.1: Handle Set#to_set returning self on Set receivers and downcasting Set subclasses to base Set when klass is left unset

Amostra verificada para gem set 1.1.1: Handle Set#to_set returning self on Set receivers and downcasting Set subclasses to base Set when klass is left unset.…

sha256:911142f7a4bef266833a736d6209fa98347340da9b948a2a186966668a37fa8d

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
1
Ambiente declarado ruby linux x64 ruby ruby rubygems

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
ruby 3 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-16
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 SKIPPED compile:SKIPPED · contract:SKIPPED · load:SKIPPED · resolve:FAIL
CONTAINER_RUN · rubygems@1
2026-08-18

Caso

HOW
Objetivo
Handle Set#to_set returning self on Set receivers and downcasting Set subclasses to base Set when klass is left unset
Pacotes
Símbolos
  • Set#to_set
  • Enumerable#to_set
Ambiente
ruby
Criado
2026-08-16T16:50:57Z

Contrato

  1. assert Set#to_set with default arguments returns self instead of a new duplicate set, so mutating the result modifies the original set in-place
  2. assert calling to_set on a Set subclass with default klass downcasts the receiver to a base Set instead of retaining the subclass
  3. assert calling to_set with an explicit klass argument constructs an instance of the specified class
  4. assert calling Set#to_set with a block returns a newly allocated Set rather than returning self
  5. assert Enumerable#to_set with default klass converts any Enumerable into a base Set instance

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

Gemfile
source "https://rubygems.org"

gem "set", "1.1.1"
gem "minitest", "5.27.0"
NOTES.md
# Set#to_set Default Argument Semantics

## Prior Search Findings

The search query for `pkg:gem/set` returned existing samples:
- `sha256:7a622d8fbf61d9ccea9796f655d24afc226f861f933bc616c92ec7ee61ee448a` (standard library gem extraction schedules, OpenStruct method collisions, and Set hash mutation)
- `sha256:9e85d5b014a6b4a61ea3ec36e54e9b1b1cc38ae71d9ff0e5bcd6433ea3820406` (Set constructor block transforms, Enumerable#map return types, in-place filter nil returns, and add/delete predicate return shapes)
- `sha256:29ae147b9ec7479fdfe499a4c8f3f472604a244e6c9aca99506591f3499d71df` (Set difference `^` receiver class preservation on subclasses)

This sample addresses the default argument behavior of `Set#to_set` and `Enumerable#to_set` (`to_set(klass = Set, *args, &block)`), where leaving arguments unset returns `self` on base `Set` instances and downcasts `Set` subclasses to base `Set`.

## Failure Modes

- Calling `set.to_set` expecting a defensive copy fails silently with a green build because `Set#to_set` returns `self` (identical object reference), causing downstream modifications on the returned set to mutate the original set in-place.
- Calling `subclass_instance.to_set` expecting receiver class preservation fails loudly with `NoMethodError` at runtime when subclass-specific methods are invoked on the returned base `Set` instance.
csx.json
{"case":{"believed":"Calling to_set on an existing Set produces a new Set instance for safe mutation, and calling to_set on a Set subclass preserves the receiver subclass.","caseId":"case:sha256:ff43ae9dbf6310c1e529237fc65bd748c2bcf3684e4495499fa2545f9c69a1d5","contract":["assert Set#to_set with default arguments returns self instead of a new duplicate set, so mutating the result modifies the original set in-place","assert calling to_set on a Set subclass with default klass downcasts the receiver to a base Set instead of retaining the subclass","assert calling to_set with an explicit klass argument constructs an instance of the specified class","assert calling Set#to_set with a block returns a newly allocated Set rather than returning self","assert Enumerable#to_set with default klass converts any Enumerable into a base Set instance"],"goal":"Handle Set#to_set returning self on Set receivers and downcasting Set subclasses to base Set when klass is left unset","kind":"HOW","packages":["pkg:gem/set@1.1.1"],"schemaVersion":1,"symbols":["Set#to_set","Enumerable#to_set"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"rubygems","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/set@1.1.1"],"schemaVersion":1,"symbols":["Set#to_set","Enumerable#to_set"],"verifierAdapter":"rubygems@1"}
test/contract.rb
require "minitest/autorun"
require "set"

class TaggedSet < Set
  def tag
    "metadata"
  end
end

class SetToSetContractTest < Minitest::Test
  def test_set_to_set_with_default_args_returns_self_causing_shared_mutation
    # Naive assumption: calling .to_set on an existing Set creates a new copy (defensive duplication)
    # Reality: Set#to_set returns self (identical object reference) when default arguments are left unset
    original = Set[:read, :write]
    duplicate_attempt = original.to_set

    assert_same original, duplicate_attempt

    # Mutating duplicate_attempt mutates original in-place
    duplicate_attempt.add(:admin)
    assert_includes original, :admin
  end

  def test_subclass_to_set_with_default_klass_downcasts_to_base_set
    # Naive assumption: calling .to_set on a Set subclass retains the subclass type
    # Reality: klass defaults to Set (not self.class), downcasting the instance to base Set
    custom = TaggedSet[:alpha, :beta]
    converted = custom.to_set

    assert_instance_of Set, converted
    refute_instance_of TaggedSet, converted
    refute_respond_to converted, :tag
    refute_same custom, converted
  end

  def test_subclass_to_set_with_explicit_klass_preserves_target_class
    # Specifying klass explicitly allows converting or retaining the desired Set subclass
    custom = TaggedSet[:alpha, :beta]
    converted = custom.to_set(TaggedSet)

    assert_instance_of TaggedSet, converted
    assert_respond_to converted, :tag
    assert_equal "metadata", converted.tag
    refute_same custom, converted
  end

  def test_set_to_set_with_block_allocates_new_set_instance
    # When a block is supplied, to_set transforms elements and returns a new Set instance rather than self
    original = Set["hello", "world"]
    transformed = original.to_set { |str| str.upcase }

    refute_same original, transformed
    assert_equal Set["HELLO", "WORLD"], transformed
    assert_equal Set["hello", "world"], original
  end

  def test_enumerable_to_set_defaults_klass_to_base_set
    # Calling to_set on Enumerable (e.g. Array) defaults klass to Set
    array = [:read, :write]
    converted = array.to_set

    assert_instance_of Set, converted
    assert_equal Set[:read, :write], converted

    # Explicit klass argument converts Enumerable into the specified subclass
    custom_converted = array.to_set(TaggedSet)
    assert_instance_of TaggedSet, custom_converted
    assert_equal "metadata", custom_converted.tag
  end
end

Seeder de origem

csx-seed