ruby.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
If you are interested in the Ruby programming language, come join us! Tell us about yourself when signing up. If you just want to join Mastodon, another server will be a better place for you.

Administered by:

Server stats:

1.1K
active users

postmodern

If you're starting an IRB session inside of an `Class.new { include TheModule }.new` which extends a module, what would you expect `self.inspect` to return?

The `Class.new.tap { |klass| klass.include(context) }.new` is necessary so the resulting context object gets access to both the module's instance methods *and* it's constants. If anyone knows of a better way to wrap a module into an instance context for IRB, feel free to correct me here.

I seem to remember the old school way was to create a new Object.new, then access it's meta-class via `class << obj; self; end`, and then include the module into the meta-class, which was a bit too magical, but prevented `self.inspect` from returning `#<#<Class: ...>>` which looks confusing vs. regular old `#<Object:...>`.

I'm sort of leaning towards just overriding `obj.inspect` to return `#<TheModule>` to indicate that you are in fact in an instance of an Object that included some other module's stuff.

Cool trick I just discovered, transparently forward `const_missing` of an object to another module without adding another line to the backtrace:

obj.singleton_class.define_singleton_method(:const_missing,&mod.method(:const_missing))

@postmodern I think we can officially call that `singleton_class` now. I don't think it's magical and I used it extensively in a lot of places, notably mocking library muack. In a different context, but I also used it in an interactive environment: github.com/godfat/rib/blob/rib

On topic, I voted for `#<TheModule>` as well because I think inspect should always be precise. If one wants to know what exactly it is, one can look at ancestors

Also defining `self.inspect` is defining on the singleton class

GitHubrib/lib/rib/shell.rb at rib-1.6.1 · godfat/ribRuby-Interactive-ruBy -- Yet another interactive Ruby shell - godfat/rib