Ruby Data Structure Query Abstractions/Patterns

Discussion in 'Ruby' started by Nicholas Van Weerdenburg, Jan 5, 2005.

  1. Hi all,

    I've written a small ruby application that uses YAML and the
    associated hash/array Ruby structure.

    I am now looking to do some query-like actions on the basic ruby data
    structure. I'm wondering if there are any packages of
    query-abstractions that make sense for Ruby in a generalized sort-of
    way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.

    Currently I've written my structure-parsing code with a few of my
    cases, and I'm starting to refactor with blocks and lambdas in order
    to keep duplication low and offer an easily configurable higher-level
    of abstraction. Now the code is starting to pressure me to create a
    simple query language. Maybe I should go to a ruby db or sqlite, but I
    expect I ultimately only need the most basic functionality.

    Any suggestions or recommendations?

    Thanks,
    Nick
    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 5, 2005
    #1
    1. Advertising

  2. Nicholas Van Weerdenburg

    PA Guest

    PA, Jan 5, 2005
    #2
    1. Advertising

  3. "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    news:...
    > Hi all,
    >
    > I've written a small ruby application that uses YAML and the
    > associated hash/array Ruby structure.
    >
    > I am now looking to do some query-like actions on the basic ruby data
    > structure. I'm wondering if there are any packages of
    > query-abstractions that make sense for Ruby in a generalized sort-of
    > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.
    >
    > Currently I've written my structure-parsing code with a few of my
    > cases, and I'm starting to refactor with blocks and lambdas in order
    > to keep duplication low and offer an easily configurable higher-level
    > of abstraction. Now the code is starting to pressure me to create a
    > simple query language. Maybe I should go to a ruby db or sqlite, but I
    > expect I ultimately only need the most basic functionality.
    >
    > Any suggestions or recommendations?


    Another thought: maybe you can even use Ruby itself. I mean, one can
    produce quite readable select expressions in Ruby and we have
    Enumerable#all?, #any? etc. You might want to add some functionality for
    recursive traversal though.

    Kind regards

    robert
     
    Robert Klemme, Jan 5, 2005
    #3
  4. Nicholas Van Weerdenburg

    Sam Roberts Guest

    Quoteing , on Thu, Jan 06, 2005 at 03:26:22AM +0900:
    > Hi all,
    >
    > I've written a small ruby application that uses YAML and the
    > associated hash/array Ruby structure.


    > I am now looking to do some query-like actions on the basic ruby data
    > structure. I'm wondering if there are any packages of
    > query-abstractions that make sense for Ruby in a generalized sort-of
    > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.


    This is why you might want to consider XML... then you can just USE
    XPath.

    Sam

    >
    > Currently I've written my structure-parsing code with a few of my
    > cases, and I'm starting to refactor with blocks and lambdas in order
    > to keep duplication low and offer an easily configurable higher-level
    > of abstraction. Now the code is starting to pressure me to create a
    > simple query language. Maybe I should go to a ruby db or sqlite, but I
    > expect I ultimately only need the most basic functionality.
    >
    > Any suggestions or recommendations?
    >
    > Thanks,
    > Nick
    > --
    > Nicholas Van Weerdenburg
    >
     
    Sam Roberts, Jan 6, 2005
    #4
  5. Thanks. Both links are very interesting.

    Regards,
    Nick


    On Thu, 6 Jan 2005 03:41:23 +0900, PA <> wrote:
    >
    > On Jan 05, 2005, at 19:26, Nicholas Van Weerdenburg wrote:
    >
    > > Any suggestions or recommendations?

    >
    > Not quite sure how well this would fit with Ruby, but you may want to
    > take a look at SODA [1] which could be used to query generic object
    > graphs.
    >
    > In addition, you may want to consider EOQualifier for, er,
    > "inspiration":
    >
    > http://developer.apple.com/documentation/WebObjects/Reference/API/com/
    > webobjects/eocontrol/EOQualifier.html
    >
    > Cheers,
    >
    > PA.
    >
    > [1] http://sodaquery.sourceforge.net/
    >
    >



    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 6, 2005
    #5
  6. On Thu, 6 Jan 2005 05:56:31 +0900, Robert Klemme <> wrote:
    >
    > "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    > news:...
    > > Hi all,
    > >
    > > I've written a small ruby application that uses YAML and the
    > > associated hash/array Ruby structure.
    > >
    > > I am now looking to do some query-like actions on the basic ruby data
    > > structure. I'm wondering if there are any packages of
    > > query-abstractions that make sense for Ruby in a generalized sort-of
    > > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.
    > >
    > > Currently I've written my structure-parsing code with a few of my
    > > cases, and I'm starting to refactor with blocks and lambdas in order
    > > to keep duplication low and offer an easily configurable higher-level
    > > of abstraction. Now the code is starting to pressure me to create a
    > > simple query language. Maybe I should go to a ruby db or sqlite, but I
    > > expect I ultimately only need the most basic functionality.
    > >
    > > Any suggestions or recommendations?

    >
    > Another thought: maybe you can even use Ruby itself. I mean, one can
    > produce quite readable select expressions in Ruby and we have
    > Enumerable#all?, #any? etc. You might want to add some functionality for
    > recursive traversal though.
    >
    > Kind regards
    >
    > robert
    >
    >


    I like the idea of using Ruby, and have been extending my code in that
    manner. However, XML becomes interesting soon because of it's DOM
    traversal capabilities.

    I'm also wondering if I'm missing some common Ruby/Perl/Python idiom
    for handling in-memory data-structure traversal in a more abstract
    way, but so far it doesn't look like it.

    Does it make sense to have a general data-structure traversal/query
    api in a language like Ruby?

    Thanks,
    Nick
    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 6, 2005
    #6
  7. On Thu, 6 Jan 2005 10:10:26 +0900, Sam Roberts <> wrote:
    > Quoteing , on Thu, Jan 06, 2005 at 03:26:22AM +0900:
    > > Hi all,
    > >
    > > I've written a small ruby application that uses YAML and the
    > > associated hash/array Ruby structure.

    >
    > > I am now looking to do some query-like actions on the basic ruby data
    > > structure. I'm wondering if there are any packages of
    > > query-abstractions that make sense for Ruby in a generalized sort-of
    > > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.

    >
    > This is why you might want to consider XML... then you can just USE
    > XPath.
    >
    > Sam
    >


    This is looking like the most reasonable choice at the moment- though
    not the most fun, so I may continue with a Ruby solution for now.

    Thanks,
    Nick

    > >
    > > Currently I've written my structure-parsing code with a few of my
    > > cases, and I'm starting to refactor with blocks and lambdas in order
    > > to keep duplication low and offer an easily configurable higher-level
    > > of abstraction. Now the code is starting to pressure me to create a
    > > simple query language. Maybe I should go to a ruby db or sqlite, but I
    > > expect I ultimately only need the most basic functionality.
    > >
    > > Any suggestions or recommendations?
    > >
    > > Thanks,
    > > Nick
    > > --
    > > Nicholas Van Weerdenburg
    > >

    >
    >



    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 6, 2005
    #7
  8. Nicholas Van Weerdenburg

    Sam Roberts Guest

    Quoteing , on Thu, Jan 06, 2005 at 01:20:40PM +0900:
    > On Thu, 6 Jan 2005 10:10:26 +0900, Sam Roberts <> wrote:
    > > Quoteing , on Thu, Jan 06, 2005 at 03:26:22AM +0900:
    > > > Hi all,
    > > >
    > > > I've written a small ruby application that uses YAML and the
    > > > associated hash/array Ruby structure.

    > >
    > > > I am now looking to do some query-like actions on the basic ruby data
    > > > structure. I'm wondering if there are any packages of
    > > > query-abstractions that make sense for Ruby in a generalized sort-of
    > > > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.

    > >
    > > This is why you might want to consider XML... then you can just USE
    > > XPath.
    > >
    > > Sam
    > >

    >
    > This is looking like the most reasonable choice at the moment- though
    > not the most fun, so I may continue with a Ruby solution for now.


    I don't know, what's not fun about XML?

    Rant: for years the world fills up with adhoc 1/2 solutions to data
    structuring, then people decide that reinventing the wheel is dumb, and
    make XML, and start adding tons of useful tools to it, like query
    languages. The folks come along and say 'its too big and complicated"
    and start again... There will soon be a YPath (YAML query language),
    mark my words!

    Ok, I feel better.

    That aside, it sounds kind of cool making a rubyish query language for
    ruby data structures... maybe XPath from Rexml could actually be abused
    for your purpose, or used to implement a similar language?

    Have fun!
    Sam
     
    Sam Roberts, Jan 6, 2005
    #8
  9. PA ha scritto:
    >
    > On Jan 05, 2005, at 19:26, Nicholas Van Weerdenburg wrote:
    >
    >> Any suggestions or recommendations?

    >
    >
    > Not quite sure how well this would fit with Ruby, but you may want to
    > take a look at SODA [1] which could be used to query generic object
    > graphs.
    >
    > In addition, you may want to consider EOQualifier for, er, "inspiration":
    >
    > http://developer.apple.com/documentation/WebObjects/Reference/API/com/
    > webobjects/eocontrol/EOQualifier.html
    >
    > Cheers,
    >
    > PA.
    >
    > [1] http://sodaquery.sourceforge.net/
    >



    also, JXPath allows XPAth to be applied to generic graphs, maybe of interest
     
    gabriele renzi, Jan 6, 2005
    #9
  10. "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    news:...
    > On Thu, 6 Jan 2005 05:56:31 +0900, Robert Klemme <> wrote:
    >>
    >> "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    >> news:...
    >> > Hi all,
    >> >
    >> > I've written a small ruby application that uses YAML and the
    >> > associated hash/array Ruby structure.
    >> >
    >> > I am now looking to do some query-like actions on the basic ruby data
    >> > structure. I'm wondering if there are any packages of
    >> > query-abstractions that make sense for Ruby in a generalized sort-of
    >> > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.
    >> >
    >> > Currently I've written my structure-parsing code with a few of my
    >> > cases, and I'm starting to refactor with blocks and lambdas in order
    >> > to keep duplication low and offer an easily configurable higher-level
    >> > of abstraction. Now the code is starting to pressure me to create a
    >> > simple query language. Maybe I should go to a ruby db or sqlite, but I
    >> > expect I ultimately only need the most basic functionality.
    >> >
    >> > Any suggestions or recommendations?

    >>
    >> Another thought: maybe you can even use Ruby itself. I mean, one can
    >> produce quite readable select expressions in Ruby and we have
    >> Enumerable#all?, #any? etc. You might want to add some functionality for
    >> recursive traversal though.
    >>
    >> Kind regards
    >>
    >> robert
    >>
    >>

    >
    > I like the idea of using Ruby, and have been extending my code in that
    > manner. However, XML becomes interesting soon because of it's DOM
    > traversal capabilities.
    >
    > I'm also wondering if I'm missing some common Ruby/Perl/Python idiom
    > for handling in-memory data-structure traversal in a more abstract
    > way, but so far it doesn't look like it.
    >
    > Does it make sense to have a general data-structure traversal/query
    > api in a language like Ruby?


    I'd say yes, but maybe only for certain types (DOM tree nodes) - not
    necessarily for general Hash, Array and the like. OTOH you can cook up
    something quite easily though:

    class Object
    def traverse() yield self end
    end

    module Enumerable
    def traverse(&b)
    each {|o| o.traverse(&b)}
    end
    end

    class Hash
    def traverse(&b)
    each {|k,v| v.traverse(&b)}
    end
    end

    Note that this does not take care of looks in the object graph. To do that,
    you need to do a bit more:

    class Object
    def traverse(context = {}) context[self.id] ||= (yield self; true) end
    end

    module Enumerable
    def traverse(context = {}, &b)
    unless context[self.id]
    context[self.id] = true
    each {|o| o.traverse(context, &b)}
    end
    end
    end

    class Hash
    def traverse(context = {}, &b)
    unless context[self.id]
    context[self.id] = true
    each {|k,v| v.traverse(context, &b)}
    end
    end
    end

    Kind regards

    robert
     
    Robert Klemme, Jan 6, 2005
    #10
  11. Nicholas Van Weerdenburg

    Sam Roberts Guest

    Quoteing , on Thu, Jan 06, 2005 at 02:46:30PM +0900:
    >
    > "Sam Roberts" <> wrote
    >
    > > I don't know, what's not fun about XML?

    >
    > Since you did not include any smileys ...
    >
    > (1) Having a uniform and extensible concrete syntax for tree structures is
    > great
    > (2) Having a whole set of uniform manipulations on (1) is great
    > (3) Insisting on applying (1) to the concrete syntax of (2) is absurd and
    > the result, ugly


    I don't know what (3) is talking about. The syntax of XPath isn't XML.
    It's a domain-specific language inside XML, much as regexs are a
    domain-specific language inside ruby.

    > Just about every (computer) language has a natural underlying tree
    > structure, with lots of cross-tree links. So, should we now start to write
    > our Ruby using XML ?


    No, nor YAML, has anybody suggested doing so?

    Sam
     
    Sam Roberts, Jan 6, 2005
    #11
  12. `On Thu, 6 Jan 2005 21:36:30 +0900, Robert Klemme <> wrote:
    >
    > "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    > news:...
    > > On Thu, 6 Jan 2005 05:56:31 +0900, Robert Klemme <> wrote:
    > >>
    > >> "Nicholas Van Weerdenburg" <> schrieb im Newsbeitrag
    > >> news:...
    > >> > Hi all,
    > >> >
    > >> > I've written a small ruby application that uses YAML and the
    > >> > associated hash/array Ruby structure.
    > >> >
    > >> > I am now looking to do some query-like actions on the basic ruby data
    > >> > structure. I'm wondering if there are any packages of
    > >> > query-abstractions that make sense for Ruby in a generalized sort-of
    > >> > way. E.g. XPath and XQuery for XML, SQL for RDMS, etc.
    > >> >
    > >> > Currently I've written my structure-parsing code with a few of my
    > >> > cases, and I'm starting to refactor with blocks and lambdas in order
    > >> > to keep duplication low and offer an easily configurable higher-level
    > >> > of abstraction. Now the code is starting to pressure me to create a
    > >> > simple query language. Maybe I should go to a ruby db or sqlite, but I
    > >> > expect I ultimately only need the most basic functionality.
    > >> >
    > >> > Any suggestions or recommendations?
    > >>
    > >> Another thought: maybe you can even use Ruby itself. I mean, one can
    > >> produce quite readable select expressions in Ruby and we have
    > >> Enumerable#all?, #any? etc. You might want to add some functionality for
    > >> recursive traversal though.
    > >>
    > >> Kind regards
    > >>
    > >> robert
    > >>
    > >>

    > >
    > > I like the idea of using Ruby, and have been extending my code in that
    > > manner. However, XML becomes interesting soon because of it's DOM
    > > traversal capabilities.
    > >
    > > I'm also wondering if I'm missing some common Ruby/Perl/Python idiom
    > > for handling in-memory data-structure traversal in a more abstract
    > > way, but so far it doesn't look like it.
    > >
    > > Does it make sense to have a general data-structure traversal/query
    > > api in a language like Ruby?

    >
    > I'd say yes, but maybe only for certain types (DOM tree nodes) - not
    > necessarily for general Hash, Array and the like. OTOH you can cook up
    > something quite easily though:
    >
    > class Object
    > def traverse() yield self end
    > end
    >
    > module Enumerable
    > def traverse(&b)
    > each {|o| o.traverse(&b)}
    > end
    > end
    >
    > class Hash
    > def traverse(&b)
    > each {|k,v| v.traverse(&b)}
    > end
    > end
    >
    > Note that this does not take care of looks in the object graph. To do that,
    > you need to do a bit more:
    >
    > class Object
    > def traverse(context = {}) context[self.id] ||= (yield self; true) end
    > end
    >
    > module Enumerable
    > def traverse(context = {}, &b)
    > unless context[self.id]
    > context[self.id] = true
    > each {|o| o.traverse(context, &b)}
    > end
    > end
    > end
    >
    > class Hash
    > def traverse(context = {}, &b)
    > unless context[self.id]
    > context[self.id] = true
    > each {|k,v| v.traverse(context, &b)}
    > end
    > end
    > end
    >
    > Kind regards
    >
    > robert
    >
    >


    That looks pretty good.

    Thanks,
    Nick

    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 6, 2005
    #12
  13. On Fri, 7 Jan 2005 00:46:35 +0900, itsme213 <> wrote:
    >
    > "Sam Roberts" <> wrote in message
    > news:...
    > > > (1) Having a uniform and extensible concrete syntax for tree structures

    > is
    > > > great
    > > > (2) Having a whole set of uniform manipulations on (1) is great
    > > > (3) Insisting on applying (1) to the concrete syntax of (2) is absurd

    > and
    > > > the result, ugly

    > >
    > > I don't know what (3) is talking about. The syntax of XPath isn't XML.
    > > It's a domain-specific language inside XML, much as regexs are a
    > > domain-specific language inside ruby.

    >
    > Well, my analogy to writing Ruby was off the mark, but I suppose I was doing
    > my rant to your "what's not to like about XML" rant. Because (3) is
    > definitely something I dislike about the XML (and it sounds like you would
    > agree too).
    >
    >


    I had trouble understanding point 3, but I'm guessing your point was
    that XSLT shouldn't be an XML dialect, but rather a proper language?

    Similar to:
    http://www.martinfowler.com/bliki/MovingAwayFromXslt.html

    Nick
    --
    Nicholas Van Weerdenburg
     
    Nicholas Van Weerdenburg, Jan 6, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Learner
    Replies:
    1
    Views:
    1,023
    Marina Levit [MVP]
    Jan 30, 2006
  2. crichmon
    Replies:
    4
    Views:
    509
    Mabden
    Jul 7, 2004
  3. J.O. Aho
    Replies:
    0
    Views:
    399
    J.O. Aho
    Jul 17, 2009
  4. A
    Replies:
    27
    Views:
    1,675
    Jorgen Grahn
    Apr 17, 2011
  5. Alpha Blue
    Replies:
    3
    Views:
    423
    Alpha Blue
    Jul 28, 2009
Loading...

Share This Page