Reg version 0.4.8 has been released!
Reg is a library for pattern matching in ruby data structures. Reg
provides
Regexp-like match and match-and-replace for all data structures
(particularly
Arrays, Objects, and Hashes), not just Strings.
Reg is best thought of in analogy to regular expressions; Regexps are
special
data structures for matching Strings; Regs are special data structures
for
matching ANY type of ruby data (Strings included, using Regexps).
== Installation:
Type this command to install the gem:
gem install reg
Or, download the tarball from rubyforge:
http://rubyforge.org/frs/download.php/68391/reg-0.4.8.tar.gz
For more complete documentation, see:
== Examples:
“… the effect of drinking a Pan Galactic Gargle Blaster is like
having
your brains smashed out by a slice of lemon wrapped round a large gold
brick.”
– Douglas Adams, Hitchhiker’s_Guide_to_the_Galaxy
Reg is kind of hard to bend your brain around, so here are some
examples:
Matches array containing exactly 2 elements; 1st is another array, 2nd
is
integer:
+[Array,Integer]
Like above, but 1st is array of arrays of symbol
+[+[+[Symbol+0]+0],Integer]
Matches array of at least 3 consecutive symbols and nothing else:
+[Symbol+3]
Matches array with at least 3 symbols in it somewhere:
+[OBS, Symbol+3, OBS]
Matches array of at most 6 strings starting with ‘g’
+[/^g/-6] #no .reg necessary for regexp
Matches array of between 5 and 9 hashes containing a key :k pointing to
something non-nil:
+[ +{:k=>~nil.reg}*(5…9) ]
Matches an object with Integer instance variable @k and property (ie
method)
foobar that returns a string with ‘baz’ somewhere in it:
-{:@k=>Integer, :foobar=>/baz/}
Matches array of 6 hashes with 6 as a value of every key, followed by
18 objects with an attribute @s which is a String:
+[ +{OB=>6}*6, -{:@s=>String}*18 ]
Matches a single item whose method ‘length’ returns a Fixnum:
item_that.length.is_a? Fixnum
Here’s an example of a Reg::Knows matcher, which matches objects that
have the
#slice method:
-:slice
== Changes:
0.4.8 / 21dec2009
- 2 Major Bugfixes:
- ported to ruby 1.9
- dropped Cursor dependancy in favor of Sequence
- 2 Minor Bugfixes:
- fixed empty array matcher matching non-empty arrays
- actually handle the case of non-present rubygems.rb correctly