[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Subclassing Array

wmwilson01

2/26/2007 3:42:00 PM

I'm sure I'm just being an idiot here... my mind is a little foggy this
morning, but I'm having a hard time understanding how to accomplish
this. I've written a class (I'll just put some snippets in for
understanding) in which I'd like to be able to use the following
behavior:

irb> columns = [[1, "Hostname"], [2, "Model"], [5, "OS Version"]]
irb> report = InventoryReport.new("HARDWARE_QUERY", columns)
irb> report.query.map {|a| a.first.downcase!; a}.save "/tmp/test.xls"

Where query() returns an array like [["host1", "Dell", "Windows"],
["host2", "Hitachi", "Windows"], ...]

However, throwing a map (or select or whatever) in there (obviously)
ends up returning an Array. How would I handle this so that it works as
I would like? Do I need to move my save(), to_csv(), and to_xls() stuff
into the Array class?

Sorry if this is a dumb question.

class InventoryReport < Array
def initialize(query_name, fields)
@query_name = query_name
@fields = fields
super()
end

def query
cols = @fields.map {|a| a.first - 1}
%x{runquery #{@query_name}}.each_line do |l|
t = l.chomp.split
tmp = []
cols.each do |a|
tmp << t[a].strip
end
self << tmp
end
self
end

def save(filename)
case filename
when /.*\.csv/ then to_csv(filename)
when /.*\.xls/ then
to_csv(filename.gsub(/xls$/, "csv"))
to_xls(filename, filename.gsub(/xls$/, "csv")
end
end

def to_csv(...)
end

def to_xls(...)
end
end

--
Posted via http://www.ruby-....

15 Answers

Ilan Berci

2/26/2007 3:59:00 PM

0

El Gato wrote:
> I'm sure I'm just being an idiot here... my mind is a little foggy this
> morning, but I'm having a hard time understanding how to accomplish
> this. I've written a class (I'll just put some snippets in for
> understanding) in which I'd like to be able to use the following
> behavior:
>

El Gato,

My suggestion is to take a good look at Ruby's incredibly powerfull use
of delegates
http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/...
(documentation provided by James Earl Gray II)

Secondly, there is a great book entitled "Ruby Recipes", which discusses
your question in great detail and I higly recommend that you pick it
up..

Hope this helps and this is only a suggestion as there exists 100 ways
to do what you are asking.. :)

ilan




--
Posted via http://www.ruby-....

James Gray

2/26/2007 4:05:00 PM

0

On Feb 26, 2007, at 9:58 AM, Ilan Berci wrote:

> (documentation provided by James Earl Gray II)

I have become a tea.

James *Edward* Gray II

Matt Lawrence

2/26/2007 4:06:00 PM

0

Ilan Berci

2/26/2007 4:07:00 PM

0

James Gray wrote:
> On Feb 26, 2007, at 9:58 AM, Ilan Berci wrote:
>
>> (documentation provided by James Earl Gray II)
>
> I have become a tea.
>
> James *Edward* Gray II

Ouch!!! Apologies.. Not enough coffee this morning and guess what my
wife's favourite is.. Also forgot to mention Gavin as well.. :(

I will keep quiet for the remainder of the day.. :)

ilan



--
Posted via http://www.ruby-....

James Gray

2/26/2007 4:11:00 PM

0

On Feb 26, 2007, at 10:07 AM, Ilan Berci wrote:

> James Gray wrote:
>> On Feb 26, 2007, at 9:58 AM, Ilan Berci wrote:
>>
>>> (documentation provided by James Earl Gray II)
>>
>> I have become a tea.
>>
>> James *Edward* Gray II
>
> Ouch!!! Apologies.. Not enough coffee this morning and guess what my
> wife's favourite is..

No worries. Hal listed me as James Edward Gray III in the second
edition of The Ruby Way. Good to know he gives me the +1 I guess... ;)

James Edward Gray II

wmwilson01

2/26/2007 4:43:00 PM

0

Ilan Berci wrote:
> El Gato wrote:
>> I'm sure I'm just being an idiot here... my mind is a little foggy this
>> morning, but I'm having a hard time understanding how to accomplish
>> this. I've written a class (I'll just put some snippets in for
>> understanding) in which I'd like to be able to use the following
>> behavior:
>>
>
> El Gato,
>
> My suggestion is to take a good look at Ruby's incredibly powerfull use
> of delegates
> http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/...
> (documentation provided by James Earl Gray II)
>
> Secondly, there is a great book entitled "Ruby Recipes", which discusses
> your question in great detail and I higly recommend that you pick it
> up..
>
> Hope this helps and this is only a suggestion as there exists 100 ways
> to do what you are asking.. :)
>
> ilan


I'm not sure if I'm missing something, but it appears that calling map()
on a DelegateClass still returns an Array object. I guess I would need
map() and friends to return an InventoryReport class (i.e., Array
doesn't have a save method, whereas InventoryReport does, so calling
report.query.map {...}.save "/tmp/filename.xls" fails)

--
Posted via http://www.ruby-....

_why

2/26/2007 4:46:00 PM

0

On Tue, Feb 27, 2007 at 01:04:39AM +0900, James Edward Gray II wrote:
> On Feb 26, 2007, at 9:58 AM, Ilan Berci wrote:
>
> >(documentation provided by James Earl Gray II)
>
> I have become a tea.

Not just any tea. A tea that speaks in the voice of Darth Vader.

_why

Ara.T.Howard

2/26/2007 4:48:00 PM

0

Ilan Berci

2/26/2007 4:54:00 PM

0

El Gato wrote:

> I'm not sure if I'm missing something, but it appears that calling map()
> on a DelegateClass still returns an Array object. I guess I would need
> map() and friends to return an InventoryReport class (i.e., Array
> doesn't have a save method, whereas InventoryReport does, so calling
> report.query.map {...}.save "/tmp/filename.xls" fails)

El Gato,

No, you didn't miss anything, your delegate class would return and
InventoryReport on map() (on a side note, I dislike the re-usage of the
"map" name for alternate functionality but that's another discussion)
which would have the save() method. The delegate pattern allows you to
"intercept" selectively in order to accomplish the task at hand.


ilan



--
Posted via http://www.ruby-....

Chad Perrin

2/26/2007 5:27:00 PM

0

On Tue, Feb 27, 2007 at 01:10:46AM +0900, James Edward Gray II wrote:
> On Feb 26, 2007, at 10:07 AM, Ilan Berci wrote:
> >James Gray wrote:
> >>On Feb 26, 2007, at 9:58 AM, Ilan Berci wrote:
> >>
> >>>(documentation provided by James Earl Gray II)
> >>
> >>I have become a tea.
> >>
> >>James *Edward* Gray II
> >
> >Ouch!!! Apologies.. Not enough coffee this morning and guess what my
> >wife's favourite is..
>
> No worries. Hal listed me as James Edward Gray III in the second
> edition of The Ruby Way. Good to know he gives me the +1 I guess... ;)
>
> James Edward Gray II

James Edward Gray II ++

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the Lucid