Online Mind Mapping and Brainstorming

Create your own awesome maps

Online Mind Mapping and Brainstorming

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account? Log In

Ruby Object Model: a first insight by Mind Map: Ruby Object Model:
a first insight
0.0 stars - reviews range from 0 to 5

Ruby Object Model: a first insight

Some useful methods I discovered

Module#constants

all constants in the current scope, module Piero class Paolo #... end class Pippo #... end end puts Piero.constants # => [Pippo, Paolo]

Module.constants

all the top level constants in the current program, including classnames!, Module.constants => ["IO", "Binding", "TypeError", "Module", "Config", "RUBY_PLATFORM", "SecurityError", "NilClass", "Mutex", "APPLE_GEM_HOME", "TrueClass", "ENV", "MAXHISTSIZE", "ARGF", "DTracer", "Precision", "LocalJumpError", "ZeroDivisionError", "ARGV", "RubyToken", "Symbol", "Time", "Continuation", "SyntaxError", "SystemStackError", "Integer", "Piero", "CROSS_COMPILING", "Math", "STDIN", "ArgumentError", "NoMemoryError", "Class", "RUBY_PATCHLEVEL", "Etc", "TRUE", "Struct", "ThreadError", "FloatDomainError", "RubyLex", "SignalException", "Enumerable", "UnboundMethod", "StringIO", "LoadError", "DateTime", "Queue", "RUBY_FRAMEWORK", "Proc", "Float", "Exception", "IndexError", "STDOUT", "Fixnum", "SystemCallError", "SLex", "VERSION", "Date", "YAML", "FalseClass", "MatchData", "SystemExit", "ObjectSpace", "Readline", "IOError", "NoMethodError", "Thread", "Numeric", "Exception2MessageMapper", "Interrupt", "FileTest", "NotImplementedError", "Object", "RUBY_VERSION", "Hash", "String", "GC", "ETC_IRBRC_LOADED", "STDERR", "RangeError", "Marshal", "Regexp", "Bignum", "Kernel", "RELEASE_DATE", "Method", "MatchingData", "FALSE", "Signal", "RegexpError", "Gem", "EOFError", "Kconv", "ConditionVariable", "TOPLEVEL_BINDING", "StandardError", "IRB", "File", "RuntimeError", "RUBY_RELEASE_DATE", "ThreadGroup", "RbConfig", "A", "Data", "Array", "Range", "NameError", "Process", "Rational", "HISTFILE", "Errno", "PLATFORM", "Dir", "NIL", "SizedQueue", "Comparable", "RUBY_FRAMEWORK_VERSION", "ScriptError"]

Module.nesting

shows the current path

spell: Open Classes

you can always reopen existing classes and modify them on the fly

even Ruby standard library classes

example: the Money gem

http://github.com/aflatter/money/blob/master/lib/money/core_extensions.rb

class Numeric def to_money Money.new(self * 100) end end

Careful with That Axe, Eugene

spell: Monkeypatch, the dark side of Open Classes, you can end up with strange bugs...

Constants

Any reference that begins with an uppercase letter, including the names of classes and modules, is a constant

Constant scope

similar to a file system, directories => modules and classes, files => constants

Constants are uniquely identified by their paths, path separator is a double colon, MyModule::MyClass::MyConstant

you can change the value of a constant

but you'll get a warning from the interpreter

and as far as class names are constants, you can change the value of a class name, String = MyClass

spell: Namespace

using a module just as a container of constants

e.g. Rake, Rake old versions, had classes such as, Task, FileTask, Rake recent versions, define its classes inside a Rake module, module Rake class Task # ...

What's in an object?

instance variables

@my_field

obj.instance_variables

methods

Object#methods, try yourself, for example "hi there!".methods

where are the methods?, in the classes, object methods, "hi!".size, vs, class' instance methods, String#size, String.instance_methods == "Hi man".methods, String.methods != "Hi man".methods

Classes themselves are nothing but objects!

this means that...

Class class has instance methods, that is, methods that every Class instance (e.g. the String class) has, Class.instance_methods(false), superclass, new

in Ruby a class is every instance of the Class class, to tell if an object obj is a class, make him this question, obj.is_a? Class

QUIZ TIME!, >> Class.is_a? Class => ? >> String.is_a? String => ? >> String.is_a? Class => ? >> "Hello there".is_a? String => ?

let's explore the Class hierarchy, Class.superclass # => Module, Module.superclass # => Object, therefore, a Class is a Module, plus the possibility to create instances (Class#new) and the capability to arrange its instances into hierarchies (Class#superclass), and a Module is just a bunch of methods

how do you create an instance?, obj = MyClass.new, obj is a reference to the new object, obj is a variable

how do you create a Class instance?, class MyClass; end, MyClass is a reference to the new class, MyClass is a constant, therefore, a class name is a reference to the relative Class instance, String is a reference to the String class, that is, an instance of the Class class

so, what's an object?, it's a bunch of instance variables, plus a link to a class, the object methods live in the object's class, where they're called "instance methods" of the class

so, what's a class?, it's an object, it's an instance of Class, plus a list of instance methods, e.g String#length, plus a link to a superclass, plus a "new" method