Ruby class extension in Rails works when declared locally, returns `nil` when imported from `/lib/`
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
add a comment |
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
1
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 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 '18 at 4:30
It seems to returnnil
to me when a key does not exist:a: 1.dig(:b) => nil
– Marcin Kołodziej
Nov 16 '18 at 4:39
add a comment |
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
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
ruby-on-rails ruby class class-extensions
edited Nov 15 '18 at 2:10
James
asked Nov 15 '18 at 1:27
JamesJames
1418
1418
1
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 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 '18 at 4:30
It seems to returnnil
to me when a key does not exist:a: 1.dig(:b) => nil
– Marcin Kołodziej
Nov 16 '18 at 4:39
add a comment |
1
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 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 '18 at 4:30
It seems to returnnil
to me when a key does not exist:a: 1.dig(:b) => nil
– Marcin Kołodziej
Nov 16 '18 at 4:39
1
1
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 at 3:01
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 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 '18 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 '18 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 '18 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 '18 at 4:39
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 15 '18 at 3:41
Ilya KonyukhovIlya Konyukhov
2,3081718
2,3081718
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Side question: Are you implementing Hash#dig?
– Marcin Kołodziej
Nov 15 '18 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 '18 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 '18 at 4:39