Ruby Object Model: a first insight

Começar. É Gratuito
ou inscrever-se com seu endereço de e-mail
Ruby Object Model: a first insight por 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

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

1.2.1.1. including classnames!

1.2.1.1.1. 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.1. http://github.com/aflatter/money/blob/master/lib/money/core_extensions.rb

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

2.3. Careful with That Axe, Eugene

2.3.1. spell: Monkeypatch

2.3.1.1. the dark side of Open Classes

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

3.2.1.1. directories => modules and classes

3.2.1.2. files => constants

3.2.2. Constants are uniquely identified by their paths

3.2.2.1. path separator is a double colon

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

3.3.2.1. String = MyClass

3.4. spell: Namespace

3.4.1. using a module just as a container of constants

3.4.2. e.g. Rake

3.4.2.1. Rake old versions

3.4.2.1.1. had classes such as

3.4.2.2. Rake recent versions

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

4.2.1.1. try yourself, for example "hi there!".methods

4.2.2. where are the methods?

4.2.2.1. in the classes

4.2.2.1.1. object methods

4.2.2.1.2. vs

4.2.2.1.3. class' instance methods

4.2.2.2. String.instance_methods == "Hi man".methods

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

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

5.1.1.1.1. Class.instance_methods(false)

5.1.2. in Ruby a class is every instance of the Class class

5.1.2.1. to tell if an object obj is a class, make him this question

5.1.2.1.1. obj.is_a? Class

5.1.3. QUIZ TIME!

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

5.1.4.1. Class.superclass # => Module

5.1.4.2. Module.superclass # => Object

5.1.4.3. 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)

5.1.4.3.1. and a Module is just a bunch of methods

5.1.5. how do you create an instance?

5.1.5.1. obj = MyClass.new

5.1.5.1.1. obj is a reference to the new object

5.1.5.1.2. obj is a variable

5.1.6. how do you create a Class instance?

5.1.6.1. class MyClass; end

5.1.6.1.1. MyClass is a reference to the new class

5.1.6.1.2. MyClass is a constant

5.1.6.1.3. therefore, a class name is a reference to the relative Class instance

5.1.7. so, what's an object?

5.1.7.1. it's a bunch of instance variables, plus a link to a class

5.1.7.2. the object methods live in the object's class

5.1.7.2.1. where they're called "instance methods" of the class

5.1.8. so, what's a class?

5.1.8.1. it's an object

5.1.8.1.1. it's an instance of Class

5.1.8.2. plus a list of instance methods

5.1.8.2.1. e.g String#length

5.1.8.3. plus a link to a superclass

5.1.8.4. plus a "new" method