This project is to try Pattern Matching in Ruby. It's a conceptual model.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
basictest
benchmark
bin
bootstraptest
ccan
coverage
cygwin
defs
doc
enc
ext
gems
include
lib
man
misc
missing
nacl
pm_samples
sample
spec
template
test
tool
win32
.document
.editorconfig
.gdbinit
.gitattributes
.gitignore
.indent.pro
.travis.yml
BSDL
CONTRIBUTING.md
COPYING
COPYING.ja
GPL
KNOWNBUGS.rb
LEGAL
Makefile.in
NEWS
README.EXT
README.EXT.ja
README.ja.md
README.md
acinclude.m4
aclocal.m4
addr2line.c
addr2line.h
appveyor.yml
array.c
bignum.c
class.c
common.mk
compar.c
compile.c
complex.c
configure.in
constant.h
cont.c
debug.c
debug_counter.c
debug_counter.h
dir.c
dln.c
dln.h
dln_find.c
dmydln.c
dmyenc.c
dmyext.c
encindex.h
encoding.c
enum.c
enumerator.c
error.c
eval.c
eval_error.c
eval_intern.h
eval_jump.c
file.c
gc.c
gc.h
gem_prelude.rb
golf_prelude.rb
goruby.c
hash.c
ia64.s
id_table.c
id_table.h
inits.c
insns.def
internal.h
io.c
iseq.c
iseq.h
lex.c.blt
load.c
loadpath.c
localeinit.c
main.c
marshal.c
math.c
method.h
miniinit.c
node.c
node.h
numeric.c
object.c
pack.c
parse.y
prelude.rb
probes.d
probes_helper.h
proc.c
process.c
random.c
range.c
rational.c
re.c
regcomp.c
regenc.c
regenc.h
regerror.c
regexec.c
regint.h
regparse.c
regparse.h
regsyntax.c
ruby-runner.c
ruby.c
ruby_assert.h
ruby_atomic.h
rubystub.c
safe.c
signal.c
siphash.c
siphash.h
sparc.c
sprintf.c
st.c
strftime.c
string.c
struct.c
symbol.c
symbol.h
thread.c
thread_pthread.c
thread_pthread.h
thread_sync.c
thread_win32.c
thread_win32.h
time.c
timev.h
transcode.c
transcode_data.h
util.c
variable.c
version.c
version.h
vm.c
vm_args.c
vm_backtrace.c
vm_core.h
vm_debug.h
vm_dump.c
vm_eval.c
vm_exec.c
vm_exec.h
vm_insnhelper.c
vm_insnhelper.h
vm_method.c
vm_opts.h
vm_trace.c
vsnprintf.c

README.md

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