What is an elegant Ruby way to have a condition of 'nil'?










1















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?










share|improve this question

















  • 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
















1















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?










share|improve this question

















  • 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














1












1








1








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 10:10









clockworkpcclockworkpc

13410




13410







  • 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













  • 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








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













4 Answers
4






active

oldest

votes


















1














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?






share|improve this answer
































    2














    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





    share|improve this answer






























      1














      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





      share|improve this answer























      • 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



















      0














      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 .






      share|improve this answer























      • 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










      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
      );



      );













      draft saved

      draft discarded


















      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









      1














      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?






      share|improve this answer





























        1














        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?






        share|improve this answer



























          1












          1








          1







          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?






          share|improve this answer















          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?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 11:07

























          answered Nov 14 '18 at 10:24









          lacostenycoderlacostenycoder

          3,73111228




          3,73111228























              2














              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





              share|improve this answer



























                2














                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





                share|improve this answer

























                  2












                  2








                  2







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 11:19









                  InterdictorInterdictor

                  413




                  413





















                      1














                      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





                      share|improve this answer























                      • 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
















                      1














                      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





                      share|improve this answer























                      • 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














                      1












                      1








                      1







                      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





                      share|improve this answer













                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 10:21









                      Jonathan DuarteJonathan Duarte

                      42237




                      42237












                      • 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

















                      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












                      0














                      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 .






                      share|improve this answer























                      • 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















                      0














                      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 .






                      share|improve this answer























                      • 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













                      0












                      0








                      0







                      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 .






                      share|improve this answer













                      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 .







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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

















                      • 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

















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto