Giter Club home page Giter Club logo

Comments (4)

PikachuEXE avatar PikachuEXE commented on June 1, 2024 1

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.

jonas054 avatar jonas054 commented on June 1, 2024

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.

PikachuEXE avatar PikachuEXE commented on June 1, 2024

What I want to achieve is to $stdout.puts "hi" for both {status: 300...} & {error: error}
How should that be done?

from rubocop.

jonas054 avatar jonas054 commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.