Comments (4)
Researched more and found out just need prefix _
https://womanonrails.com/ruby-pattern-matching-second-look (point 5)
So this works
# frozen_string_literal: true
require "httpx"
def handle_response(res)
puts res.inspect
case res
in {status: 300...} | {error: _err}
$stdout.puts "hi"
puts "Error: #{_err}" if _err
else
$stdout.puts "bye"
end
end
handle_response(HTTPX.get("https://exampleasdadadadad.org"))
handle_response(HTTPX.get("https://nginx.org"))
handle_response(HTTPX.get("https://reddit.com"))
from rubocop.
Even though it's in {error: error}
that's highlighted, what the cop reports here is that you have $stdout.puts "hi"
in two branches. The problem goes away if you change one of them to $stdout.puts "hello"
for example.
from rubocop.
What I want to achieve is to $stdout.puts "hi"
for both {status: 300...}
& {error: error}
How should that be done?
from rubocop.
Warning: I'm not an expert in these matters. I've never used the pattern matching case
..in
construct myself and had to look it up.
Anyway, it seems like you can list several patterns by putting a |
delimiter between them. It is, however, a syntax error to use variables like error
in such expressions as it would not have a defined value in case of not matching. So:
def handle(res)
case res
in {status: 300...} | {error: _}
$stdout.puts "hi", res
else
$stdout.puts "bye"
end
end
handle(status: 300)
handle(error: 'Bad gateway')
will output
hi
{:status=>300}
hi
{:error=>"Bad gateway"}
Source: https://www.toptal.com/ruby/ruby-pattern-matching-tutorial
from rubocop.
Related Issues (20)
- Regression in v.1.63.3 HOT 6
- False positive `Lint/Syntax` with ruby 3.3.1 and anonymous rest arg/keyword argument forwarding HOT 2
- False positive for `Layout/HeredocIndentation` with `parser_prism` HOT 2
- False positive for `Layout/LineEndStringConcatenationIndentation` with Prism
- `Lint/ShadowingOuterLocalVariable` does not take into account `if`/`else` branches HOT 1
- `class Foo; bar do` will crash Metrics/BlockLength HOT 2
- `Style/ArgumentsForwarding` enforce anonymous bad autocorrect
- regression in 1.63.4 -- "Could not locate Gemfile" HOT 2
- False Positive for `Lint/ImplicitStringConcatenation` with prism HOT 1
- `Style/HashEachMethods` mistakenly reported for zipped arrays HOT 2
- New cops warning still show up even after they have been configured HOT 1
- `cop.documentation_url` is nil for extension cops
- Question about Lint/ToEnumArguments HOT 1
- Metrics/AbcSize with `auto-gen-only-exclude` option
- Unexpected change to Style/Copyright from #12804 HOT 4
- uninitialized constant Bundler::SharedHelpers (NameError) HOT 16
- `Style/MapIntoArray` misinterprets my class design HOT 3
- Rubocop::Config#gem_version_to_major_minor_float doesn't work for Rails 8.0
- Cop idea: Detect `map` where result is discarded HOT 2
- "too many arguments for format string" warning HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rubocop.