Skip to content

Instantly share code, notes, and snippets.

@pusewicz
Last active May 9, 2024 11:11
Show Gist options
  • Save pusewicz/81aec630deabb0b971a8474e69391bee to your computer and use it in GitHub Desktop.
Save pusewicz/81aec630deabb0b971a8474e69391bee to your computer and use it in GitHub Desktop.
class Cell
@@cellid = -1
# Renaming the attribute cellid to eg. id makes it work?!
attr_reader :cellid
def initialize
@cellid = (@@cellid += 1)
end
def ==(other)
@cellid == other.cellid
end
end
def tick(args)
reset if args.tick_count.zero?
@cells.delete(@cells.sample)
args.outputs.labels << [640, 720, "Cells: #{@cells.size}", 5, 1]
end
def reset
@cells = Array.new(1_000) { Cell.new }
end
# Simplified version for MRuby, that does not work whatever the attribute name is
class Cell
attr_accessor :id
def initialize
@id = rand(1_000_000)
end
def ==(other)
@id == other.id
end
def id
@id
end
end
cells = Array.new(1_000) { Cell.new }
1_000.times do |i|
cells.delete(cells.sample)
end
@pusewicz
Copy link
Author

pusewicz commented May 9, 2024

Simply renaming the @cellid and attr_reader :cellid to eg. id makes the whole thing work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment