what is difference between extended and extend_object hook












1















can someone explain the difference between extended and extend_object hook. There is no proper documentation on the internet for this.










share|improve this question



























    1















    can someone explain the difference between extended and extend_object hook. There is no proper documentation on the internet for this.










    share|improve this question

























      1












      1








      1








      can someone explain the difference between extended and extend_object hook. There is no proper documentation on the internet for this.










      share|improve this question














      can someone explain the difference between extended and extend_object hook. There is no proper documentation on the internet for this.







      ruby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 12:25









      Sanjay SalunkheSanjay Salunkhe

      154




      154
























          2 Answers
          2






          active

          oldest

          votes


















          3














          In essence, extend_object is the method that does the extending. You can override it to for example create a typecheck before extend happens:



          module Foo
          def self.extend_object(obj)
          raise(TypeError, "No strings!") if obj.is_a?(String)
          super # need to call super or object
          # won't be extended
          end

          def self.extended(obj)
          puts "#{obj.inspect} was extended with #{self}"
          end
          end

          1.extend(Foo)
          "1".extend(Foo) # raises TypeError: No strings!





          share|improve this answer


























          • why do we need super keyword /

            – Sanjay Salunkhe
            Nov 14 '18 at 12:38











          • Because otherwise the object won't be extended.

            – Kimmo Lehto
            Nov 14 '18 at 12:40













          • so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

            – mrzasa
            Nov 14 '18 at 13:32











          • @mrzasa correct.

            – Kimmo Lehto
            Nov 14 '18 at 13:39



















          0














          extend_object is a private API method used by extend and it adds module constants and methods




          extend_object(p1) private



          Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.




          https://apidock.com/ruby/Module/extend_object



          extended is just a callback that can be used by a developer to run specific code when a module is being extended.
          https://ruby-doc.org/core-2.2.0/Module.html#method-i-extended






          share|improve this answer
























          • extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

            – Sanjay Salunkhe
            Nov 14 '18 at 12:36













          • As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

            – mrzasa
            Nov 14 '18 at 12:38













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300220%2fwhat-is-difference-between-extended-and-extend-object-hook%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          In essence, extend_object is the method that does the extending. You can override it to for example create a typecheck before extend happens:



          module Foo
          def self.extend_object(obj)
          raise(TypeError, "No strings!") if obj.is_a?(String)
          super # need to call super or object
          # won't be extended
          end

          def self.extended(obj)
          puts "#{obj.inspect} was extended with #{self}"
          end
          end

          1.extend(Foo)
          "1".extend(Foo) # raises TypeError: No strings!





          share|improve this answer


























          • why do we need super keyword /

            – Sanjay Salunkhe
            Nov 14 '18 at 12:38











          • Because otherwise the object won't be extended.

            – Kimmo Lehto
            Nov 14 '18 at 12:40













          • so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

            – mrzasa
            Nov 14 '18 at 13:32











          • @mrzasa correct.

            – Kimmo Lehto
            Nov 14 '18 at 13:39
















          3














          In essence, extend_object is the method that does the extending. You can override it to for example create a typecheck before extend happens:



          module Foo
          def self.extend_object(obj)
          raise(TypeError, "No strings!") if obj.is_a?(String)
          super # need to call super or object
          # won't be extended
          end

          def self.extended(obj)
          puts "#{obj.inspect} was extended with #{self}"
          end
          end

          1.extend(Foo)
          "1".extend(Foo) # raises TypeError: No strings!





          share|improve this answer


























          • why do we need super keyword /

            – Sanjay Salunkhe
            Nov 14 '18 at 12:38











          • Because otherwise the object won't be extended.

            – Kimmo Lehto
            Nov 14 '18 at 12:40













          • so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

            – mrzasa
            Nov 14 '18 at 13:32











          • @mrzasa correct.

            – Kimmo Lehto
            Nov 14 '18 at 13:39














          3












          3








          3







          In essence, extend_object is the method that does the extending. You can override it to for example create a typecheck before extend happens:



          module Foo
          def self.extend_object(obj)
          raise(TypeError, "No strings!") if obj.is_a?(String)
          super # need to call super or object
          # won't be extended
          end

          def self.extended(obj)
          puts "#{obj.inspect} was extended with #{self}"
          end
          end

          1.extend(Foo)
          "1".extend(Foo) # raises TypeError: No strings!





          share|improve this answer















          In essence, extend_object is the method that does the extending. You can override it to for example create a typecheck before extend happens:



          module Foo
          def self.extend_object(obj)
          raise(TypeError, "No strings!") if obj.is_a?(String)
          super # need to call super or object
          # won't be extended
          end

          def self.extended(obj)
          puts "#{obj.inspect} was extended with #{self}"
          end
          end

          1.extend(Foo)
          "1".extend(Foo) # raises TypeError: No strings!






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 13:39

























          answered Nov 14 '18 at 12:34









          Kimmo LehtoKimmo Lehto

          4,10911328




          4,10911328













          • why do we need super keyword /

            – Sanjay Salunkhe
            Nov 14 '18 at 12:38











          • Because otherwise the object won't be extended.

            – Kimmo Lehto
            Nov 14 '18 at 12:40













          • so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

            – mrzasa
            Nov 14 '18 at 13:32











          • @mrzasa correct.

            – Kimmo Lehto
            Nov 14 '18 at 13:39



















          • why do we need super keyword /

            – Sanjay Salunkhe
            Nov 14 '18 at 12:38











          • Because otherwise the object won't be extended.

            – Kimmo Lehto
            Nov 14 '18 at 12:40













          • so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

            – mrzasa
            Nov 14 '18 at 13:32











          • @mrzasa correct.

            – Kimmo Lehto
            Nov 14 '18 at 13:39

















          why do we need super keyword /

          – Sanjay Salunkhe
          Nov 14 '18 at 12:38





          why do we need super keyword /

          – Sanjay Salunkhe
          Nov 14 '18 at 12:38













          Because otherwise the object won't be extended.

          – Kimmo Lehto
          Nov 14 '18 at 12:40







          Because otherwise the object won't be extended.

          – Kimmo Lehto
          Nov 14 '18 at 12:40















          so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

          – mrzasa
          Nov 14 '18 at 13:32





          so its not before_extended - it's the method doing extension. If you override it without calling super you'll be unable to extend.

          – mrzasa
          Nov 14 '18 at 13:32













          @mrzasa correct.

          – Kimmo Lehto
          Nov 14 '18 at 13:39





          @mrzasa correct.

          – Kimmo Lehto
          Nov 14 '18 at 13:39













          0














          extend_object is a private API method used by extend and it adds module constants and methods




          extend_object(p1) private



          Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.




          https://apidock.com/ruby/Module/extend_object



          extended is just a callback that can be used by a developer to run specific code when a module is being extended.
          https://ruby-doc.org/core-2.2.0/Module.html#method-i-extended






          share|improve this answer
























          • extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

            – Sanjay Salunkhe
            Nov 14 '18 at 12:36













          • As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

            – mrzasa
            Nov 14 '18 at 12:38


















          0














          extend_object is a private API method used by extend and it adds module constants and methods




          extend_object(p1) private



          Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.




          https://apidock.com/ruby/Module/extend_object



          extended is just a callback that can be used by a developer to run specific code when a module is being extended.
          https://ruby-doc.org/core-2.2.0/Module.html#method-i-extended






          share|improve this answer
























          • extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

            – Sanjay Salunkhe
            Nov 14 '18 at 12:36













          • As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

            – mrzasa
            Nov 14 '18 at 12:38
















          0












          0








          0







          extend_object is a private API method used by extend and it adds module constants and methods




          extend_object(p1) private



          Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.




          https://apidock.com/ruby/Module/extend_object



          extended is just a callback that can be used by a developer to run specific code when a module is being extended.
          https://ruby-doc.org/core-2.2.0/Module.html#method-i-extended






          share|improve this answer













          extend_object is a private API method used by extend and it adds module constants and methods




          extend_object(p1) private



          Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend.




          https://apidock.com/ruby/Module/extend_object



          extended is just a callback that can be used by a developer to run specific code when a module is being extended.
          https://ruby-doc.org/core-2.2.0/Module.html#method-i-extended







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 12:34









          mrzasamrzasa

          10.4k104078




          10.4k104078













          • extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

            – Sanjay Salunkhe
            Nov 14 '18 at 12:36













          • As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

            – mrzasa
            Nov 14 '18 at 12:38





















          • extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

            – Sanjay Salunkhe
            Nov 14 '18 at 12:36













          • As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

            – mrzasa
            Nov 14 '18 at 12:38



















          extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

          – Sanjay Salunkhe
          Nov 14 '18 at 12:36







          extend_object and extended both are hooks and are called automatically whenever i extend module in my class , so what is the need of extend_object ?

          – Sanjay Salunkhe
          Nov 14 '18 at 12:36















          As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

          – mrzasa
          Nov 14 '18 at 12:38







          As the docs says - it extends an object using the module - it does the job. You probably don't have to touch it.

          – mrzasa
          Nov 14 '18 at 12:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300220%2fwhat-is-difference-between-extended-and-extend-object-hook%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine