Ruby class extension in Rails works when declared locally, returns `nil` when imported from `/lib/`











up vote
0
down vote

favorite
1












TLDR: A hash extension works flawlessly, returning the desired output, when included locally in my Mailer, but always returns nil when imported from a module in lib/, even though the class method is successfully loaded.



When I declare the extension in my mailer.rb file, before my class definition, as in:



class Hash
def try_deep(*fields)
...
end
end

class MyMailer < ApplicationMailer
some_hash.try_deep(:some_key)
end


it works flawlessly, but this is bad practice. I thought it better to declare the extension in /lib/core_ext/hash/try_deep.rb and then require it in the Mailer, as in:



/lib/core_ext/hash/try_deep.rb:



module CoreExtensions
module Hash
module TryDeep
def try_deep(*fields)
...
end
end
end
end


/my_mailer.rb:



require 'core_ext/hash/try_deep'

class MyMailer < ApplicationMailer
Hash.include CoreExtensions::Hash::TryDeep
some_hash.try_deep(:some_key)
end









share|improve this question




















  • 1




    Side question: Are you implementing Hash#dig?
    – Marcin Kołodziej
    Nov 15 at 3:01










  • @MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
    – James
    Nov 16 at 4:30












  • It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
    – Marcin Kołodziej
    Nov 16 at 4:39















up vote
0
down vote

favorite
1












TLDR: A hash extension works flawlessly, returning the desired output, when included locally in my Mailer, but always returns nil when imported from a module in lib/, even though the class method is successfully loaded.



When I declare the extension in my mailer.rb file, before my class definition, as in:



class Hash
def try_deep(*fields)
...
end
end

class MyMailer < ApplicationMailer
some_hash.try_deep(:some_key)
end


it works flawlessly, but this is bad practice. I thought it better to declare the extension in /lib/core_ext/hash/try_deep.rb and then require it in the Mailer, as in:



/lib/core_ext/hash/try_deep.rb:



module CoreExtensions
module Hash
module TryDeep
def try_deep(*fields)
...
end
end
end
end


/my_mailer.rb:



require 'core_ext/hash/try_deep'

class MyMailer < ApplicationMailer
Hash.include CoreExtensions::Hash::TryDeep
some_hash.try_deep(:some_key)
end









share|improve this question




















  • 1




    Side question: Are you implementing Hash#dig?
    – Marcin Kołodziej
    Nov 15 at 3:01










  • @MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
    – James
    Nov 16 at 4:30












  • It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
    – Marcin Kołodziej
    Nov 16 at 4:39













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





TLDR: A hash extension works flawlessly, returning the desired output, when included locally in my Mailer, but always returns nil when imported from a module in lib/, even though the class method is successfully loaded.



When I declare the extension in my mailer.rb file, before my class definition, as in:



class Hash
def try_deep(*fields)
...
end
end

class MyMailer < ApplicationMailer
some_hash.try_deep(:some_key)
end


it works flawlessly, but this is bad practice. I thought it better to declare the extension in /lib/core_ext/hash/try_deep.rb and then require it in the Mailer, as in:



/lib/core_ext/hash/try_deep.rb:



module CoreExtensions
module Hash
module TryDeep
def try_deep(*fields)
...
end
end
end
end


/my_mailer.rb:



require 'core_ext/hash/try_deep'

class MyMailer < ApplicationMailer
Hash.include CoreExtensions::Hash::TryDeep
some_hash.try_deep(:some_key)
end









share|improve this question















TLDR: A hash extension works flawlessly, returning the desired output, when included locally in my Mailer, but always returns nil when imported from a module in lib/, even though the class method is successfully loaded.



When I declare the extension in my mailer.rb file, before my class definition, as in:



class Hash
def try_deep(*fields)
...
end
end

class MyMailer < ApplicationMailer
some_hash.try_deep(:some_key)
end


it works flawlessly, but this is bad practice. I thought it better to declare the extension in /lib/core_ext/hash/try_deep.rb and then require it in the Mailer, as in:



/lib/core_ext/hash/try_deep.rb:



module CoreExtensions
module Hash
module TryDeep
def try_deep(*fields)
...
end
end
end
end


/my_mailer.rb:



require 'core_ext/hash/try_deep'

class MyMailer < ApplicationMailer
Hash.include CoreExtensions::Hash::TryDeep
some_hash.try_deep(:some_key)
end






ruby-on-rails ruby class class-extensions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 2:10

























asked Nov 15 at 1:27









James

1318




1318








  • 1




    Side question: Are you implementing Hash#dig?
    – Marcin Kołodziej
    Nov 15 at 3:01










  • @MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
    – James
    Nov 16 at 4:30












  • It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
    – Marcin Kołodziej
    Nov 16 at 4:39














  • 1




    Side question: Are you implementing Hash#dig?
    – Marcin Kołodziej
    Nov 15 at 3:01










  • @MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
    – James
    Nov 16 at 4:30












  • It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
    – Marcin Kołodziej
    Nov 16 at 4:39








1




1




Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 at 3:01




Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 at 3:01












@MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
– James
Nov 16 at 4:30






@MarcinKołodziej Good question. The reason I'm implementing this is to be able to safely dig for attributes in a mixed-object-version environment. Older objects are missing certain newer attributes. Hash#dig, while it seems great at first, doesn't solve my problem because it throws an error rather than 'nil' when it digs for a key that doesn't exist. For my idiosyncratic use-case (sending an email), 'nil' is what I want when a key isn't found.
– James
Nov 16 at 4:30














It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
– Marcin Kołodziej
Nov 16 at 4:39




It seems to return nil to me when a key does not exist: {a: 1}.dig(:b) => nil
– Marcin Kołodziej
Nov 16 at 4:39












1 Answer
1






active

oldest

votes

















up vote
2
down vote













You need to inject your custom method into Hash outside of your class:



my_mailer.rb:



require 'core_ext/hash/try_deep'

class Hash
include CoreExtensions::Hash::TryDeep
end

class MyMailer < ApplicationMailer
some_hash.try_deep(:some_key)
end





share|improve this answer





















    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',
    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%2f53311167%2fruby-class-extension-in-rails-works-when-declared-locally-returns-nil-when-im%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    You need to inject your custom method into Hash outside of your class:



    my_mailer.rb:



    require 'core_ext/hash/try_deep'

    class Hash
    include CoreExtensions::Hash::TryDeep
    end

    class MyMailer < ApplicationMailer
    some_hash.try_deep(:some_key)
    end





    share|improve this answer

























      up vote
      2
      down vote













      You need to inject your custom method into Hash outside of your class:



      my_mailer.rb:



      require 'core_ext/hash/try_deep'

      class Hash
      include CoreExtensions::Hash::TryDeep
      end

      class MyMailer < ApplicationMailer
      some_hash.try_deep(:some_key)
      end





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        You need to inject your custom method into Hash outside of your class:



        my_mailer.rb:



        require 'core_ext/hash/try_deep'

        class Hash
        include CoreExtensions::Hash::TryDeep
        end

        class MyMailer < ApplicationMailer
        some_hash.try_deep(:some_key)
        end





        share|improve this answer












        You need to inject your custom method into Hash outside of your class:



        my_mailer.rb:



        require 'core_ext/hash/try_deep'

        class Hash
        include CoreExtensions::Hash::TryDeep
        end

        class MyMailer < ApplicationMailer
        some_hash.try_deep(:some_key)
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 3:41









        Ilya Konyukhov

        2,226718




        2,226718






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53311167%2fruby-class-extension-in-rails-works-when-declared-locally-returns-nil-when-im%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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents