Skip to content

Commit 3265f09

Browse files
Prefer filter_map and map+grep instead of map+compact and select+map (#1094)
* Prefer filter_map in completion * Prefer filter_map in test_irb * Prefer filter_map in inspector * map with grep is more readable * map with grep is more readable
1 parent 6a519e9 commit 3265f09

3 files changed

Lines changed: 9 additions & 13 deletions

File tree

lib/irb/completion.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def doc_namespace(preposing, matched, postposing, bind:)
4040

4141
def retrieve_gem_and_system_load_path
4242
candidates = (GEM_PATHS | $LOAD_PATH)
43-
candidates.map do |p|
43+
candidates.filter_map do |p|
4444
if p.respond_to?(:to_path)
4545
p.to_path
4646
else
4747
String(p) rescue nil
4848
end
49-
end.compact.sort
49+
end.sort
5050
end
5151

5252
def retrieve_files_to_require_from_load_path
@@ -171,16 +171,12 @@ def complete_require_path(target, preposing, postposing)
171171

172172
case tok.tok
173173
when 'require'
174-
retrieve_files_to_require_from_load_path.select { |path|
175-
path.start_with?(actual_target)
176-
}.map { |path|
177-
quote + path
174+
retrieve_files_to_require_from_load_path.filter_map { |path|
175+
quote + path if path.start_with?(actual_target)
178176
}
179177
when 'require_relative'
180-
retrieve_files_to_require_relative_from_current_dir.select { |path|
181-
path.start_with?(actual_target)
182-
}.map { |path|
183-
quote + path
178+
retrieve_files_to_require_relative_from_current_dir.filter_map { |path|
179+
quote + path if path.start_with?(actual_target)
184180
}
185181
end
186182
end

lib/irb/inspector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class << self
4646
# Determines the inspector to use where +inspector+ is one of the keys passed
4747
# during inspector definition.
4848
def keys_with_inspector(inspector)
49-
INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k}
49+
INSPECTORS.filter_map {|k, v| k if v == inspector}
5050
end
5151

5252
# Example

test/irb/test_irb.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ def bar
877877
end
878878

879879
assert_match(/irbtest-.*\.rb:2:in (`|'Object#)foo': error \(RuntimeError\)/, output)
880-
frame_traces = output.split("\n").select { |line| line.strip.match?(/from /) }.map(&:strip)
880+
frame_traces = output.split("\n").map(&:strip).grep(/from /)
881881

882882
expected_traces = if RUBY_VERSION >= "3.3.0"
883883
[
@@ -932,7 +932,7 @@ def bar
932932
end
933933

934934
assert_match(/irbtest-.*\.rb:2:in (`|'Object#)foo': error \(RuntimeError\)/, output)
935-
frame_traces = output.split("\n").select { |line| line.strip.match?(/from /) }.map(&:strip)
935+
frame_traces = output.split("\n").map(&:strip).grep(/from /)
936936

937937
expected_traces = [
938938
/from .*\/irbtest-.*.rb:6:in (`|'Object#)bar'/,

0 commit comments

Comments
 (0)