The value of a constant: inherited vs. lexical scope
An example from Ruby Koans:
class Animal
LEGS = 4
class MyAnimals
LEGS = 2
class Bird < Animal
def legs_in_bird
LEGS
end
end
end
end
How many legs does a Bird have? Two.
Consider this:
class MyAnimals::Oyster < Animal
def legs_in_oyster
LEGS
end
end
How many legs does an Oyster have (at least, in this weird scenario)? Four.
Written by Melanie Archer
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#