Git Product home page Git Product logo

Comments (3)

lopopolo avatar lopopolo commented on May 18, 2024

Object#object_id implementation: GH-9

from artichoke.

lopopolo avatar lopopolo commented on May 18, 2024
[2.6.3] > BasicObject.class
=> Class
[2.6.3] > Object.class
=> Class
[2.6.3] > Module.class
=> Class
[2.6.3] > Class.class
=> Class
[2.6.3] > Kernel.class
=> Module
[2.6.3] > Class.ancestors
=> [Class, Module, Object, Kernel, BasicObject]

There are some circular references here. To avoid them:

  • Ancestors is used for vtable calculations. It should be a list of ObjectIds.
  • Classes should store an owned reference to their super-class except for Class. Use an enum to help here.
  • Instances should store an owned pointer to their Class. This ensures that they maintain their class if the constant naming the class is removed. This will allow the class to get collected only if all of its instances become unreachable.
  • Modules and classes should store a reference to their enclosing Module or Class scope.
  • Modules and classes have an internal name property that is distinct from the constants they are assigned to.
[2.6.3] > module A; class B; end; end
=> nil
[2.6.3] > inst = A::B.new
=> #<A::B:0x00007fe939a08460>
[2.6.3] > inst.class
=> A::B
[2.6.3] > Object.send(:remove_const, :A)
=> A
[2.6.3] > inst.class
=> A::B
[2.6.3] > inst.class.name
=> "A::B"

from artichoke.

lopopolo avatar lopopolo commented on May 18, 2024

Hash mutable iteration:

  • Deletions while iterating are allowed.
  • Insertions while iterating are not.
$ irb
[3.0.1] > h = { a: "a", b: "b", c: "c" }
=> {:a=>"a", :b=>"b", :c=>"c"}
[3.0.1] > del = true
=> true
[3.0.1] > h.each_value { |v| puts v ; h.delete(:a) if del ; ins = true if del ; del = false ; h[:a] = "aaa" if ins }
a
(irb):3:in `block in <main>': can't add a new key into hash during iteration (RuntimeError)
        from (irb):3:in `each_value'
        from (irb):3:in `<main>'
        from /usr/local/var/rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
        from /usr/local/var/rbenv/versions/3.0.1/bin/irb:23:in `load'
        from /usr/local/var/rbenv/versions/3.0.1/bin/irb:23:in `<main>'
[3.0.1] > h.each_key { |v| puts v ; h.delete(:a) if del ; ins = true if del ; del = false ; h[:a] = "aaa" if ins }
b
c
=> {:b=>"b", :c=>"c"}
[3.0.1] > h = { a: "a", b: "b", c: "c" }
=> {:a=>"a", :b=>"b", :c=>"c"}
[3.0.1] > del = true
=> true
[3.0.1] > h.each_key { |v| puts v ; h.delete(:a) if del ; ins = true if del ; del = false ; h[:a] = "aaa" if ins }
a
(irb):7:in `block in <main>': can't add a new key into hash during iteration (RuntimeError)
        from (irb):7:in `each_key'
        from (irb):7:in `<main>'
        from /usr/local/var/rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
        from /usr/local/var/rbenv/versions/3.0.1/bin/irb:23:in `load'
        from /usr/local/var/rbenv/versions/3.0.1/bin/irb:23:in `<main>'
[3.0.1] > h = { a: "a", b: "b", c: "c" }
=> {:a=>"a", :b=>"b", :c=>"c"}
[3.0.1] > del = true
=> true
[3.0.1] > h.each_key { |v| puts v ; h.delete(:b) if del ; ins = true if del ; del = false }
a
c
=> {:a=>"a", :c=>"c"}

from artichoke.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.