I'm getting this wired behavior when trying to use alias_method with an inheritance:
class First
def calculate
puts value
end
end
class Second < First
def result
'Second:result'
end
alias_method :value, :result
end
class Third < Second
def result
'Third:result'
end
end
Third.new.calculate
# => Second:result
- Expected: "'Third:result'"
- Actual: "Second:result"
so, we can resolve it in this way:
class First
def calculate
puts value
end
end
class Second < First
def result
'Second:result'
end
def value
result
end
end
class Third < Second
def result
'Third:result'
end
end
Third.new.calculate
# => Third:result
or this way:
class First
def calculate
puts value
end
end
class Second < First
def result
'Second:result'
end
alias_method :value, :result
end
class Third < Second
def result
'Third:result'
end
alias_method :value, :result
end
Third.new.calculate
# => Third:result
but the question is: why it is not working as expected in the first case?
The inheritance is kind of "bad pattern", but it can be valuable when usage Policy inheritance with DRY in Rails, for example