Cannot extract data from a hash
I have an array of hashes:
array_hash = [{product: 'A', qty: 1}, {product: 'B', qty: 2}]
I want to get the value of the first key "product".
I get the expected hash with this code:
get_first_hash = array_hash[0]
# => {product: 'A', qty: 1}
However, this code returns nil:
get_value = get_first_hash['product']
# => nil
I hope someones can tell me about this.
ruby hash
add a comment |
I have an array of hashes:
array_hash = [{product: 'A', qty: 1}, {product: 'B', qty: 2}]
I want to get the value of the first key "product".
I get the expected hash with this code:
get_first_hash = array_hash[0]
# => {product: 'A', qty: 1}
However, this code returns nil:
get_value = get_first_hash['product']
# => nil
I hope someones can tell me about this.
ruby hash
1
This is because:product == 'product' #=> false
– iGian
Nov 20 '18 at 19:23
Your hash does not have a key"product"(although you have key:product). Don't use (hash) literals without knowing what it means.
– sawa
Nov 21 '18 at 4:01
add a comment |
I have an array of hashes:
array_hash = [{product: 'A', qty: 1}, {product: 'B', qty: 2}]
I want to get the value of the first key "product".
I get the expected hash with this code:
get_first_hash = array_hash[0]
# => {product: 'A', qty: 1}
However, this code returns nil:
get_value = get_first_hash['product']
# => nil
I hope someones can tell me about this.
ruby hash
I have an array of hashes:
array_hash = [{product: 'A', qty: 1}, {product: 'B', qty: 2}]
I want to get the value of the first key "product".
I get the expected hash with this code:
get_first_hash = array_hash[0]
# => {product: 'A', qty: 1}
However, this code returns nil:
get_value = get_first_hash['product']
# => nil
I hope someones can tell me about this.
ruby hash
ruby hash
edited Nov 21 '18 at 3:57
sawa
131k29205303
131k29205303
asked Nov 20 '18 at 15:40
Nguyen Khoi NguyenNguyen Khoi Nguyen
64
64
1
This is because:product == 'product' #=> false
– iGian
Nov 20 '18 at 19:23
Your hash does not have a key"product"(although you have key:product). Don't use (hash) literals without knowing what it means.
– sawa
Nov 21 '18 at 4:01
add a comment |
1
This is because:product == 'product' #=> false
– iGian
Nov 20 '18 at 19:23
Your hash does not have a key"product"(although you have key:product). Don't use (hash) literals without knowing what it means.
– sawa
Nov 21 '18 at 4:01
1
1
This is because
:product == 'product' #=> false– iGian
Nov 20 '18 at 19:23
This is because
:product == 'product' #=> false– iGian
Nov 20 '18 at 19:23
Your hash does not have a key
"product" (although you have key :product). Don't use (hash) literals without knowing what it means.– sawa
Nov 21 '18 at 4:01
Your hash does not have a key
"product" (although you have key :product). Don't use (hash) literals without knowing what it means.– sawa
Nov 21 '18 at 4:01
add a comment |
2 Answers
2
active
oldest
votes
Hope this suffices!
array_hash.first[:product]
If you want the second one you just do
array_hash.second[:product]
Also, .first is just a helper for doing array_hash[0], but the issue behind you getting nil is that you need to represent it with a :symbol in Ruby
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
add a comment |
I'm guessing you're accustom to using Hash in Ruby on Rails, which subclasses the Hash class that comes with Ruby as HashWithIndifferentAccess, and you usually never forced to look at this as a developer (in Rails). It should also be noted, that Rails directly modifies Ruby's base Hash class, so you actually have a lot more available to you in Rails (or, specifically when ActiveSupport is loaded).
Ruby (pure), however doesn't mess around when it comes to keys in the hash. This allows for some powerful applications—remember, everything in Ruby is an Object.
So, 'string' and :symbol are both objects, and different objects at that. You can even use a Class as a key in your hash.
{
Object => :object_symbol,
Class => :class_symbol
}
So when you're trying to access values behind a key in your hash, make sure you use the exact key itself.
In your example above, you're using the symbol product as your key. Ruby uses : to denote that.
{
product: 'A',
qty: 1
}
This is the equivalent of saying
{
:product => 'A',
:qty => 1
}
So what you want to call is get_value = get_first_hash[:product]
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
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%2f53396521%2fcannot-extract-data-from-a-hash%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
Hope this suffices!
array_hash.first[:product]
If you want the second one you just do
array_hash.second[:product]
Also, .first is just a helper for doing array_hash[0], but the issue behind you getting nil is that you need to represent it with a :symbol in Ruby
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
add a comment |
Hope this suffices!
array_hash.first[:product]
If you want the second one you just do
array_hash.second[:product]
Also, .first is just a helper for doing array_hash[0], but the issue behind you getting nil is that you need to represent it with a :symbol in Ruby
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
add a comment |
Hope this suffices!
array_hash.first[:product]
If you want the second one you just do
array_hash.second[:product]
Also, .first is just a helper for doing array_hash[0], but the issue behind you getting nil is that you need to represent it with a :symbol in Ruby
Hope this suffices!
array_hash.first[:product]
If you want the second one you just do
array_hash.second[:product]
Also, .first is just a helper for doing array_hash[0], but the issue behind you getting nil is that you need to represent it with a :symbol in Ruby
answered Nov 20 '18 at 15:57
kallelundgren93kallelundgren93
1189
1189
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
add a comment |
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
Thank you for your answer. It really works for me
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
add a comment |
I'm guessing you're accustom to using Hash in Ruby on Rails, which subclasses the Hash class that comes with Ruby as HashWithIndifferentAccess, and you usually never forced to look at this as a developer (in Rails). It should also be noted, that Rails directly modifies Ruby's base Hash class, so you actually have a lot more available to you in Rails (or, specifically when ActiveSupport is loaded).
Ruby (pure), however doesn't mess around when it comes to keys in the hash. This allows for some powerful applications—remember, everything in Ruby is an Object.
So, 'string' and :symbol are both objects, and different objects at that. You can even use a Class as a key in your hash.
{
Object => :object_symbol,
Class => :class_symbol
}
So when you're trying to access values behind a key in your hash, make sure you use the exact key itself.
In your example above, you're using the symbol product as your key. Ruby uses : to denote that.
{
product: 'A',
qty: 1
}
This is the equivalent of saying
{
:product => 'A',
:qty => 1
}
So what you want to call is get_value = get_first_hash[:product]
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
add a comment |
I'm guessing you're accustom to using Hash in Ruby on Rails, which subclasses the Hash class that comes with Ruby as HashWithIndifferentAccess, and you usually never forced to look at this as a developer (in Rails). It should also be noted, that Rails directly modifies Ruby's base Hash class, so you actually have a lot more available to you in Rails (or, specifically when ActiveSupport is loaded).
Ruby (pure), however doesn't mess around when it comes to keys in the hash. This allows for some powerful applications—remember, everything in Ruby is an Object.
So, 'string' and :symbol are both objects, and different objects at that. You can even use a Class as a key in your hash.
{
Object => :object_symbol,
Class => :class_symbol
}
So when you're trying to access values behind a key in your hash, make sure you use the exact key itself.
In your example above, you're using the symbol product as your key. Ruby uses : to denote that.
{
product: 'A',
qty: 1
}
This is the equivalent of saying
{
:product => 'A',
:qty => 1
}
So what you want to call is get_value = get_first_hash[:product]
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
add a comment |
I'm guessing you're accustom to using Hash in Ruby on Rails, which subclasses the Hash class that comes with Ruby as HashWithIndifferentAccess, and you usually never forced to look at this as a developer (in Rails). It should also be noted, that Rails directly modifies Ruby's base Hash class, so you actually have a lot more available to you in Rails (or, specifically when ActiveSupport is loaded).
Ruby (pure), however doesn't mess around when it comes to keys in the hash. This allows for some powerful applications—remember, everything in Ruby is an Object.
So, 'string' and :symbol are both objects, and different objects at that. You can even use a Class as a key in your hash.
{
Object => :object_symbol,
Class => :class_symbol
}
So when you're trying to access values behind a key in your hash, make sure you use the exact key itself.
In your example above, you're using the symbol product as your key. Ruby uses : to denote that.
{
product: 'A',
qty: 1
}
This is the equivalent of saying
{
:product => 'A',
:qty => 1
}
So what you want to call is get_value = get_first_hash[:product]
I'm guessing you're accustom to using Hash in Ruby on Rails, which subclasses the Hash class that comes with Ruby as HashWithIndifferentAccess, and you usually never forced to look at this as a developer (in Rails). It should also be noted, that Rails directly modifies Ruby's base Hash class, so you actually have a lot more available to you in Rails (or, specifically when ActiveSupport is loaded).
Ruby (pure), however doesn't mess around when it comes to keys in the hash. This allows for some powerful applications—remember, everything in Ruby is an Object.
So, 'string' and :symbol are both objects, and different objects at that. You can even use a Class as a key in your hash.
{
Object => :object_symbol,
Class => :class_symbol
}
So when you're trying to access values behind a key in your hash, make sure you use the exact key itself.
In your example above, you're using the symbol product as your key. Ruby uses : to denote that.
{
product: 'A',
qty: 1
}
This is the equivalent of saying
{
:product => 'A',
:qty => 1
}
So what you want to call is get_value = get_first_hash[:product]
edited Nov 20 '18 at 16:05
answered Nov 20 '18 at 15:58
VolteVolte
1,5691322
1,5691322
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
add a comment |
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
Thank you so much for your detail answer. It really works for me. Because I am a newbie in Ruby and I am trying to learn by myself so there is something I can not handle it. Thank you
– Nguyen Khoi Nguyen
Nov 21 '18 at 16:46
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
@NguyenKhoiNguyen here at Stack Overflow, we try to accept answers that have the most explanation that also addresses the question. Please consider changing your accepted answer.
– Volte
Nov 22 '18 at 14:52
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%2f53396521%2fcannot-extract-data-from-a-hash%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
This is because
:product == 'product' #=> false– iGian
Nov 20 '18 at 19:23
Your hash does not have a key
"product"(although you have key:product). Don't use (hash) literals without knowing what it means.– sawa
Nov 21 '18 at 4:01