LoginSignup
0
0

More than 5 years have passed since last update.

Ruby の Set#add? を思い出そうとあれこれ見てたら Set の中身が Hash だという事も思い出した

Posted at

すっきり。

github.com/ruby/ruby/blob/trunk/lib/set.rb#L320-L322
  def add?(o)
    add(o) unless include?(o)
  end
github.com/ruby/ruby/blob/trunk/lib/set.rb#L214-L216
  def include?(o)
    @hash[o]
  end
github.com/ruby/ruby/blob/trunk/lib/set.rb#L312-L315
  def add(o)
    @hash[o] = true
    self
  end
github.com/ruby/ruby/blob/trunk/lib/set.rb#L82-L92
  def initialize(enum = nil, &block) # :yields: o
    @hash ||= Hash.new(false)

    enum.nil? and return

    if block
      do_with_enum(enum) { |o| add(block[o]) }
    else
      merge(enum)
    end
  end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0