Top Banner
LESSON 4: METHOD_MISSING AND ITS FRIENDS DYNAMIC RUBY
27

Dynamic Ruby. Lesson #4: method_missing and its friends

Feb 09, 2017

Download

Software

Alex Mikitenko
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic Ruby. Lesson #4: method_missing and its friends

LESSON 4: METHOD_MISSING AND ITS FRIENDS

DYNAMIC RUBY

Page 2: Dynamic Ruby. Lesson #4: method_missing and its friends

RUBY METHOD LOOKUP

Page 3: Dynamic Ruby. Lesson #4: method_missing and its friends

RUBY METHOD LOOKUP

ruby method lookup

Page 4: Dynamic Ruby. Lesson #4: method_missing and its friends

RUBY METHOD LOOKUP

Page 5: Dynamic Ruby. Lesson #4: method_missing and its friends

RUBY METHOD LOOKUP

Поскольку подключаемые модули образовывают цепочку наследования, мы можем обращаться к одноименным методам суперкласса через метод super.

Page 6: Dynamic Ruby. Lesson #4: method_missing and its friends

RUBY METHOD LOOKUP

Page 7: Dynamic Ruby. Lesson #4: method_missing and its friends

PREPEND

Page 8: Dynamic Ruby. Lesson #4: method_missing and its friends

PRETEND

prepend позволяет нарушить классический механизм наследования объектов, определив подключаемому модулю больший приоритет, чем классу как таковому.

Page 9: Dynamic Ruby. Lesson #4: method_missing and its friends

respect my authority*

* иначе статья 148 УК РФ

Page 10: Dynamic Ruby. Lesson #4: method_missing and its friends

PRETEND

Page 11: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

Page 12: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

method_missing

Page 13: Dynamic Ruby. Lesson #4: method_missing and its friends
Page 14: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

method_missing это приватный метод, определенный еще на уровне BasicObject. В этой реализации он просто вызывает исключение NoMethodError.

Page 15: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

Page 16: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

Хм. Раз method_missing это просто инстанс метод BasicObject, значит его можно переопределить в дочерних классах.

Page 17: Dynamic Ruby. Lesson #4: method_missing and its friends

METHOD_MISSING

Page 18: Dynamic Ruby. Lesson #4: method_missing and its friends

RESPOND_TO_MISSING?

Page 19: Dynamic Ruby. Lesson #4: method_missing and its friends

RESPOND_TO_MISSING?

Page 20: Dynamic Ruby. Lesson #4: method_missing and its friends

Сложно искать черную кошку в темной комнате.

Особенно, если ее там нет.

Page 21: Dynamic Ruby. Lesson #4: method_missing and its friends

RESPOND_TO_MISSING?

Page 22: Dynamic Ruby. Lesson #4: method_missing and its friends

DELEGATORS

Page 23: Dynamic Ruby. Lesson #4: method_missing and its friends

DELEGATORS

Как можно использовать мощь method_missing? Например, при создании делегаторов (на самом деле ruby имеет хорошие встроенные механизмы делегирования, такие как модуль Forwardable и класс SimpleDelegator, но нам важно показать возможности method_missing).

Page 24: Dynamic Ruby. Lesson #4: method_missing and its friends

DELEGATORS

Page 25: Dynamic Ruby. Lesson #4: method_missing and its friends

BLOCKS, PROCS AND LAMBDAS

REFERENCES

▸ https://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works/

▸ http://dev.af83.com/2012/10/19/ruby-2-0-module-prepend.html

▸ https://makandracards.com/makandra/23171-how-ruby-method-lookup-works

▸ http://blog.marc-andre.ca/2010/11/15/methodmissing-politely/

▸ https://habrahabr.ru/post/269785/

Page 26: Dynamic Ruby. Lesson #4: method_missing and its friends

THANK YOU!

Alex Mikitenko [email protected] https://github.com/nonkor

Page 27: Dynamic Ruby. Lesson #4: method_missing and its friends

TO BE CONTINUED…