access-specifier

How to I make private class constants in Ruby

百般思念 提交于 2019-12-03 07:25:51
问题 In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" end end Person.new.show_secret puts Person::SECRET # I'd like this to fail 回答1: You can also change your constant into a class method: def self.secret 'xxx' end private_class_method :secret This makes it accessible within all instances of the class, but not outside. 回答2: Starting on

Why is it legal to inappropriately access privates in an explicit instantiation?

帅比萌擦擦* 提交于 2019-12-03 02:43:22
Why on earth would this be allowed: ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// template<typename T> struct invisible { static typename T::type value; }; template<typename T> typename T::type invisible<T>::value; ////////////////////////////////////////////////////////////////////////// template<typename T, typename T::type P> class construct_invisible { construct_invisible(){ invisible<T>::value = P; } static const construct_invisible instance; }; template<typename T, typename T::type P>

How to I make private class constants in Ruby

本小妞迷上赌 提交于 2019-12-02 20:05:46
In Ruby how does one create a private class constant? (i.e one that is visible inside the class but not outside) class Person SECRET='xxx' # How to make class private?? def show_secret puts "Secret: #{SECRET}" end end Person.new.show_secret puts Person::SECRET # I'd like this to fail You can also change your constant into a class method: def self.secret 'xxx' end private_class_method :secret This makes it accessible within all instances of the class, but not outside. Renato Zannon Starting on ruby 1.9.3, you have the Module#private_constant method, which seems to be exactly what you wanted:

Don't the Ruby methods instance_eval() and send() negate the benefits of private visibility?

眉间皱痕 提交于 2019-12-02 02:37:38
w = Widget.new # Create a Widget w.send :utility_method # Invoke private method! w.instance_eval { utility_method } # Another way to invoke it w.instance_eval { @x } # Read instance variable of w Looking at the example above which relates to the Widget class (below), the send and instance_eval methods violate all of the protections provided by private and protected visibility. If so, why bother with private and protected access in Ruby at all since there is no guarantee that your definitions will be honored? class Widget def x # Accessor method for @x @x end protected :x # Make it protected

Don't the Ruby methods instance_eval() and send() negate the benefits of private visibility?

∥☆過路亽.° 提交于 2019-12-02 02:18:56
问题 w = Widget.new # Create a Widget w.send :utility_method # Invoke private method! w.instance_eval { utility_method } # Another way to invoke it w.instance_eval { @x } # Read instance variable of w Looking at the example above which relates to the Widget class (below), the send and instance_eval methods violate all of the protections provided by private and protected visibility. If so, why bother with private and protected access in Ruby at all since there is no guarantee that your definitions

Is it possible to hide or lower access to Inherited Methods in Java?

那年仲夏 提交于 2019-12-01 15:07:32
I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from derived classes. According to the Java Language specification it is possible to override access specifications on inherited methods to make them more public, but not more private. For example, this is the gist of what I need to do, but is illegal: // Defines myMethod public class Base { protected void myMethod() {} } // Uses myMethod and then hides it. public class DerivedOne extends Base { @Override private void myMethod(); }

Is it possible to hide or lower access to Inherited Methods in Java?

被刻印的时光 ゝ 提交于 2019-12-01 14:46:35
问题 I have a class structure where I would like some methods in a base class to be accessible from classes derived directly from the base class, but not classes derived from derived classes. According to the Java Language specification it is possible to override access specifications on inherited methods to make them more public, but not more private. For example, this is the gist of what I need to do, but is illegal: // Defines myMethod public class Base { protected void myMethod() {} } // Uses

Inconsistent accessibility: base class is less accessible than class

空扰寡人 提交于 2019-12-01 02:26:11
So I have an abstract base class in a DLL and child classes of that class. I want the childs to be public, but the base to be private so that it cannot be accessed outside of the dll. How do I do that? You don't and you can't. If you want to expose the class as public , the base-type must be public . One other option is to have a public interface , and only expose the type via the interface (presumably with a factory method somewhere for creating instances). One final option is to encapsulate the base-class rather than inherit it. Make it public , make all constructors internal (if you're

Inconsistent accessibility: base class is less accessible than class

穿精又带淫゛_ 提交于 2019-11-30 21:57:13
问题 So I have an abstract base class in a DLL and child classes of that class. I want the childs to be public, but the base to be private so that it cannot be accessed outside of the dll. How do I do that? 回答1: You don't and you can't. If you want to expose the class as public , the base-type must be public . One other option is to have a public interface , and only expose the type via the interface (presumably with a factory method somewhere for creating instances). One final option is to

How do I dynamically define a method as private?

独自空忆成欢 提交于 2019-11-30 17:47:47
This does not seem to work: class Test private define_method :private_method do "uh!" end end puts Test.new.private_method Test.instance_eval { private :private_method } Or, just run private :private_method from within the Test class. It seems that starting with Ruby 2.1, define_method respects private : $ rvm 2.1.0 $ ruby /tmp/test.rb /tmp/test.rb:10:in `<main>': private method `private_method' called for #<Test:0x00000102014598> (NoMethodError) $ rvm 2.0 $ ruby /tmp/test.rb uh! (I realize this is an old question, but I happened across it via Google.) Module#private takes an optional argument