Skip to content

Instantly share code, notes, and snippets.

@Fire-Dragon-DoL
Last active August 29, 2015 14:08
Show Gist options
  • Save Fire-Dragon-DoL/675a69a698105088c3e3 to your computer and use it in GitHub Desktop.
Save Fire-Dragon-DoL/675a69a698105088c3e3 to your computer and use it in GitHub Desktop.
Ruby (2.1+) protected method weirdness. Fails with "NoMethodError", accessing protected method. See Ruby code in action: https://eval.in/209730 See Java code in action: http://ideone.com/2B3CEq See C# code in action: http://ideone.com/RJjn4f See C++ code in action: http://ideone.com/7Ihvw0 See Ruby alternative code in action: https://eval.in/209934
class A
def testme(other)
puts "value from this: #{ value } value from other: #{ other.value }"
end
protected def value
raise NotImplementedError
end
end
class B < A
protected def value
"B class"
end
end
class C < A
protected def value
"C class"
end
end
b = B.new
c = C.new
b.testme(c)
# /tmp/execpad-e832b1b53f4e/source-e832b1b53f4e:4:in `testme': protected method `value' called for #<C:0x4105c724> (NoMethodError)
# from /tmp/execpad-e832b1b53f4e/source-e832b1b53f4e:32:in `<main>'
class A
def testme(other)
puts "value from this: #{ value } value from other: #{ other.value }"
end
protected def value
_value
end
private def _value
raise NotImplementedError
end
end
class B < A
private def _value
"B class"
end
end
class C < A
private def _value
"C class"
end
end
b = B.new
c = C.new
b.testme(c)
# value from this: B class value from other: C class
#include <iostream>
using namespace std;
class A;
class B;
class C;
class A
{
public:
void Testme(A& other)
{
cout << "value from this: " << Value() << " value from other: " << other.Value();
}
protected:
virtual char const* Value() = 0;
};
class B : public A
{
protected:
char const* Value()
{
return "B class";
}
};
class C : public A
{
protected:
char const* Value()
{
return "C class";
}
};
int main() {
B b = B();
C c = C();
b.Testme(c);
return 0;
}
// value from this: B class value from other: C class
using System.IO;
using System;
abstract class A
{
public void Testme(A other)
{
Console.WriteLine("value from this: " + Value() + " value from other: " + other.Value());
}
protected abstract string Value();
}
class B : A
{
protected override string Value()
{
return "B class";
}
}
class C : A
{
protected override string Value()
{
return "C class";
}
}
class Program
{
static void Main()
{
B b = new B();
C c = new C();
b.Testme(c);
}
}
// value from this: B class value from other: C class
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
abstract class A
{
public void Testme(A other)
{
System.out.println("value from this: " + Value() + " value from other: " + other.Value());
}
abstract protected String Value();
}
class B extends A
{
protected String Value()
{
return "B class";
}
}
class C extends A
{
protected String Value()
{
return "C class";
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Program
{
public static void main (String[] args) throws java.lang.Exception
{
B b = new B();
C c = new C();
b.Testme(c);
}
}
/* value from this: B class value from other: C class */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment