Last active
May 9, 2024 11:11
-
-
Save pusewicz/81aec630deabb0b971a8474e69391bee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simply renaming the
@cellidandattr_reader :cellidto eg.idmakes the whole thing work.