DISQUS

Dan Manges's Blog: Dan Manges's Blog - The Power of Implementing Ruby in Ruby

  • Charles Oliver Nutter · 1 year ago
    This isn't a factor of being implemented in Ruby; rather, it's a factor of having access to all internals as normal objects. And although MRI doesn't provide that capability, JRuby does:

    <pre>
    require 'java'
    require 'jruby'

    def examine(obj)
    real = JRuby.reference(obj)
    realclass = JRuby.reference(real.meta_class)
    while realclass
    p realclass
    break unless realclass.super_class
    realclass = JRuby.reference(realclass.super_class)
    end
    end

    object = Class.new { def foo; 'foo from object'; end }.new
    mixin = Module.new { def foo; 'foo from mixin'; end }

    object.extend(mixin)
    examine(object)
    </pre>

    In JRuby, as in Rubinius, all objects can be accessed as "just objects". So the same capabilities apply.