Skip to content

Instantly share code, notes, and snippets.

@pasberth
Last active December 16, 2015 05:08
Show Gist options
  • Select an option

  • Save pasberth/5381921 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/5381921 to your computer and use it in GitHub Desktop.

Syntax

Receiver message arg1 arg2 arg3 ==  self message receiver aeg1 arg2 arg3

self message returns a function whose type is receiver -> arg1 -> arg2 -> arg3 .

self message is undefined, but the message will be hooked by core function like AUTOLOAD, and the message is transmitted into receiver.

self is the main context object. In the language, a context is an object.

Refinement

Defines a function on self simply.

In Ruby

def self.method_missing(messsage, receiver, *args, &block)
  receiver.__send__(message, *args, &block)
end

Receiver.message(arg1, arg2, arg3) == self.message(Receiver, arg1, arg2, arg3)

def self.hoge(receiver, *args, &block)
  if receiver.is_a?(NilClass)
    receiver
  else
    receiver.__send__(message, *args, &block)
  end
end

Receiver.hoge(arg1, arg2, arg3) == self.hoge(Receiver, arg1, arg2, arg3) == Receiver.hoge(arg1, arg2, arg3)
nil.hoge(arg1, arg2, arg3) == self.hoge(nil, arg1, arg2, arg3) == nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment