Ruby Object Model: a first insight

Get Started. It's Free
or sign up with your email address
Ruby Object Model: a first insight by Mind Map: Ruby Object Model: a first insight

1. Some useful methods I discovered

1.1. Module#constants

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

1.2. Module.constants

1.2.1. 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"]

1.3. Module.nesting

1.3.1. shows the current path

2. spell: Open Classes

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

2.1.1. even Ruby standard library classes

2.2. example: the Money gem


2.2.2. class Numeric def to_money * 100) end end

2.3. Careful with That Axe, Eugene

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

3. Constants

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

3.2. Constant scope

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

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

3.3. you can change the value of a constant

3.3.1. but you'll get a warning from the interpreter

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

3.4. spell: Namespace

3.4.1. using a module just as a container of constants

3.4.2. e.g. Rake Rake old versions had classes such as Rake recent versions define its classes inside a Rake module

4. What's in an object?

4.1. instance variables

4.1.1. @my_field

4.1.2. obj.instance_variables

4.2. methods

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

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

5. Classes themselves are nothing but objects!

5.1. this means that...

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

5.1.2. 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

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

5.1.4. 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

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

5.1.6. 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

5.1.7. 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

5.1.8. 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