Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page

Looking Inside Classes

We can find out a bit more about the methods and constants in a particular object. Instead of just checking to see whether the object responds to a given message, we can ask for methods by access level, we can ask for just singleton methods, and we can have a look at the object's constants.

class Demo
    def privMethod
    def protMethod
    def pubMethod
  def Demo.classMethod
  CONST = 1.23
Demo.private_instance_methods ["privMethod"]
Demo.protected_instance_methods ["protMethod"]
Demo.public_instance_methods ["pubMethod"]
Demo.singleton_methods ["classMethod"]
Demo.constants - Demo.superclass.constants ["CONST"]

Module.constants returns all the constants available via a module, including constants from the module's superclasses. We're not interested in those just at the moment, so we'll subtract them from our list.

Given a list of method names, we might now be tempted to try calling them. Fortunately, that's easy with Ruby.
Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire