Method: Weak::Set#initialize

Defined in:
lib/weak/set.rb

#initialize(enum = nil) {|element| ... } ⇒ Set

Returns a new instance of Set.

Parameters:

Yields:

  • (element)

    calls the given block once for each element in ‘enum` and add the block’s return value instead if the enum’s value. Make sure to only return objects which are references somewhere else to avoid them being quickly garbage collected again.

Yield Parameters:

  • element (Object)

    the yielded value from the ‘enum`


232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/weak/set.rb', line 232

def initialize(enum = nil)
  clear

  return if enum.nil?
  if block_given?
    do_with_enum(enum) do |obj|
      add yield(obj)
    end
  else
    do_with_enum(enum) do |obj|
      add obj
    end
  end
end