@@ -2606,21 +2606,53 @@ rb_mod_public(int argc, VALUE *argv, VALUE module)
26062606 * protected(method_name, method_name, ...) -> array
26072607 * protected(array) -> array
26082608 *
2609- * With no arguments, sets the default visibility for subsequently
2610- * defined methods to protected. With arguments, sets the named methods
2611- * to have protected visibility.
2612- * String arguments are converted to symbols.
2613- * An Array of Symbols and/or Strings is also accepted.
2614- * If a single argument is passed, it is returned.
2615- * If no argument is passed, nil is returned.
2616- * If multiple arguments are passed, the arguments are returned as an array.
2609+ * Sets the visibility of a section or of a list of method names as protected.
2610+ * Accepts no arguments, a splat of method names (symbols or strings) or an
2611+ * array of method names. Returns the arguments that it received.
2612+ *
2613+ * == Important difference between protected in other languages
2614+ *
2615+ * Protected methods in Ruby are different from other languages such as Java,
2616+ * where methods are marked as protected to give access to subclasses. In Ruby,
2617+ * subclasses <b>already have access to all methods defined in the parent
2618+ * class</b>, even private ones.
2619+ *
2620+ * Marking a method as protected allows <b>different objects of the same
2621+ * class</b> to call it.
2622+ *
2623+ * One use case is for comparison methods, such as <code>==</code>, if we want
2624+ * to expose a method for comparison between objects of the same class without
2625+ * making the method public to objects of other classes.
26172626 *
2618- * If a method has protected visibility, it is callable only where
2619- * <code>self</code> of the context is the same as the method.
2620- * (method definition or instance_eval). This behavior is different from
2621- * Java's protected method. Usually <code>private</code> should be used.
2627+ * == Performance considerations
26222628 *
2623- * Note that a protected method is slow because it can't use inline cache.
2629+ * Protected methods are slower than others because they can't use inline
2630+ * cache.
2631+ *
2632+ * == Example
2633+ *
2634+ * class Account
2635+ * # Mark balance as protected, so that we can compare between accounts
2636+ * # without making it public.
2637+ * attr_reader :balance
2638+ * protected :balance
2639+ *
2640+ * def initialize(balance)
2641+ * @balance = balance
2642+ * end
2643+ *
2644+ * def >(other)
2645+ * # The invocation to `other.balance` is allowed because `other` is a
2646+ * # different object of the same class (Account).
2647+ * balance > other.balance
2648+ * end
2649+ * end
2650+ *
2651+ * account1 = Account.new(100)
2652+ * account2 = Account.new(50)
2653+ *
2654+ * account1 > account2 # => true (works)
2655+ * account1.balance # => NoMethodError (fails because balance is not public)
26242656 *
26252657 * To show a private method on RDoc, use <code>:doc:</code> instead of this.
26262658 */
@@ -3196,4 +3228,3 @@ Init_eval_method(void)
31963228 REPLICATE_METHOD (rb_eException , idRespond_to_missing );
31973229 }
31983230}
3199-
0 commit comments