Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Set.new
[unchecked out A] (::_Each[A]) -> ::Set[A]
[unchecked out A, X] (::_Each[X]) { (X) -> A } -> ::Set[A]
[unchecked out A] (?nil) -> ::Set[A]
new(enum = nil) { |o| ... }
lib/set.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/set.rb, line 243
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
Creates a new set containing the elements of the given enumerable object.
If a block is given, the elements of enum are preprocessed by the given block.
Example
Set.new([1, 2]) #=> #<Set: {1, 2}> Set.new([1, 2, 1]) #=> #<Set: {1, 2}> Set.new([1, 'c', :s]) #=> #<Set: {1, "c", :s}> Set.new(1..5) #=> #<Set: {1, 2, 3, 4, 5}> Set.new([1, 2, 3]) { |x| x * x } #=> #<Set: {1, 4, 9}>