Create your own awesome maps

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account?
Log In

Ruby Object Model: method calling by Mind Map: Ruby Object Model:
method calling
5.0 stars - 1 reviews range from 0 to 5

Ruby Object Model: method calling

What happens when you call a method?

when you call a method, Ruby does two things

it finds the method, method lookup

it executes the method, with the receiver as self

Method Execution

Every line of Ruby code is executed inside an object:

the current object

self, only one object at a time can take the role of self, when you call a method, the receiver takes the role of self, and from that moment on..., all instance variables are instance variables of self, all methods called without an explicit receiver are called on self, as soon as your code explicitly calls a method on some other object, that other object becomes self, in a class or module definition, the role of self is taken by the class or module, outside a class/module definition and outside a method, the role of self is taken by the top level context, main, it's the object you're in when you're at the tip level of the call stack, current object quiz, puts "current object is #{self}" class MyClass puts "current object is #{self}" def my_stunning_method puts "current object is #{self}" end end MyClass.new.my_stunning_method, current object is ? current object is ? current object is ?, see also http://rubylearning.com/satishtalim/ruby_self.html

spell: Kernel Method

class Object includes Kernel

so, Kernel gets into every object's ancestors chain, and you can call the Kernel methods from anywhere, e.g. puts, Kernel.private_instance_methods.grep(/^pr/) # => [:printf, :print, :proc]

you can take advantage of this mechanism yourself, if you add a method to Kernel, this Kernel Method will be available to all objects, e.g. The RubyGems example, require 'rubygems' gem 'rails', '= 2.3.2', You can call gem() from anywhere because it's a Kernel Method, module Kernel def gem(gem_name, *version_requirements) # ...

Method Lookup

to find a method, Ruby goes into the receivers's class, and from there it climbs the ancestors chain until it finds the method

aka "one step to the right, than up" rule

Class#ancestors method asks a class for its ancestors chain

ancestors chain also include modules!, also known as "include classes", When you include a module in a class, Ruby creates an anonymous class that wraps the module and inserts the anonymous class in the chain, just above the including class itself, exercise: a simple program to spot in which class/module a method is defined, require 'test/unit' class KernelTest < Test::Unit::TestCase def test_lookup_method assert_equal(Kernel, "a string instance".where_is(:send)) assert_equal(Kernel, "a string instance".where_is("instance_variable_get")) assert_equal(String, "a string instance".where_is("to_s")) assert_equal(Integer, 12.where_is("ceil")) assert_equal(Numeric, 12.where_is("nonzero?")) assert_nil("ciao".where_is("unexisting_method")) end end, require 'test/unit' module Kernel def where_is method_name self.class.ancestors.detect do |each| each.instance_methods(false).include? method_name.to_s end end end class KernelTest < Test::Unit::TestCase def test_lookup_method assert_equal(Kernel, "a string instance".where_is(:send)) assert_equal(Kernel, "a string instance".where_is("instance_variable_get")) assert_equal(String, "a string instance".where_is("to_s")) assert_equal(Fixnum, 12.where_is("to_s")) assert_equal(Integer, 12.where_is("ceil")) assert_equal(Numeric, 12.where_is("nonzero?")) assert_nil("ciao".where_is("unexisting_method")) end end