What is an elegant Ruby way to have a condition of 'nil'?
Is there a more elegant way to write this code?
def create_a_hash_from_a_collection
my_hash =
collection_of_hashes.each do |key, value|
my_hash[key] = if my_hash[key].nil?
my_hash[key] = value
end
my_hash
end
The line that seems clumsy to me is this:
my_hash[key] = if my_hash[key].nil?
Is there a shorthand way of expressing it?
ruby hash block
add a comment |
Is there a more elegant way to write this code?
def create_a_hash_from_a_collection
my_hash =
collection_of_hashes.each do |key, value|
my_hash[key] = if my_hash[key].nil?
my_hash[key] = value
end
my_hash
end
The line that seems clumsy to me is this:
my_hash[key] = if my_hash[key].nil?
Is there a shorthand way of expressing it?
ruby hash block
5
Why would you need this anyway if you assigningvalue
on next line?
– Martin Zinovsky
Nov 14 '18 at 10:17
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
1
can you please add output ofcollection_of_hashes
variable? is it array of hashes?
– Gagan Gami
Nov 14 '18 at 11:13
add a comment |
Is there a more elegant way to write this code?
def create_a_hash_from_a_collection
my_hash =
collection_of_hashes.each do |key, value|
my_hash[key] = if my_hash[key].nil?
my_hash[key] = value
end
my_hash
end
The line that seems clumsy to me is this:
my_hash[key] = if my_hash[key].nil?
Is there a shorthand way of expressing it?
ruby hash block
Is there a more elegant way to write this code?
def create_a_hash_from_a_collection
my_hash =
collection_of_hashes.each do |key, value|
my_hash[key] = if my_hash[key].nil?
my_hash[key] = value
end
my_hash
end
The line that seems clumsy to me is this:
my_hash[key] = if my_hash[key].nil?
Is there a shorthand way of expressing it?
ruby hash block
ruby hash block
asked Nov 14 '18 at 10:10
clockworkpcclockworkpc
13410
13410
5
Why would you need this anyway if you assigningvalue
on next line?
– Martin Zinovsky
Nov 14 '18 at 10:17
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
1
can you please add output ofcollection_of_hashes
variable? is it array of hashes?
– Gagan Gami
Nov 14 '18 at 11:13
add a comment |
5
Why would you need this anyway if you assigningvalue
on next line?
– Martin Zinovsky
Nov 14 '18 at 10:17
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
1
can you please add output ofcollection_of_hashes
variable? is it array of hashes?
– Gagan Gami
Nov 14 '18 at 11:13
5
5
Why would you need this anyway if you assigning
value
on next line?– Martin Zinovsky
Nov 14 '18 at 10:17
Why would you need this anyway if you assigning
value
on next line?– Martin Zinovsky
Nov 14 '18 at 10:17
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
1
1
can you please add output of
collection_of_hashes
variable? is it array of hashes?– Gagan Gami
Nov 14 '18 at 11:13
can you please add output of
collection_of_hashes
variable? is it array of hashes?– Gagan Gami
Nov 14 '18 at 11:13
add a comment |
4 Answers
4
active
oldest
votes
You can use ||=
operator which does exactly what you want
my_hash[key] ||=
The rest of my answer here is because I'm not sure what a "collection of hashes" is, so my best guess is that it would be an array of hashes. If I'm wrong, let me know and disregard the rest of this answer.
It seems that the rest of your method may not do what it sounds like you're trying to do. Consider:
@collection_of_hashes = [foo: 'bar', baz: 'qux']
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |key, value|
# this is not actually doing anything here and returns same with or
# without the following line
# my_hash[key] ||=
my_hash[key] = value
end
my_hash
end
#=> :foo=>"bar"=>nil, :baz=>"qux"=>nil
But what you probably want is
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |hash|
hash.keys.each do |k|
my_hash[k] = hash[k]
end
end
my_hash
end
#=> :foo=>"bar", :baz=>"qux"
But also keep in mind, if any of your "collection of hashes" which we would tend to assume would be an array of hashes, contain the same key, which one wins? This code, it would be the last item in the array's key value. What is the actual goal of your method?
add a comment |
If you want to have a brand new hash for each key in your initial hash you need to initialise it with a block:
hash = Hash.new hash[key] =
hash[:foo].object_id == hash[:bar].object_id #=> false
Otherwise, if you do this, It will be always the same default hash
hash = Hash.new()
hash[:foo].object_id == hash[:bar].object_id #=> true
add a comment |
I guess what you want is to initialise your my_hash
with a default value, so then you don't need to check if it's nil or not.
That can be done using the Hash.new
constructor, compare:
my_hash =
puts my_hash['no_existing_key'] #=> nil
my_hash = Hash.new()
puts my_hash['no_existing_key'] #=>
You then can reduce your code to:
def create_a_hash_from_a_collection
my_hash = Hash.new()
collection_of_hashes.each do |key, value|
my_hash[key] = value
end
my_hash
end
Although as mentioned by Interdictor in his answer. This defaultHash
is a single reference somy_hash['no_existing_key'].merge!(name: 'Nope')
means thatmy_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator andmy_hash['no_existing_key']
still not have a actual value andmy_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin toHash.new h[k] =
where each non existing key gets its own newHash
and becomes part of the originalHash
– engineersmnky
Nov 14 '18 at 13:51
add a comment |
Since you are assigning value anyway, maybe you could use my_hash[key] = value ||
?
So, if value
to assign is nil
, the value of that key becomes .
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
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%2f53297664%2fwhat-is-an-elegant-ruby-way-to-have-a-condition-of-nil%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use ||=
operator which does exactly what you want
my_hash[key] ||=
The rest of my answer here is because I'm not sure what a "collection of hashes" is, so my best guess is that it would be an array of hashes. If I'm wrong, let me know and disregard the rest of this answer.
It seems that the rest of your method may not do what it sounds like you're trying to do. Consider:
@collection_of_hashes = [foo: 'bar', baz: 'qux']
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |key, value|
# this is not actually doing anything here and returns same with or
# without the following line
# my_hash[key] ||=
my_hash[key] = value
end
my_hash
end
#=> :foo=>"bar"=>nil, :baz=>"qux"=>nil
But what you probably want is
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |hash|
hash.keys.each do |k|
my_hash[k] = hash[k]
end
end
my_hash
end
#=> :foo=>"bar", :baz=>"qux"
But also keep in mind, if any of your "collection of hashes" which we would tend to assume would be an array of hashes, contain the same key, which one wins? This code, it would be the last item in the array's key value. What is the actual goal of your method?
add a comment |
You can use ||=
operator which does exactly what you want
my_hash[key] ||=
The rest of my answer here is because I'm not sure what a "collection of hashes" is, so my best guess is that it would be an array of hashes. If I'm wrong, let me know and disregard the rest of this answer.
It seems that the rest of your method may not do what it sounds like you're trying to do. Consider:
@collection_of_hashes = [foo: 'bar', baz: 'qux']
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |key, value|
# this is not actually doing anything here and returns same with or
# without the following line
# my_hash[key] ||=
my_hash[key] = value
end
my_hash
end
#=> :foo=>"bar"=>nil, :baz=>"qux"=>nil
But what you probably want is
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |hash|
hash.keys.each do |k|
my_hash[k] = hash[k]
end
end
my_hash
end
#=> :foo=>"bar", :baz=>"qux"
But also keep in mind, if any of your "collection of hashes" which we would tend to assume would be an array of hashes, contain the same key, which one wins? This code, it would be the last item in the array's key value. What is the actual goal of your method?
add a comment |
You can use ||=
operator which does exactly what you want
my_hash[key] ||=
The rest of my answer here is because I'm not sure what a "collection of hashes" is, so my best guess is that it would be an array of hashes. If I'm wrong, let me know and disregard the rest of this answer.
It seems that the rest of your method may not do what it sounds like you're trying to do. Consider:
@collection_of_hashes = [foo: 'bar', baz: 'qux']
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |key, value|
# this is not actually doing anything here and returns same with or
# without the following line
# my_hash[key] ||=
my_hash[key] = value
end
my_hash
end
#=> :foo=>"bar"=>nil, :baz=>"qux"=>nil
But what you probably want is
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |hash|
hash.keys.each do |k|
my_hash[k] = hash[k]
end
end
my_hash
end
#=> :foo=>"bar", :baz=>"qux"
But also keep in mind, if any of your "collection of hashes" which we would tend to assume would be an array of hashes, contain the same key, which one wins? This code, it would be the last item in the array's key value. What is the actual goal of your method?
You can use ||=
operator which does exactly what you want
my_hash[key] ||=
The rest of my answer here is because I'm not sure what a "collection of hashes" is, so my best guess is that it would be an array of hashes. If I'm wrong, let me know and disregard the rest of this answer.
It seems that the rest of your method may not do what it sounds like you're trying to do. Consider:
@collection_of_hashes = [foo: 'bar', baz: 'qux']
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |key, value|
# this is not actually doing anything here and returns same with or
# without the following line
# my_hash[key] ||=
my_hash[key] = value
end
my_hash
end
#=> :foo=>"bar"=>nil, :baz=>"qux"=>nil
But what you probably want is
def create_a_hash_from_a_collection
my_hash =
@collection_of_hashes.each do |hash|
hash.keys.each do |k|
my_hash[k] = hash[k]
end
end
my_hash
end
#=> :foo=>"bar", :baz=>"qux"
But also keep in mind, if any of your "collection of hashes" which we would tend to assume would be an array of hashes, contain the same key, which one wins? This code, it would be the last item in the array's key value. What is the actual goal of your method?
edited Nov 14 '18 at 11:07
answered Nov 14 '18 at 10:24
lacostenycoderlacostenycoder
3,73111228
3,73111228
add a comment |
add a comment |
If you want to have a brand new hash for each key in your initial hash you need to initialise it with a block:
hash = Hash.new hash[key] =
hash[:foo].object_id == hash[:bar].object_id #=> false
Otherwise, if you do this, It will be always the same default hash
hash = Hash.new()
hash[:foo].object_id == hash[:bar].object_id #=> true
add a comment |
If you want to have a brand new hash for each key in your initial hash you need to initialise it with a block:
hash = Hash.new hash[key] =
hash[:foo].object_id == hash[:bar].object_id #=> false
Otherwise, if you do this, It will be always the same default hash
hash = Hash.new()
hash[:foo].object_id == hash[:bar].object_id #=> true
add a comment |
If you want to have a brand new hash for each key in your initial hash you need to initialise it with a block:
hash = Hash.new hash[key] =
hash[:foo].object_id == hash[:bar].object_id #=> false
Otherwise, if you do this, It will be always the same default hash
hash = Hash.new()
hash[:foo].object_id == hash[:bar].object_id #=> true
If you want to have a brand new hash for each key in your initial hash you need to initialise it with a block:
hash = Hash.new hash[key] =
hash[:foo].object_id == hash[:bar].object_id #=> false
Otherwise, if you do this, It will be always the same default hash
hash = Hash.new()
hash[:foo].object_id == hash[:bar].object_id #=> true
answered Nov 14 '18 at 11:19
InterdictorInterdictor
413
413
add a comment |
add a comment |
I guess what you want is to initialise your my_hash
with a default value, so then you don't need to check if it's nil or not.
That can be done using the Hash.new
constructor, compare:
my_hash =
puts my_hash['no_existing_key'] #=> nil
my_hash = Hash.new()
puts my_hash['no_existing_key'] #=>
You then can reduce your code to:
def create_a_hash_from_a_collection
my_hash = Hash.new()
collection_of_hashes.each do |key, value|
my_hash[key] = value
end
my_hash
end
Although as mentioned by Interdictor in his answer. This defaultHash
is a single reference somy_hash['no_existing_key'].merge!(name: 'Nope')
means thatmy_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator andmy_hash['no_existing_key']
still not have a actual value andmy_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin toHash.new h[k] =
where each non existing key gets its own newHash
and becomes part of the originalHash
– engineersmnky
Nov 14 '18 at 13:51
add a comment |
I guess what you want is to initialise your my_hash
with a default value, so then you don't need to check if it's nil or not.
That can be done using the Hash.new
constructor, compare:
my_hash =
puts my_hash['no_existing_key'] #=> nil
my_hash = Hash.new()
puts my_hash['no_existing_key'] #=>
You then can reduce your code to:
def create_a_hash_from_a_collection
my_hash = Hash.new()
collection_of_hashes.each do |key, value|
my_hash[key] = value
end
my_hash
end
Although as mentioned by Interdictor in his answer. This defaultHash
is a single reference somy_hash['no_existing_key'].merge!(name: 'Nope')
means thatmy_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator andmy_hash['no_existing_key']
still not have a actual value andmy_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin toHash.new h[k] =
where each non existing key gets its own newHash
and becomes part of the originalHash
– engineersmnky
Nov 14 '18 at 13:51
add a comment |
I guess what you want is to initialise your my_hash
with a default value, so then you don't need to check if it's nil or not.
That can be done using the Hash.new
constructor, compare:
my_hash =
puts my_hash['no_existing_key'] #=> nil
my_hash = Hash.new()
puts my_hash['no_existing_key'] #=>
You then can reduce your code to:
def create_a_hash_from_a_collection
my_hash = Hash.new()
collection_of_hashes.each do |key, value|
my_hash[key] = value
end
my_hash
end
I guess what you want is to initialise your my_hash
with a default value, so then you don't need to check if it's nil or not.
That can be done using the Hash.new
constructor, compare:
my_hash =
puts my_hash['no_existing_key'] #=> nil
my_hash = Hash.new()
puts my_hash['no_existing_key'] #=>
You then can reduce your code to:
def create_a_hash_from_a_collection
my_hash = Hash.new()
collection_of_hashes.each do |key, value|
my_hash[key] = value
end
my_hash
end
answered Nov 14 '18 at 10:21
Jonathan DuarteJonathan Duarte
42237
42237
Although as mentioned by Interdictor in his answer. This defaultHash
is a single reference somy_hash['no_existing_key'].merge!(name: 'Nope')
means thatmy_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator andmy_hash['no_existing_key']
still not have a actual value andmy_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin toHash.new h[k] =
where each non existing key gets its own newHash
and becomes part of the originalHash
– engineersmnky
Nov 14 '18 at 13:51
add a comment |
Although as mentioned by Interdictor in his answer. This defaultHash
is a single reference somy_hash['no_existing_key'].merge!(name: 'Nope')
means thatmy_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator andmy_hash['no_existing_key']
still not have a actual value andmy_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin toHash.new h[k] =
where each non existing key gets its own newHash
and becomes part of the originalHash
– engineersmnky
Nov 14 '18 at 13:51
Although as mentioned by Interdictor in his answer. This default
Hash
is a single reference so my_hash['no_existing_key'].merge!(name: 'Nope')
means that my_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator and my_hash['no_existing_key']
still not have a actual value and my_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin to Hash.new h[k] =
where each non existing key gets its own new Hash
and becomes part of the original Hash
– engineersmnky
Nov 14 '18 at 13:51
Although as mentioned by Interdictor in his answer. This default
Hash
is a single reference so my_hash['no_existing_key'].merge!(name: 'Nope')
means that my_hash['another_no_existing_key'] #=> name: 'Nope'
because this default value in this case is acting almost like an accumulator and my_hash['no_existing_key']
still not have a actual value and my_hash.key?('no_existing_key') #=> false
. Where as the original implementation is akin to Hash.new h[k] =
where each non existing key gets its own new Hash
and becomes part of the original Hash
– engineersmnky
Nov 14 '18 at 13:51
add a comment |
Since you are assigning value anyway, maybe you could use my_hash[key] = value ||
?
So, if value
to assign is nil
, the value of that key becomes .
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
add a comment |
Since you are assigning value anyway, maybe you could use my_hash[key] = value ||
?
So, if value
to assign is nil
, the value of that key becomes .
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
add a comment |
Since you are assigning value anyway, maybe you could use my_hash[key] = value ||
?
So, if value
to assign is nil
, the value of that key becomes .
Since you are assigning value anyway, maybe you could use my_hash[key] = value ||
?
So, if value
to assign is nil
, the value of that key becomes .
answered Nov 14 '18 at 16:35
iGianiGian
4,1082623
4,1082623
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
add a comment |
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
This seems like the most logical solution however it has a different result than the current code
– engineersmnky
Nov 14 '18 at 17:59
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
@engineersmnky is it actually clear what the OP is trying to achieve?
– lacostenycoder
Nov 15 '18 at 19:32
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
Not at all. Thus no answer from me :)
– engineersmnky
Nov 15 '18 at 20:13
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%2f53297664%2fwhat-is-an-elegant-ruby-way-to-have-a-condition-of-nil%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
5
Why would you need this anyway if you assigning
value
on next line?– Martin Zinovsky
Nov 14 '18 at 10:17
Are we to assume a "collection" here is an array of hashes? Where will this data be provided from?
– lacostenycoder
Nov 14 '18 at 11:01
1
can you please add output of
collection_of_hashes
variable? is it array of hashes?– Gagan Gami
Nov 14 '18 at 11:13