Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

object pattern matching

3 views
Skip to first unread message

Ara.T.Howard

unread,
Aug 20, 2005, 12:21:19 PM8/20/05
to

i've been toying with an idea lately and thinking about how one would crawl an
object graph to determine if it matched a specifc object. for instance


array_of_hashes = [ {}, {} ]

specific_array_of_hashes = [ {'key' => 'value}, {:k => :v} ]
specific_array_of_hashes_2 = [ {:k => :v} ]

specific_array_of_hashes.is_shaped_like? array_of_hashes #=> true

so, something like pattern matching in ml languages. any concepts on how to
do that in ruby for arbitrary objects?

cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================

James Britt

unread,
Aug 20, 2005, 12:50:07 PM8/20/05
to
Ara.T.Howard wrote:
>
> i've been toying with an idea lately and thinking about how one would
> crawl an
> object graph to determine if it matched a specifc object. for instance
>
>
> array_of_hashes = [ {}, {} ]
>
> specific_array_of_hashes = [ {'key' => 'value}, {:k => :v} ]
> specific_array_of_hashes_2 = [ {:k => :v} ]
>
> specific_array_of_hashes.is_shaped_like? array_of_hashes #=> true
>
> so, something like pattern matching in ml languages. any concepts on
> how to
> do that in ruby for arbitrary objects?
>

Isn't there a Ruby project that allows one to run regexen over object
hierarchies?

James


--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys


Ara.T.Howard

unread,
Aug 20, 2005, 1:12:33 PM8/20/05
to
On Sun, 21 Aug 2005, James Britt wrote:

> Ara.T.Howard wrote:
>>
>> i've been toying with an idea lately and thinking about how one would crawl
>> an
>> object graph to determine if it matched a specifc object. for instance
>>
>>
>> array_of_hashes = [ {}, {} ]
>>
>> specific_array_of_hashes = [ {'key' => 'value}, {:k => :v} ]
>> specific_array_of_hashes_2 = [ {:k => :v} ]
>>
>> specific_array_of_hashes.is_shaped_like? array_of_hashes #=> true
>>
>> so, something like pattern matching in ml languages. any concepts on how
>> to
>> do that in ruby for arbitrary objects?
>>
>
> Isn't there a Ruby project that allows one to run regexen over object
> hierarchies?

dunno... i seem to recall something too but searching the raa didn't bring up
any hits?

James Britt

unread,
Aug 20, 2005, 1:44:04 PM8/20/05
to
Ara.T.Howard wrote:
> On Sun, 21 Aug 2005, James Britt wrote:
>
>> ...

>>
>> Isn't there a Ruby project that allows one to run regexen over object
>> hierarchies?
>
>
> dunno... i seem to recall something too but searching the raa didn't
> bring up
> any hits?
>

I was selling some folks on Ruby recently, and mentioned this project,
and I swore I had saved off the initial announcement because it struck
me as one of those things that did solve any immediate problems but
seemed so slick that I knew it would come in handy.


Now I can't find it. :(

Phil Tomson

unread,
Aug 20, 2005, 1:51:51 PM8/20/05
to
In article <43076BDE...@neurogami.com>,

James Britt <jam...@neurogami.com> wrote:
>Ara.T.Howard wrote:
>> On Sun, 21 Aug 2005, James Britt wrote:
>>
>>> ...
>>>
>>> Isn't there a Ruby project that allows one to run regexen over object
>>> hierarchies?
>>
>>
>> dunno... i seem to recall something too but searching the raa didn't
>> bring up
>> any hits?
>>
>
>I was selling some folks on Ruby recently, and mentioned this project,
>and I swore I had saved off the initial announcement because it struck
>me as one of those things that did solve any immediate problems but
>seemed so slick that I knew it would come in handy.
>
>
>Now I can't find it. :(
>
>

I think you're looking for Reg:

http://rubyforge.org/projects/reg/

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.

Phil

James Britt

unread,
Aug 20, 2005, 2:32:06 PM8/20/05
to
Phil Tomson wrote:
..

>
> I think you're looking for Reg:
>
> http://rubyforge.org/projects/reg/
>
> 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.


Yes! Thanks.

James


Ara.T.Howard

unread,
Aug 20, 2005, 2:48:02 PM8/20/05
to

thanks phil! downloading now...

Eric Mahurin

unread,
Aug 20, 2005, 3:28:12 PM8/20/05
to
--- "Ara.T.Howard" <Ara.T....@noaa.gov> wrote:

From my communications with Caleb, I believe he'll be changing
the interface to look more like Regexp. This will help in
places where you have ducked-type places that deal with
Regexp-like objects. An example is some of the scan_pattern*
methods I'll be adding to Cursor (not released yet). I think
he might add the ability to operate on a Cursor directly also,
but I'm not sure about that. A Cursor by the way is something
generic that can look kind of like a Array, String, IO, C++
iterator, Java stream, etc. all rolled into one API. I have
many derived classes that operate on a variety of sequential
data structures.

In addition I'm putting together Grammar which will also be
able to operate on any sequential data structure - as long as
you can give it a subset of the Cursor API. You'll be able to
write a lexer (operates on characters) and a parser (operates
on tokens) in a uniform way. Similar to ANTLR, but more
unification.


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


Phil Tomson

unread,
Aug 20, 2005, 4:54:57 PM8/20/05
to
In article <2005082019280...@web36115.mail.mud.yahoo.com>,

Eric Mahurin <eric_m...@yahoo.com> wrote:
>--- "Ara.T.Howard" <Ara.T....@noaa.gov> wrote:
>
>> On Sun, 21 Aug 2005, Phil Tomson wrote:
>>=20

We eagerly await the release of Grammar.

Phil

0 new messages