Ruby class extension in Rails works when declared locally, returns `nil` when imported from `/lib/`
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 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 returnnil
to me when a key does not exist:{a: 1}.dig(:b) => nil
– Marcin Kołodziej
Nov 16 at 4:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 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 returnnil
to me when a key does not exist:{a: 1}.dig(:b) => nil
– Marcin Kołodziej
Nov 16 at 4:39
add a comment |
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 returnnil
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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 at 3:41
Ilya Konyukhov
2,226718
2,226718
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.
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.
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 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