How to pass &:read as argument to File.open as indicated by Rubocop





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















I have this code



File.open(file_name, 'r') { |file| file.read }


but Rubocop is warning:




Offenses:



Style/SymbolProc: Pass &:read as argument to open instead of a
block.




How do you do this?










share|improve this question




















  • 2





    You would like to read stackoverflow.com/a/9429972/10522579

    – ray
    Jan 3 at 7:07






  • 1





    You could just do File.read(file_name).

    – Kimmo Lehto
    Jan 3 at 9:27




















5















I have this code



File.open(file_name, 'r') { |file| file.read }


but Rubocop is warning:




Offenses:



Style/SymbolProc: Pass &:read as argument to open instead of a
block.




How do you do this?










share|improve this question




















  • 2





    You would like to read stackoverflow.com/a/9429972/10522579

    – ray
    Jan 3 at 7:07






  • 1





    You could just do File.read(file_name).

    – Kimmo Lehto
    Jan 3 at 9:27
















5












5








5








I have this code



File.open(file_name, 'r') { |file| file.read }


but Rubocop is warning:




Offenses:



Style/SymbolProc: Pass &:read as argument to open instead of a
block.




How do you do this?










share|improve this question
















I have this code



File.open(file_name, 'r') { |file| file.read }


but Rubocop is warning:




Offenses:



Style/SymbolProc: Pass &:read as argument to open instead of a
block.




How do you do this?







ruby rubocop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:36









Stefan

77.5k895148




77.5k895148










asked Jan 3 at 3:24









ObromiosObromios

4,63233368




4,63233368








  • 2





    You would like to read stackoverflow.com/a/9429972/10522579

    – ray
    Jan 3 at 7:07






  • 1





    You could just do File.read(file_name).

    – Kimmo Lehto
    Jan 3 at 9:27
















  • 2





    You would like to read stackoverflow.com/a/9429972/10522579

    – ray
    Jan 3 at 7:07






  • 1





    You could just do File.read(file_name).

    – Kimmo Lehto
    Jan 3 at 9:27










2




2





You would like to read stackoverflow.com/a/9429972/10522579

– ray
Jan 3 at 7:07





You would like to read stackoverflow.com/a/9429972/10522579

– ray
Jan 3 at 7:07




1




1





You could just do File.read(file_name).

– Kimmo Lehto
Jan 3 at 9:27







You could just do File.read(file_name).

– Kimmo Lehto
Jan 3 at 9:27














3 Answers
3






active

oldest

votes


















5














I just created a file named "t.txt" that contains "Hello, Worldn". We can read that as follows.



File.open('t.txt', 'r', &:read)
#=> "Hello, Worldn"


Incidentally, as the default of the second argument is 'r', it suffices to write:



File.open('t.txt', &:read)


Here's another example:



"This is A Test".gsub('i', &:upcase)
#=> "ThIs Is A Test"


In other words, include the proc (e.g., &:read) as the last argument.






share|improve this answer

































    3














    File.open(file_name, 'r', &:read)



    Rubocop wants you to use the 'symbol to proc' feature in Ruby instead of defining a complete block. This is purely stylistic, and doesn't affect the code execution. You can find it in the Rubocop style guide.






    share|improve this answer



















    • 1





      Learned new things from you, thank you very much :)

      – Tiw
      Jan 3 at 16:38



















    1














    You can look up the offense in RuboCop's docs, e.g. Style/SymbolProc – it usually shows a "bad" and a "good" example:



    # bad
    something.map { |s| s.upcase }

    # good
    something.map(&:upcase)


    If this doesn't help, you can have RuboCop auto-correct the offense (for cops supporting auto-correction like this one).



    Given a file test.rb:



    # frozen_string_literal: true

    File.open(file_name, 'r') { |file| file.read }


    Run rubocop -a: (the actual output depends on your config)



    $ rubocop -a test.rb
    Inspecting 1 file
    C

    Offenses:

    test.rb:3:27: C: [Corrected] Style/SymbolProc: Pass &:read as an argument to open instead of a block.
    File.open(file_name, 'r') { |file| file.read }
    ^^^^^^^^^^^^^^^^^^^^

    1 file inspected, 1 offense detected, 1 offense corrected


    And test.rb will become:



    # frozen_string_literal: true

    File.open(file_name, 'r', &:read)





    share|improve this answer


























      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%2f54015929%2fhow-to-pass-read-as-argument-to-file-open-as-indicated-by-rubocop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      I just created a file named "t.txt" that contains "Hello, Worldn". We can read that as follows.



      File.open('t.txt', 'r', &:read)
      #=> "Hello, Worldn"


      Incidentally, as the default of the second argument is 'r', it suffices to write:



      File.open('t.txt', &:read)


      Here's another example:



      "This is A Test".gsub('i', &:upcase)
      #=> "ThIs Is A Test"


      In other words, include the proc (e.g., &:read) as the last argument.






      share|improve this answer






























        5














        I just created a file named "t.txt" that contains "Hello, Worldn". We can read that as follows.



        File.open('t.txt', 'r', &:read)
        #=> "Hello, Worldn"


        Incidentally, as the default of the second argument is 'r', it suffices to write:



        File.open('t.txt', &:read)


        Here's another example:



        "This is A Test".gsub('i', &:upcase)
        #=> "ThIs Is A Test"


        In other words, include the proc (e.g., &:read) as the last argument.






        share|improve this answer




























          5












          5








          5







          I just created a file named "t.txt" that contains "Hello, Worldn". We can read that as follows.



          File.open('t.txt', 'r', &:read)
          #=> "Hello, Worldn"


          Incidentally, as the default of the second argument is 'r', it suffices to write:



          File.open('t.txt', &:read)


          Here's another example:



          "This is A Test".gsub('i', &:upcase)
          #=> "ThIs Is A Test"


          In other words, include the proc (e.g., &:read) as the last argument.






          share|improve this answer















          I just created a file named "t.txt" that contains "Hello, Worldn". We can read that as follows.



          File.open('t.txt', 'r', &:read)
          #=> "Hello, Worldn"


          Incidentally, as the default of the second argument is 'r', it suffices to write:



          File.open('t.txt', &:read)


          Here's another example:



          "This is A Test".gsub('i', &:upcase)
          #=> "ThIs Is A Test"


          In other words, include the proc (e.g., &:read) as the last argument.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 12:02









          Stefan

          77.5k895148




          77.5k895148










          answered Jan 3 at 3:41









          Cary SwovelandCary Swoveland

          71k54167




          71k54167

























              3














              File.open(file_name, 'r', &:read)



              Rubocop wants you to use the 'symbol to proc' feature in Ruby instead of defining a complete block. This is purely stylistic, and doesn't affect the code execution. You can find it in the Rubocop style guide.






              share|improve this answer



















              • 1





                Learned new things from you, thank you very much :)

                – Tiw
                Jan 3 at 16:38
















              3














              File.open(file_name, 'r', &:read)



              Rubocop wants you to use the 'symbol to proc' feature in Ruby instead of defining a complete block. This is purely stylistic, and doesn't affect the code execution. You can find it in the Rubocop style guide.






              share|improve this answer



















              • 1





                Learned new things from you, thank you very much :)

                – Tiw
                Jan 3 at 16:38














              3












              3








              3







              File.open(file_name, 'r', &:read)



              Rubocop wants you to use the 'symbol to proc' feature in Ruby instead of defining a complete block. This is purely stylistic, and doesn't affect the code execution. You can find it in the Rubocop style guide.






              share|improve this answer













              File.open(file_name, 'r', &:read)



              Rubocop wants you to use the 'symbol to proc' feature in Ruby instead of defining a complete block. This is purely stylistic, and doesn't affect the code execution. You can find it in the Rubocop style guide.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 3:43









              TomTom

              1,192718




              1,192718








              • 1





                Learned new things from you, thank you very much :)

                – Tiw
                Jan 3 at 16:38














              • 1





                Learned new things from you, thank you very much :)

                – Tiw
                Jan 3 at 16:38








              1




              1





              Learned new things from you, thank you very much :)

              – Tiw
              Jan 3 at 16:38





              Learned new things from you, thank you very much :)

              – Tiw
              Jan 3 at 16:38











              1














              You can look up the offense in RuboCop's docs, e.g. Style/SymbolProc – it usually shows a "bad" and a "good" example:



              # bad
              something.map { |s| s.upcase }

              # good
              something.map(&:upcase)


              If this doesn't help, you can have RuboCop auto-correct the offense (for cops supporting auto-correction like this one).



              Given a file test.rb:



              # frozen_string_literal: true

              File.open(file_name, 'r') { |file| file.read }


              Run rubocop -a: (the actual output depends on your config)



              $ rubocop -a test.rb
              Inspecting 1 file
              C

              Offenses:

              test.rb:3:27: C: [Corrected] Style/SymbolProc: Pass &:read as an argument to open instead of a block.
              File.open(file_name, 'r') { |file| file.read }
              ^^^^^^^^^^^^^^^^^^^^

              1 file inspected, 1 offense detected, 1 offense corrected


              And test.rb will become:



              # frozen_string_literal: true

              File.open(file_name, 'r', &:read)





              share|improve this answer






























                1














                You can look up the offense in RuboCop's docs, e.g. Style/SymbolProc – it usually shows a "bad" and a "good" example:



                # bad
                something.map { |s| s.upcase }

                # good
                something.map(&:upcase)


                If this doesn't help, you can have RuboCop auto-correct the offense (for cops supporting auto-correction like this one).



                Given a file test.rb:



                # frozen_string_literal: true

                File.open(file_name, 'r') { |file| file.read }


                Run rubocop -a: (the actual output depends on your config)



                $ rubocop -a test.rb
                Inspecting 1 file
                C

                Offenses:

                test.rb:3:27: C: [Corrected] Style/SymbolProc: Pass &:read as an argument to open instead of a block.
                File.open(file_name, 'r') { |file| file.read }
                ^^^^^^^^^^^^^^^^^^^^

                1 file inspected, 1 offense detected, 1 offense corrected


                And test.rb will become:



                # frozen_string_literal: true

                File.open(file_name, 'r', &:read)





                share|improve this answer




























                  1












                  1








                  1







                  You can look up the offense in RuboCop's docs, e.g. Style/SymbolProc – it usually shows a "bad" and a "good" example:



                  # bad
                  something.map { |s| s.upcase }

                  # good
                  something.map(&:upcase)


                  If this doesn't help, you can have RuboCop auto-correct the offense (for cops supporting auto-correction like this one).



                  Given a file test.rb:



                  # frozen_string_literal: true

                  File.open(file_name, 'r') { |file| file.read }


                  Run rubocop -a: (the actual output depends on your config)



                  $ rubocop -a test.rb
                  Inspecting 1 file
                  C

                  Offenses:

                  test.rb:3:27: C: [Corrected] Style/SymbolProc: Pass &:read as an argument to open instead of a block.
                  File.open(file_name, 'r') { |file| file.read }
                  ^^^^^^^^^^^^^^^^^^^^

                  1 file inspected, 1 offense detected, 1 offense corrected


                  And test.rb will become:



                  # frozen_string_literal: true

                  File.open(file_name, 'r', &:read)





                  share|improve this answer















                  You can look up the offense in RuboCop's docs, e.g. Style/SymbolProc – it usually shows a "bad" and a "good" example:



                  # bad
                  something.map { |s| s.upcase }

                  # good
                  something.map(&:upcase)


                  If this doesn't help, you can have RuboCop auto-correct the offense (for cops supporting auto-correction like this one).



                  Given a file test.rb:



                  # frozen_string_literal: true

                  File.open(file_name, 'r') { |file| file.read }


                  Run rubocop -a: (the actual output depends on your config)



                  $ rubocop -a test.rb
                  Inspecting 1 file
                  C

                  Offenses:

                  test.rb:3:27: C: [Corrected] Style/SymbolProc: Pass &:read as an argument to open instead of a block.
                  File.open(file_name, 'r') { |file| file.read }
                  ^^^^^^^^^^^^^^^^^^^^

                  1 file inspected, 1 offense detected, 1 offense corrected


                  And test.rb will become:



                  # frozen_string_literal: true

                  File.open(file_name, 'r', &:read)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 12:42

























                  answered Jan 3 at 12:33









                  StefanStefan

                  77.5k895148




                  77.5k895148






























                      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%2f54015929%2fhow-to-pass-read-as-argument-to-file-open-as-indicated-by-rubocop%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







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory