strategy by delagating to an alias

I saw this really nice way of setting behaviour. You call a Student object which has a method formal_greeting that delegates to say_hi, but say_hi is aliasing say_hello which calls a superclass since the say_hello of Student is declared below.

class Person
  def say_hello
    puts 'how do you do.'
  end
end

class Student  Person
end

class Student    # reopening class
  # Assuming Person has a say_hello method...
  alias :say_hi :say_hello #this calls super
  def say_hello
    puts "Hi, there."
  end
  def formal_greeting
    # Say hello the way my superclass would.
    say_hi
  end
end
Student.new.formal_greeting

So positioning alias above say_hello definition ensures your call goes up to super class. Am I reading this correctly? if so then this is a nice strategy for designing watir tests depending on configurations needed for your test objects.

close Reblog this comment
blog comments powered by Disqus