Skip to content

Instantly share code, notes, and snippets.

@avdi
Created June 12, 2012 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avdi/2919483 to your computer and use it in GitHub Desktop.
Save avdi/2919483 to your computer and use it in GitHub Desktop.
Class reloading breaks type-checking
class Foo
end
Foo.object_id # => 34581480
f = Foo.new # => #<Foo:0x000000041f53c0>
f.class.object_id # => 34581480
# This just prevents the "const redefined" warning
Object.send(:remove_const, :Foo)
# "Reload" class Foo
class Foo
end
Foo.object_id # => 34580180
f.class.object_id # => 34581480
f.is_a?(Foo) # => false
f.class == Foo # => false
f.class.name == "Foo" # => true
@geeksam
Copy link

geeksam commented Jun 14, 2012

@shevegen, Avdi is saying that actual newbs are actually surprised when they first encounter this behavior. Telling someone that they shouldn't be surprised by something that has surprised them is, in effect, a "well, actually".

The fundamental source of the surprise, as you point out, is remove_const. (Or, as I like to think of it, Ruby providing a concrete example of [the first half of] the adage "constants aren't, variables don't.") In many other languages, there's no equivalent -- once a reference is in the symbol table, it's there to stay.

@avdi
Copy link
Author

avdi commented Jun 14, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment