In Ruby, I would like to get something like this Java example:
class A {
private void f() { System.out.println("Hello world"); }
public void g() { f(); }
}
class B extends A {
public void f() { throw new RuntimeException("bad guy");}
}
public class Try {
public static void main(String[] args) { new B().g();}
}
This will print "Hello world" in Java, but direct Ruby transcription:
class A
def g; f; end
private
def f; puts "Hello world"; end
end
class B < A
def f; raise "bad guy"; end
end
B.new.g
of course, will raise a bad guy - due to differences in the method search method (I understand that the value of 'private' is very different between these languages)
Is there any way to achieve this effect? I don’t really like visibility, in fact, I would prefer all public methods here. My goal is simply to isolate the method in the superclass from overriding in subclasses (which will break other basic methods).
I think if there is a solution that will work with / modules too?
module BaseAPI
def f; puts "Hello world"; end
def g; f; end;
end
module ExtAPI
include BaseAPI
def f; raise "bad guy"; end
end
include ExtAPI
g
Follow-up observation: this looks like a rare case when something is possible with Java, but not with Ruby: - /