Skip to content

Instantly share code, notes, and snippets.

@redconfetti
Last active March 18, 2016 23:47
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 redconfetti/eb7020802cf261d16a7c to your computer and use it in GitHub Desktop.
Save redconfetti/eb7020802cf261d16a7c to your computer and use it in GitHub Desktop.
Constants in super classes not overridden by child classes
# It appears that a base class method refers to the constant value in that method
# instead of the constant value declared in child classes.
# You have to use a method to provide the expected behavior.
class BaseClass
COLUMNS = []
def self.shared_processor
child_processor(COLUMNS + ['column1'])
end
def self.alternative_processor
child_processor(columns_method + ['baseColumn'])
end
end
module Area1
class Connector < BaseClass
COLUMNS = %w(column2 column3)
def self.child_processor(value)
return value + ['area1_column']
end
def self.columns_method
['area1Column']
end
end
end
module Area2
class Connector < BaseClass
COLUMNS = %w(column5 column6)
def self.child_processor(value)
return value + ['area2_column']
end
def self.columns_method
['area2Column']
end
end
end
# Direct reference to constant works
BaseClass::COLUMNS # => []
Area1::Connector::COLUMNS # => ["column2", "column3"]
Area2::Connector::COLUMNS # => ["column5", "column6"]
# COLUMNS used is the one defined in base class, because it uses
# the value of COLUMNS present when the class method was defined
Area1::Connector.shared_processor # => ["column1", "area1_column"]
Area2::Connector.shared_processor # => ["column1", "area2_column"]
Area1::Connector.alternative_processor # => ["area1Column", "baseColumn", "area1_column"]
Area2::Connector.alternative_processor # => ["area2Column", "baseColumn", "area2_column"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment