hamsters

vs

squirrels


class Squirrel  attr_reader :nuts

  def initialize
    @nuts = []
  end

  def add(nut)
    nuts << nut
  end
end
sq = Squirrel.new
sq.add :one
sq.add :two
puts sq.inspect=> #<Squirrel:0x00000001cefe40 @nuts=[:one, :two]>
sq.nuts.delete_at 0puts sq.inspect=> #<Squirrel:0x00000001cefe40 @nuts=[:two]>


class Squirrel

  def initialize
    @nuts = []
  end

  def add(nut)
    @nuts << nut
  end

  def nuts
    @nuts.dup
  end
end 


what just happened?

Broken encapsulation

hamster


module Forest
class Hamster
  attr_reader :seeds

  def initialize
    @seeds = ::Hamster.vector
  end

  def add(seed)
    @seeds = @seeds << seed
  end
end
end 
h = Forest::Hamster.new
h.add :one
h.add :two
puts h.inspect
h.seeds.delete_at 0=> #<Forest::Hamster:0x000000014dc230 @seeds=[:one, :two]>=> NoMethodError: undefined method `delete_at' for [:one, :two]:Hamster::Vector


Efficient, Immutable, Thread-Safe Collection classes for Ruby

  • Hash
  • List
  • Stack
  • Queue
  • Vector
  • Set
  • copy on each write?



    github@harukizaemon/hamster

    Made with Slides.com