class ClosedStruct

Constants

VERSION

Public Class Methods

new(contents, &block) click to toggle source
# File lib/closed_struct.rb, line 4
def initialize(contents, &block)
  @contents = contents.dup

  singleton_class = (class << self; self; end)
  @contents.each do |key, value|
    raise ArgumentError.new("Cannot define #{key} as it already exists") if respond_to?(key)
    singleton_class.send(:define_method, key) { value }
  end

  singleton_class.class_eval(&block) if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/closed_struct.rb, line 24
def ==(other)
  (other.class == self.class) && (other.to_h == self.to_h)
end
Also aliased as: eql?
each_pair() { |key, value| ... } click to toggle source
# File lib/closed_struct.rb, line 33
def each_pair
  return enum_for(:each_pair) unless block_given?

  @contents.each_pair do |key, value|
    yield key, value
  end
end
empty?() click to toggle source
# File lib/closed_struct.rb, line 29
def empty?
  @contents.empty?
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/closed_struct.rb, line 20
def hash
  @contents.hash
end
to_h() click to toggle source
# File lib/closed_struct.rb, line 16
def to_h
  @contents
end