Why do the following two code snippets not produce the same output? The difference between push
and |=
is a tricky one. I suppose that |=
being an assignment might make a difference? On top of that would constants actually be safe from change later on, I guess not?
The code comes from answers to this question. You can see it in action here.
class LibraryItem
ATTRIBUTES = ['title', 'authors', 'location']
end
class LibraryBook < LibraryItem
ATTRIBUTES.push('ISBN', 'pages']
end
puts LibraryItem::ATTRIBUTES
puts LibraryBook::ATTRIBUTES
> ["title", "authors", "location", "ISBN", "pages"]
> ["title", "authors", "location", "ISBN", "pages"]
and
class Foo
ATTRIBUTES = ['title','authors','location']
end
class Bar < Foo
ATTRIBUTES |= ['ISBN', 'pages']
end
puts Foo::ATTRIBUTES
puts Bar::ATTRIBUTES
> ["title", "authors", "location"]
> ["title", "authors", "location", "ISBN", "pages"]