Understanding private methods in Ruby

Here’s the short and the long of it. What private means in Ruby is a method cannot be called with an explicit receivers, e.g. some_instance.private_method(value). So even though the implicit receiver is self, in your example you explicitly use self so the private methods are not accessible. Think of it this way, would you expect … Read more

How to set private instance variable used within a method test?

As you answered in your question the easiest way to set instance variable is with instance_eval method: obj.instance_eval(‘@a = 1’) Another way is to use instance_variable_set: obj.instance_variable_set(:@a, 1) But I would not recommend to do this in your specs. Specs are all about testing behavior of an object and testing behaviour by breaking class encapsulation … Read more

Access to protected member through member-pointer: is it a hack?

The fact that a member is not accessible using class member access expr.ref (aclass.amember) due to access control [class.access] does not make this member inaccessible using other expressions. The expression &Derived::value (whose type is int Base::*) is perfectly standard compliant, and it designates the member value of Base. Then the expression a_base.*p where p is … Read more

What is the default access specifier in Java?

The default visibility is known as “package-private” (though you can’t use this explicitly), which means the field will be accessible from inside the same package to which the class belongs. As mdma pointed out, it isn’t true for interface members though, for which the default is “public”. See Java’s Access Specifiers

Why does Ruby have both private and protected methods?

protected methods can be called by any instance of the defining class or its subclasses. private methods can be called only from within the calling object. You cannot access another instance’s private methods directly. Here is a quick practical example: def compare_to(x) self.some_method <=> x.some_method end some_method cannot be private here. It must be protected … Read more

Private virtual method in C++

Herb Sutter has very nicely explained it here. Guideline #2: Prefer to make virtual functions private. This lets the derived classes override the function to customize the behavior as needed, without further exposing the virtual functions directly by making them callable by derived classes (as would be possible if the functions were just protected). The … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)