object pattern matching

A

Ara.T.Howard

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
===============================================================================
 
J

James Britt

Ara.T.Howard said:
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
 
A

Ara.T.Howard

Ara.T.Howard said:
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?

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
===============================================================================
 
J

James Britt

Ara.T.Howard said:
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. :(


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
 
P

Phil Tomson

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
 
A

Ara.T.Howard

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

thanks phil! downloading now...

-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
===============================================================================
 
E

Eric Mahurin

--- "Ara.T.Howard said:
On Sun, 21 Aug 2005, Phil Tomson wrote:
=20
Strings.

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=20
http://mail.yahoo.com=20
 
P

Phil Tomson

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.

We eagerly await the release of Grammar.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top