Skip to content

Instantly share code, notes, and snippets.

@deepak
Last active January 4, 2016 07:31
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 deepak/5fe96a9a70d2693b8df3 to your computer and use it in GitHub Desktop.
Save deepak/5fe96a9a70d2693b8df3 to your computer and use it in GitHub Desktop.
calling different methods in sub-class (ruby)
public class Foo {
public String ping() {
return "ping";
}
public String pong() {
return "pong";
}
}
public class BadFoo extends Foo {
public String ping() {
return super.pong();
}
public String pong() {
return super.ping();
}
}
class Foo
def ping
"ping"
end
def pong
"pong"
end
end
# does not work in ruby
# but `super.method_name` works in java
# how to do this in ruby ?
class BadFoo < Foo
def ping
super.pong
end
def pong
super.ping
end
end
# thanks to Yuva
class ConfusedFoo < Foo
alias_method :parent_pong, :pong
alias_method :parent_ping, :ping
def ping
parent_pong
end
def pong
parent_ping
end
end
# Java's way of doing things is verbose
# but it does make this usecase easier
# but not the most common one. ie. calling the super method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment