Skip to content

Instantly share code, notes, and snippets.

@jtprince
Created August 1, 2014 04:52
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 jtprince/98426c862b4df48f52e5 to your computer and use it in GitHub Desktop.
Save jtprince/98426c862b4df48f52e5 to your computer and use it in GitHub Desktop.
ruby, do I need to include the inheritance each time I open a class? Answer: NO (but the inheritance must be there the 1st time or you'll get a superclass mismatch TypeError)
# [be careful, though, if you switch the order of introduction, then you will get this error:
# <file>.rb:11:in `<main>': superclass mismatch for class Dog (TypeError)
class Dog < Hash
def silly(key)
self[key] = 88 + key.to_i
end
end
# This is the *key* point: we can re-open this class and it won't give us any trouble!
class Dog
def workable(h)
puts "HI: #{h}"
p self.keys
puts "WORD!"
end
end
dog = Dog[key: 'val']
p dog
dog.silly(7)
dog.workable(4)
p dog
dog.workable(9)
# output (ruby 2.1.2)
{:key=>"val"}
HI: 4
[:key, 7]
WORD!
{:key=>"val", 7=>95}
HI: 9
[:key, 7]
WORD!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment