{"id":267082,"date":"2015-10-29T16:57:02","date_gmt":"2015-10-29T13:57:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=267082"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=267082","title":{"rendered":"Ghost Methods \u0432 Ruby (\u043f\u0435\u0440\u0435\u0432\u043e\u0434)"},"content":{"rendered":"<p>       <i>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043d\u0438\u0433\u0438 <a href=\"https:\/\/pragprog.com\/book\/ppmetr2\/metaprogramming-ruby-2\">Metaprogramming Ruby 2<\/a> \u0437\u0430 \u0430\u0432\u0442\u043e\u0440\u0441\u0442\u0432\u043e\u043c \u041f\u0430\u043e\u043b\u043e \u041f\u0435\u0440\u0440\u043e\u0442\u0442\u0430 (Paolo Perrotta).<\/i><\/p>\n<h4>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 method_missing?<\/h4>\n<p>  \u0412 \u0420\u0443\u0431\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442, \u043d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0430\u043c \u043e\u0448\u0438\u0431\u043a\u0443. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"ruby\">class Lawyer; end nick = Lawyer.new nick = talk_simple  NoMethodError: undefine method 'talk_simple' for #&lt;Lawyer:0x007f801aa81938&gt; <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u043b\u0438 \u0432\u044b \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u043c\u0435\u0442\u043e\u0434\u043e\u0432? \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 talk_simple, Ruby \u0438\u0434\u0435\u0442 \u0432 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 nick, \u0438 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0430\u043c \u043c\u0435\u0442\u043e\u0434\u044b. \u0415\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0442\u043e\u0434 \u0442\u0430\u043c, \u043e\u043d \u0438\u0449\u0435\u0442 \u0435\u0433\u043e \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043f\u043e\u0442\u043e\u043c \u0432 Object \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0432 BasicObject. \u041d\u0443 \u0438 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 Ruby \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0438\u0433\u0434\u0435 \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0442\u043e\u0434 talk_simple, \u043e\u043d \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 method_missing \u0434\u043b\u044f nick\u2019a. Ruby \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0435\u0441\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 BasicObject \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0430\u043c\u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439, \u0432\u044b \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"ruby\">nick.send :method_missing, :my_method  NoMethodError: undefine method 'talk_simple' for #&lt;Lawyer:0x007f801aa81938&gt; <\/code><\/pre>\n<p>  \u0422\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0432\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0442\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 Ruby, \u0412\u044b \u0441\u043a\u0430\u0437\u0430\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u201c\u042f \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u043f\u043e\u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 :my_method, \u0430 \u0435\u0433\u043e \u043d\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e, \u0432\u0435\u0440\u043d\u0438 \u043c\u043d\u0435 \u043e\u0448\u0438\u0431\u043a\u0443.\u201d BasicObject#method_missing \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 NoMethodError, \u043f\u043e \u0441\u0443\u0442\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u043d \u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442.<\/p>\n<h4>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 method_missing<\/h4>\n<p>  \u0421\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u0430\u043c \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 method_missing \u0441\u0430\u043c\u0438\u043c\u0438. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u0430 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432. \u041a\u0430\u0436\u0434\u044b\u0439 \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0437\u043e\u0432 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b.<\/p>\n<pre><code class=\"ruby\">class Lawyer   def method_missing(method, *args)     puts &quot;You called: #{method}(#{args.join(', ')})&quot;     puts &quot;(You also passed it a block)&quot; if block_given?   end end  bob = Lawyer.new bob.talk_simple('a', 'b') do # a block end  You called: talk_simple(a, b) (You also passed it a block) <\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 method_missing \u0434\u0430\u0435\u0442 \u0432\u0430\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b, \u0430 \u0442\u043e-\u0435\u0441\u0442\u044c \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u0435\u0435.<\/p>\n<h4>Ghost Methods<\/h4>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0441\u043f\u0430\u0441\u0442\u0438\u0441\u044c \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438\u0445 \u0447\u0435\u0440\u0435\u0437 method_missing. \u042d\u0442\u043e \u043a\u0430\u043a \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u0443, \u0447\u0442\u043e \u201c\u0415\u0441\u043b\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0439 \u044d\u0442\u043e\u201d. \u0421\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0447\u0435\u0440\u0435\u0437 method_missing, \u0432\u0441\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u0442\u0440\u044e\u043a \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u041f\u0440\u0438\u0437\u0440\u0430\u0447\u043d\u044b\u043c \u041c\u0435\u0442\u043e\u0434\u043e\u043c (Ghost Method). \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u044b.<\/p>\n<h4>Hashie \u043f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<p>  \u0413\u0435\u043c Hashie \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0430\u0433\u0438\u0438 \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c Hashie::Mash. Mash \u044d\u0442\u043e \u0431\u043e\u043b\u0435\u0435 \u043c\u043e\u0449\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Ruby OpenStruct, \u044d\u0442\u043e \u0445\u0435\u0448-\u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 Ruby. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043d\u043e\u0432\u044b\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u0432\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430. \u0412\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<pre><code class=\"ruby\">require 'hashie'  icecream = Hashie::Mash.new icecream.flavor = 'strawberry' icecream.flavor                        # =&gt; 'strawberry'  <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e Hashie::Mash \u0441\u0443\u0431\u043a\u043b\u0430\u0441 Ruby Hash, \u0438 \u044d\u0442\u0438 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u043f\u043e \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438\u0437\u0440\u0430\u0447\u043d\u044b\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438. \u0412\u043e\u0442 \u043a\u0430\u043a \u0437\u0434\u0435\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d method_missing:<\/p>\n<pre><code class=\"ruby\">module Hashie   class Mash&lt; Hashie::Hash     def method_missing(method_name, *args, &blk)       return self.[](method_name, &blk) if key?(method_name)       match = method_name.to_s.match(\/(.*?)([?=!]?)$\/)       case match[2]       when &quot;=&quot;         self[match[1]] = args.first                 # ...             else         default(method_name, *args, &blk)       end     end      # ...   end end <\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0438\u043c\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u044d\u0442\u043e \u0438\u043c\u044f \u043a\u043b\u044e\u0447\u0430 \u0432 \u0445\u0435\u0448\u0435 (\u043a\u0430\u043a flavor), \u0442\u043e\u0433\u0434\u0430 Hashie::Mash#method_missing \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 [] \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0438\u043c\u044f \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u201c=\u201d, \u0442\u043e\u0433\u0434\u0430 method_missing \u043e\u0442\u0440\u0435\u0437\u0430\u0435\u0442 \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0438\u043c\u044f \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0442\u043e \u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0435, \u0442\u043e\u0433\u0434\u0430 method_missing \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. Hashie::Mash \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u201c?\u201d.<\/p>\n<h4>respond_to_missing?<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u0440\u0438\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e respond_to?, \u0442\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0435 \u0434\u0435\u043b\u043e Ruby \u0432\u0430\u043c \u0441\u043e\u0432\u0440\u0435\u0442.<\/p>\n<pre><code class=\"ruby\">john = Lawyer.new john.respond_to?(:talk_simple)        # =&gt; false <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0442\u0438\u0447\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c respond_to \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a. \u041d\u043e Ruby \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0447\u0442\u043e \u0431\u044b \u043f\u043e\u0434\u0440\u0443\u0436\u0438\u0442\u044c respond_to \u0438 \u043f\u0440\u0438\u0437\u0440\u0430\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<p>  respond_to? \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 respond_to_missing? \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 true \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0440\u0438\u0437\u0440\u0430\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e \u0431\u044b \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u0438\u043d\u043e\u0441\u0438\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0439\u0442\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 method_missing \u0438 respond_to_missing.<\/p>\n<pre><code class=\"ruby\">class Lawyer   # ...    def respond_to_missing?(method, include_private = false)     methods.include?(method) || super   end end  bill = Lawyer.new bill.respond_to?(:talk_simple)          # =&gt; true <\/code><\/pre>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u043d\u044c\u0448\u0435 \u0440\u0443\u0431\u0438\u0441\u0442\u044b \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438 \u0441\u0430\u043c respond_to? \u043c\u0435\u0442\u043e\u0434. \u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0441\u0442\u044c respond_to_missing? \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 respond_to? \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 method_missing.<\/p>\n<p>  Ghost Methods\u200a\u2014\u200a\u044d\u0442\u043e \u043b\u0438\u0448\u044c \u043c\u0430\u043b\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0432 Ruby 2.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/269785\/\"> http:\/\/habrahabr.ru\/post\/269785\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <i>\u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043d\u0438\u0433\u0438 <a href=\"https:\/\/pragprog.com\/book\/ppmetr2\/metaprogramming-ruby-2\">Metaprogramming Ruby 2<\/a> \u0437\u0430 \u0430\u0432\u0442\u043e\u0440\u0441\u0442\u0432\u043e\u043c \u041f\u0430\u043e\u043b\u043e \u041f\u0435\u0440\u0440\u043e\u0442\u0442\u0430 (Paolo Perrotta).<\/i><\/p>\n<h4>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 method_missing?<\/h4>\n<p>  \u0412 \u0420\u0443\u0431\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442, \u043d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0430\u043c \u043e\u0448\u0438\u0431\u043a\u0443. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"ruby\">class Lawyer; end nick = Lawyer.new nick = talk_simple  NoMethodError: undefine method 'talk_simple' for #&lt;Lawyer:0x007f801aa81938&gt; <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u043b\u0438 \u0432\u044b \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u043c\u0435\u0442\u043e\u0434\u043e\u0432? \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u043c\u0435\u0442\u043e\u0434 talk_simple, Ruby \u0438\u0434\u0435\u0442 \u0432 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 nick, \u0438 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u0430\u043c \u043c\u0435\u0442\u043e\u0434\u044b. \u0415\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0442\u043e\u0434 \u0442\u0430\u043c, \u043e\u043d \u0438\u0449\u0435\u0442 \u0435\u0433\u043e \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043f\u043e\u0442\u043e\u043c \u0432 Object \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0432 BasicObject. \u041d\u0443 \u0438 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 Ruby \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0438\u0433\u0434\u0435 \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0442\u043e\u0434 talk_simple, \u043e\u043d \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 method_missing \u0434\u043b\u044f nick\u2019a. Ruby \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0435\u0441\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 BasicObject \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-267082","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=267082"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/267082\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}