Skip to Main Content
Ruby. Receptury
book

Ruby. Receptury

by Lucas Carlson, Leonard Richardson
November 2012
Intermediate to advanced content levelIntermediate to advanced
888 pages
54h 46m
Polish
Helion
Content preview from Ruby. Receptury

10.3. Lista metod unikalnych dla obiektu

Problem

Listy metod zwracane przez opisane wcześniej metody są dość obszerne, a duża część ich zawartości dotyczy zwykle metod dziedziczonych od przodków. Chcielibyśmy tę część odfiltrować, pozostawiając jedynie metody charakterystyczne dla danej klasy czy obiektu.

Rozwiązanie

Od zbioru metod definiowanych przez dany obiekt czy klasę należy odjąć zbiór metod definiowanych przez jego (jej) superklasę. Widoczna poniżej metoda my_methods_only wykonuje to zadanie dla każdego obiektu:

class Object def my_methods_only my_super = self.class.superclass return my_super ? methods - my_super.instance_methods : methods end end s = '' s.methods.size # => 158 Object.instance_methods.size # => 53 s.my_methods_only.size ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ReasonML Quick Start Guide

ReasonML Quick Start Guide

Raphael Rafatpanah, Bruno Joseph D'mello
Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Egbert Jeschke, Eckehard Pfeifer, Helmut Reinke, Sara Unverhau, Bodo Fienitz
Mastering Ruby Closures

Mastering Ruby Closures

Benjamin Tan Wei Hao

Publisher Resources

ISBN: 9781457177453Errata Page