Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Created November 9, 2018 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edisonywh/e9d1f7e00d441a7c451d3c4d72a9bc3f to your computer and use it in GitHub Desktop.
Save edisonywh/e9d1f7e00d441a7c451d3c4d72a9bc3f to your computer and use it in GitHub Desktop.
ObjectSpace.count_objects[:T_CLASS] #=> 920
Foo = Class.new
ObjectSpace.count_objects[:T_CLASS] #=> 922
```
Now, before we understand what `extend` is doing, we need to first understand `Singleton Class`, so bear with me, get through the next section and we’ll then be able to understand the `extend` magic!
## The one about Singleton Class
`Singleton Class`, like its alternative name, `Anonymous Class` suggests, is anonymous, thus does not show up in the ancestor chain(!!!) That doesn’t mean we can’t access the singleton class though, in fact, there’s a handy method called `.singleton_class` that we can use.
```ruby
class Foo
end
Foo.singleton_class #=> #<Class:Foo>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment