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;
}
I have this code
File.open(file_name, 'r') { |file| file.read }
but Rubocop is warning:
Offenses:
Style/SymbolProc: Pass
&:read
as argument toopen
instead of a
block.
How do you do this?
ruby rubocop
add a comment |
I have this code
File.open(file_name, 'r') { |file| file.read }
but Rubocop is warning:
Offenses:
Style/SymbolProc: Pass
&:read
as argument toopen
instead of a
block.
How do you do this?
ruby rubocop
2
You would like to read stackoverflow.com/a/9429972/10522579
– ray
Jan 3 at 7:07
1
You could just doFile.read(file_name)
.
– Kimmo Lehto
Jan 3 at 9:27
add a comment |
I have this code
File.open(file_name, 'r') { |file| file.read }
but Rubocop is warning:
Offenses:
Style/SymbolProc: Pass
&:read
as argument toopen
instead of a
block.
How do you do this?
ruby rubocop
I have this code
File.open(file_name, 'r') { |file| file.read }
but Rubocop is warning:
Offenses:
Style/SymbolProc: Pass
&:read
as argument toopen
instead of a
block.
How do you do this?
ruby rubocop
ruby rubocop
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 doFile.read(file_name)
.
– Kimmo Lehto
Jan 3 at 9:27
add a comment |
2
You would like to read stackoverflow.com/a/9429972/10522579
– ray
Jan 3 at 7:07
1
You could just doFile.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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
1
Learned new things from you, thank you very much :)
– Tiw
Jan 3 at 16:38
add a comment |
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)
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%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
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 3 at 12:02
Stefan
77.5k895148
77.5k895148
answered Jan 3 at 3:41
Cary SwovelandCary Swoveland
71k54167
71k54167
add a comment |
add a comment |
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.
1
Learned new things from you, thank you very much :)
– Tiw
Jan 3 at 16:38
add a comment |
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.
1
Learned new things from you, thank you very much :)
– Tiw
Jan 3 at 16:38
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Jan 3 at 12:42
answered Jan 3 at 12:33
StefanStefan
77.5k895148
77.5k895148
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%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
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
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