Ruby 3.0 đã chính thức hỗ trợ Pattern Matching

Pattern Matching trong Ruby cho phép giải cấu trúc dữ liệu một cách ngắn gọn, giúp dễ dàng gán giá trị cho các biến với cú pháp rõ ràng. Pattern matching là một tính năng đã được giới thiệu trong Ruby 2.7. Từ Ruby 3.0 trở đi, nó không còn là một tính năng thử nghiệm nữa và chúng ta có thể bắt đầu sử dụng nó một cách chính thức.

Warning Ruby 2.7: Pattern matching is experimental, and the behavior may change in future versions of Ruby!

Pattern Matching là gì?

Pattern matching là một tính năng cho phép bạn so sánh và hiểu cấu trúc dữ liệu. Điều này được thực hiện bằng cách kiểm tra cách thông tin được tổ chức và gán các phần tương ứng cho các biến cục bộ để sử dụng sau này.

Pattern Matching được hỗ trợ thông qua cú pháp case / in. Quan trọng là không nhầm lẫn với case / when và không kết hợp chúng. Nếu không có sự phù hợp với bất kỳ biểu thức nào và không có định nghĩa else, thì một ngoại lệ NoMatchingPatternError sẽ được raise

case <expression>
in <pattern1>
  # ...
in <pattern2>
  # ...
else
  # ...
end

Pattern có thể là:

  • Value: Any Ruby object (compared with the === operator, as in ‘when’).
  • Array: Array pattern: [<subpattern>, <subpattern>, <subpattern>, <subpattern>, ...].
  • Find: Search pattern: [*variable, <subpattern>, <subpattern>, <subpattern>, <subpattern>, ..., *variable].
  • Hash: Hash pattern: {key: <subpattern>, key: <subpattern>, ...}.
  • Alternative: Pattern combination with | (vertical bar).
  • Variable capture<pattern> => variable or variable.

Định nghĩa Pattern Matching

# Define a method that uses pattern matching with case/in
def process_data(data)
  case data
  in { type: "number", value: Integer => num }
    puts "Received a number: #{num}"
  in { type: "string", value: String => str }
    puts "Received a string: #{str}"
  in { type: "array", value: Array => arr }
    puts "Received an array: #{arr}"
  in { type: "hash", value: Hash => hash }
    puts "Received a hash: #{hash}"
  else
    puts "Received something else."
  end
end

# Test the method with different data structures
process_data({ type: "number", value: 42 })               # Output: Received a number: 42
process_data({ type: "string", value: "Hello, Ruby!" })   # Output: Received a string: Hello, Ruby!
process_data({ type: "array", value: [1, 2, 3] })         # Output: Received an array: [1, 2, 3]
process_data({ type: "hash", value: { key: "value" } })   # Output: Received a hash: {:key=>"value"}
process_data({ type: "unknown", value: "unknown data" })  # Output: Received something else.

Deconstruct và Deconstruct_keys

Đây là hai phương thức đặc biệt trong pattern matching: deconstruct, được gọi khi đánh giá trên một Array, và deconstruct_keys được gọi khi đánh giá trên một Hash. Hãy xem một ví dụ:

class Coordinates
  attr_accessor :x, :y

  def initialize(x, y)
    @x = x
    @y = y
  end

  def deconstruct
    [@x, @y]
  end

  def deconstruct_key
    {x: @x, y: @y}
  end
end

Khi một thể hiện của lớp Coordinate được đánh giá trên một Array

c = Coordinates.new(32,50)

case c
in [a,b]
  p a #=> 32
  p b #=> 50
end

Khi một thể hiện của lớp Coordinate được đánh giá trên một Hash

case c
in {x:, y:}
  p x #=> 32
  p y #=> 50
end

Nếu class không có 2 methods trên thì sẽ báo lỗi

#<Coordinate:0x000000010d27fb10 @x=32, @y=50>: #<Coordinate:0x000000010d27fb10 @x=32, @y=50> does not respond to #deconstruct (NoMatchingPatternError)

Tham khảo thêm tại: https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html

0 Shares:
10 comments
  1. Ticketleanamongst.hb.cn sexFlowser ljfe sex1994 dodge ram tranny oilLeazther bondage suit2007 moie sexx annd
    deafh 101Gaay male rwpairmen studsBlack haiey slendeer pussyAnghelina
    joloie cumshot gameBestt of dolrm voyeurForeskin pic frewe gayNakedd pis off jamie gertzSexxo een panamja
    y escortsPibeapple fcial homemadeFreee fulllnght medi player seex videosVaagina cumshit
    pumpersSough asian tigerMy dauterr is fuckingJetsin sex bby jabHackdd phonne
    pornCharliue daniels transvestite barGauge gangbang girlWatcching boyfrriend suyck cockBabee
    bikini poolFial examinatin seex sceneNatuiral titfs teeen galleriesHow lng is
    tthe wolds ongest penisNorway aal redtubesGirls pisss there pantsSurgery
    hughe boob sitesNj sexx offender internett registryMatture womern posesArtistioc vagina photosAdult passpoirt applicaton iin canadaNinna hazrtley sjcking cockParkinson’s efect on sexjal performanceForceed aanal oon a deskHandjob her sweet hnd clipSexy girll chevby truckLowwer stgomach paqin oon right sice bby vaginaGivee mme blolw jobb
    momPanyie peee pissingCameron hore free nuee picsAnaa sucksSexyy
    black femake on spring beeak bigg bootyGorgeus redheead picturesGirl phot
    vintageNeww ground pornKennneth cole bikiniAdul colege graduatess livving att homeThhe
    itches oof breastwik seex scenesBesst breastt cncer specialistEroti pajamaDominwtion oof argentinaDeeep friedd chicen strips recipiesAnume movje shemaleFreee italiann hared sexCoouple fjcking
    gallery teenTigut youhng ass xxxBllack guyy bottle snmort cumReal mother-in-law
    sexAdulpt commpletely datjng ree siteLijsey llohand pornFreee gay muscle pleasureAdult
    educxation mcnairy coo tnWhy doo sperms change colorsSexx oon thhe beah profewssional picturesCornwall sexua abuseHeidi montag ppink bikiniTentijcle henhtai clipsJosefi ljungstrom pornSexx playiing cardsIsqbella bones fuckedBeautiful porn moviesPussyy lips spreaqd videosMakiong
    a hoke porno movieNudde sexy hotgirlsJapanesee cream piie
    sexDate chec escortDylazn daiels pornSalphic lesbian sexPlqnts maturde purchhase brassicaHott
    teen gjrls pussyHc tgpReedtube bigg ppussy orgasmGayy
    malee nude pictures armpits freeSexxy lesbiaan with biig boobsMen in lingeriie blogSexx offendefs
    jctFreee photos of connie brtittain nudeNyc nnaked gatheringsNew ersey
    sexual offenders dbExtreme amature pornCrotchless bikinisBig tited
    nude modelsBbww delicious oon demandTerra gerard nude ofvd9wuaptg5w0v0qd49

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Cài đặt Rails 7 với Vite + Stimulus + Tailwind
Read More

Cài đặt Rails 7 với Vite + Stimulus + Tailwind

Nếu bạn đang tìm kiếm một giải pháp phát triển web hiệu suất cao và tiện lợi, Vite sẽ là lựa chọn đáng xem xét. Còn với Rails 7, bạn sẽ phải cân nhắc xem tính năng importmap có đủ thuyết phục để bạn chuyển đổi không.
Read More

Nâng bao nhiêu pool connect database là phù hợp

Trong bài viết này chúng ta sẽ tìm hiểu về sự quan trọng của việc tối ưu hóa kết nối database và tìm cách đảm bảo rằng bạn đang sử dụng số lượng pool kết nối đúng đắn, tiết kiệm tài nguyên và đảm bảo hiệu suất hệ thống.
Read More

Building a GraphQL API in Rails

Table of Contents Hide GraphQL là gì?Thiết Lập Dự Án RailsThêm DependenciesTạo Schema GraphQLĐịnh Nghĩa Các LoạiKiểm Thử…