stefkin / pattern-match-ruby

This project is to try Pattern Matching in Ruby. It's a conceptual model.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pattern match in Ruby

About this project

This project is to try Pattern Matching in Ruby. It's a conceptual model, and has many arguments about specification of pattern match. Welcome to your feed back via issues or PR.

How to build?

Before trying this feature, you need build customized Ruby interpreter. See https://github.com/ruby/ruby/ to build and install a ruby interpreter.

For Japanese language users MRI のビルド、およびインストール will help you.

How to try?

Basic: use %p to write pattern and match operator is =~.

if %p([:ok, x]) =~ [:ok, 200]
   p "Seconnd Element is #{x}"
else
   p "Not Match!"
end

You can also use === as the alias of =~.

res = [:ng, 500]

case res
when %p([:ok, status])
  p "You got! status: #{status}"
when %p([:ng, status])
  p "Nooo! status: #{status}"
else
  raise "unexpected response!"
end

%p also allows hash. _ is ignored.

user = { name: 'yuki', from: 'Fukuoka' }

case user
when %p({name: name, from: 'Hiroshima'})
  p "#{name} is a local people. Thank you!"
when %p({name: name, from: _})
  p "#{name} is a visitor. welcome!"
end

You can write hash in array, and array as value of hash.

if %p( [x, :y, { "array" => [5, v]}] ) =~ [1, :y, { "array" => [5, 'a, b']}]
  p x
  p v
end

%p allows regexp too.

language = { name: 'pm-ruby', version: "development" }

case language
when %p({ name: /^pm-.*/, version: version })
  p "This pattern-match's version is #{version}"
when %p({ name: name, version: version })
  p "This #{name}'s version is #{version}"
end

and _ + class name

obj = %w(a b c)

case obj
when %p(_String)
  p obj
when %p(_Array)
  p obj.join(',')
end

About

This project is to try Pattern Matching in Ruby. It's a conceptual model.

License:Other


Languages

Language:Ruby 64.9%Language:C 32.9%Language:Yacc 0.7%Language:Makefile 0.5%Language:Emacs Lisp 0.3%Language:C++ 0.2%Language:GDB 0.1%Language:Ragel 0.1%Language:HTML 0.1%Language:JavaScript 0.0%Language:CSS 0.0%Language:XSLT 0.0%Language:Batchfile 0.0%Language:Shell 0.0%Language:Python 0.0%Language:M4 0.0%Language:Assembly 0.0%Language:Scilab 0.0%Language:Scheme 0.0%Language:Perl 6 0.0%Language:Perl 0.0%