Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > interesting usages of Struct?

Reply
Thread Tools

interesting usages of Struct?

 
 
Joe at CodeGear
Guest
Posts: n/a
 
      02-17-2007
Anyone have an interesting or idiomatic usage of Struct?

Other than the obvious usage for on-the-fly attr reader/writer
objects, what else can you do with this?

One use case that came to mind was for managing CSV files (esp. w/
headers). You could create a Struct for the file, using the header
row from the file, passing a block to the new method to add a method
to initialize it based on an array (as returned from the csv reader).

Anyone have a cool usage?

Joe at CodeGear

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      02-17-2007
On Feb 16, 2007, at 7:30 PM, Joe at CodeGear wrote:

> One use case that came to mind was for managing CSV files (esp. w/
> headers). You could create a Struct for the file, using the header
> row from the file, passing a block to the new method to add a method
> to initialize it based on an array (as returned from the csv reader).


I really like the way you think:

#!/usr/bin/env ruby -w

require "rubygems"
require "faster_csv"

require "pp"

Name = Struct.new(:first, :last)
names = [Name.new("James", "Gray"), Name.new("Joe")]

csv = FCSV.dump(names)
puts <<END_CSV
CSV
===
#{csv}
END_CSV

reloaded = FCSV.load(csv)
puts <<END_RUBY
Ruby
====
END_RUBY
pp reloaded
# >> CSV
# >> ===
# >> class,Name
# >> first=,last=
# >> James,Gray
# >> Joe,
# >>
# >> Ruby
# >> ====
# >> [#<struct Name first="James", last="Gray">,
# >> #<struct Name first="Joe", last=nil>]

__END__

James Edward Gray II




 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      02-17-2007
On 2/17/07, Joe at CodeGear <(E-Mail Removed)> wrote:
> Anyone have an interesting or idiomatic usage of Struct?
>
> Other than the obvious usage for on-the-fly attr reader/writer
> objects, what else can you do with this?
>
> One use case that came to mind was for managing CSV files (esp. w/
> headers). You could create a Struct for the file, using the header
> row from the file, passing a block to the new method to add a method
> to initialize it based on an array (as returned from the csv reader).
>
> Anyone have a cool usage?
>
> Joe at CodeGear
>
>
>

I was naming regular expressions with it roughly like this

Fields=Struct.new(:a,:b,:c)
/(x*)(y*)(z*)/ === whatever
named= Fields.new( *Regexp.last_match.captures )

Cheers
Robert

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

 
Reply With Quote
 
Sharon Phillips
Guest
Posts: n/a
 
      02-17-2007
At work I need to process different bits of information stored in csv
format. I use OpenStruct modified to create new propertied from
strings that I read from the file headers. I don't have the code
here, but can get it if anyone is interested.

I find this method very useful as then I can work with the properties
I require, but the code doesn't fall over when the format of the
files changes (not that that would happen without anyone telling me
first... )

Cheers,
Dave


On 17/02/2007, at 12:30 PM, Joe at CodeGear wrote:

> Anyone have an interesting or idiomatic usage of Struct?
>
> Other than the obvious usage for on-the-fly attr reader/writer
> objects, what else can you do with this?
>
> One use case that came to mind was for managing CSV files (esp. w/
> headers). You could create a Struct for the file, using the header
> row from the file, passing a block to the new method to add a method
> to initialize it based on an array (as returned from the csv reader).
>
> Anyone have a cool usage?
>
> Joe at CodeGear
>
>



 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      02-17-2007
On Feb 17, 1:39 am, "Robert Dober" <(E-Mail Removed)> wrote:
> I was naming regular expressions with it roughly like this
>
> Fields=Struct.new(:a,:b,:c)
> /(x*)(y*)(z*)/ === whatever
> named= Fields.new( *Regexp.last_match.captures )


Ooh, that's quite cool. Thanks for sharing that!

 
Reply With Quote
 
Gregory Seidman
Guest
Posts: n/a
 
      02-17-2007
On Sun, Feb 18, 2007 at 12:00:15AM +0900, Phrogz wrote:
> On Feb 17, 1:39 am, "Robert Dober" <(E-Mail Removed)> wrote:
> > I was naming regular expressions with it roughly like this
> >
> > Fields=Struct.new(:a,:b,:c)
> > /(x*)(y*)(z*)/ === whatever
> > named= Fields.new( *Regexp.last_match.captures )

>
> Ooh, that's quite cool. Thanks for sharing that!


That's one way of doing it. There was a discussion not too long ago of
doing it with the MatchData itself. Something like (tested, works):

class MatchData
def name_captures(*names)
meta = (class << self; self; end)
names.each_with_index { |name,index|
meta.send(:define_method, name) { captures[index] }
}
self
end
end

named = /(x*)(y*)(z*)/.match(whatever).name_captures(:a, :b, :c)

--Greg


 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      02-17-2007
On 2/17/07, Phrogz <(E-Mail Removed)> wrote:
> On Feb 17, 1:39 am, "Robert Dober" <(E-Mail Removed)> wrote:
> > I was naming regular expressions with it roughly like this
> >
> > Fields=Struct.new(:a,:b,:c)
> > /(x*)(y*)(z*)/ === whatever
> > named= Fields.new( *Regexp.last_match.captures )

>
> Ooh, that's quite cool. Thanks for sharing that!
>
>
>

My pleasure
Robert

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      02-17-2007
On 2/17/07, Gregory Seidman <(E-Mail Removed)> wrote:
> On Sun, Feb 18, 2007 at 12:00:15AM +0900, Phrogz wrote:
> > On Feb 17, 1:39 am, "Robert Dober" <(E-Mail Removed)> wrote:
> > > I was naming regular expressions with it roughly like this
> > >
> > > Fields=Struct.new(:a,:b,:c)
> > > /(x*)(y*)(z*)/ === whatever
> > > named= Fields.new( *Regexp.last_match.captures )

> >
> > Ooh, that's quite cool. Thanks for sharing that!

>
> That's one way of doing it. There was a discussion not too long ago of
> doing it with the MatchData itself. Something like (tested, works):
>
> class MatchData
> def name_captures(*names)
> meta = (class << self; self; end)
> names.each_with_index { |name,index|
> meta.send(:define_method, name) { captures[index] }
> }
> self
> end
> end
>
> named = /(x*)(y*)(z*)/.match(whatever).name_captures(:a, :b, :c)
>
> --Greg
>
>
>

That seems a little bit heavy. I was thinking to extend
RegularExpressions so that the names could be bound to the Regexp for
one and than just thought that I do not have enough usecases.
AFAIK Ruby 2 will have named captures and I am not really smart enough to handle
alternatives and nesting so I let it be...

Cheers
Robert


--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 usages of same name in different contexts !! karthikbalaguru C Programming 8 03-14-2009 04:36 AM
Re: Struct usages in Python Alex Gusarov Python 4 05-29-2008 07:50 AM
Java and SSL/HTTPS, some advanced usages in one place Rogan Dawes Java 0 01-13-2006 11:46 AM
"Interesting" behavior with aggregates Tim Hubberstey VHDL 6 06-30-2004 06:38 AM
in response to an interesting thread catchmerevisited Firefox 2 05-08-2004 10:57 AM



Advertisments