How to collect question 
Author Message
 How to collect question

  Hello all, I have been coding in Ruby for a few days now and have
  some questions.

  I understand (I think) about telling collect to an array as exhibited
  by the table method below.

  I would like to do something similar in the method table_fancy but
  had to use a temp var, x, to make it work.

  Any ideas on how I can eliminate the x?

class Array

  def table (*args)

    alf = HTMLtable.new(*args)
    self.each { |r| alf << trow (r.collect! { |c| tcell (c)})}
    alf.to_s

  end

  def table_fancy (format, *args)

    alf = HTMLtable.new(*args)
    x = ''

    self.each { |row| 0.upto (row.length - 1) { |i| x << tcell (row[i], *format[i]) }
      alf << trow (x)
      x = ''}

    alf.to_s

  end

end

  This is an excerpt from my HTML lib that I wrote for Scheme and am
  porting to Ruby.

  The above functions would be called as:

    doc = HTMLdoc.new(H_title => 'Some Document')

    foo = [['red', 'green', 'blue'],
          ['moe', 'martin', 'alf']]

    format = [[H_th => true], [H_bgcolor => '#ddffdd'], []]

    doc << foo.table (H_border => 1)

    doc << foo.table_fancy (format, H_border => 1)

    puts doc

  Thank you for you thoughts,
  -Dave



Wed, 14 Jan 2004 03:49:12 GMT  
 How to collect question
Hello --

Quote:

>   Hello all, I have been coding in Ruby for a few days now and have
>   some questions.

Always glad to see a new Dav* :-)

Quote:
>   I understand (I think) about telling collect to an array as exhibited
>   by the table method below.

>   I would like to do something similar in the method table_fancy but
>   had to use a temp var, x, to make it work.

>   Any ideas on how I can eliminate the x?

> class Array

It seems a little sideways to add these methods to Array.  I think it
might be more logical to have the HTMLtable objects know how to
assemble themselves.  But anyway, let me address the question as it
stands.

[...]

Quote:
>   def table_fancy (format, *args)

>     alf = HTMLtable.new(*args)
>     x = ''

>     self.each { |row| 0.upto (row.length - 1) { |i| x << tcell (row[i], *format[i]) }
>       alf << trow (x)

(Note: if you have a space between a method name and a parenthesized
argument list, you'll get a warning (when running under -w)).

Quote:
>       x = ''}

>     alf.to_s

>   end

> end

In order to gather up the results of doing the tcell call over the
whole array, I think you'll need either a temporary variable or an
array method which will return a new array with those results.

#map (aka #collect) will do that, except you also need to keep track
of the indices for the purpose of stepping through "format".

Unfortunately, there's no #map_with_indices.  So you might want to use
this one:

  module Enumerable
    def map_with_indices
      res = []
      each_with_index do |e,i|
        res.push yield e,i
      end
      res
    end
  end

and then do something like this:

  def table_fancy(format, *args)
    HTMLtable.new(*args) << map {|row|
       row.map_with_indices {|c,i|
        tcell(c, format[i])
      }
    } .map {|x| trow(x)} .to_s
  end

which basically filters the array through the cell and row routines
and converts the results to a string.

(I keep staring at that and thinking there should be a way to reduce
it by about 40%, but pending that, see if it helps :-)

Another way: if you don't mind destroying "format", you could do:

   ...
   row.map { |c|
     tcell(c, format.shift)
   }
   ...

or dup format first (but then you have a temporary variable again :-),
or call the method with a dup of format.

(Sorry, this got longer than I'd expected.)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 14 Jan 2004 11:21:52 GMT  
 How to collect question

Quote:
> Hello --


> >   Hello all, I have been coding in Ruby for a few days now and have
> >   some questions.

> Always glad to see a new Dav* :-)

First David: G'day, David!

Second David: Oh, Hello David!

[Jim enters with Third David.]

Jim: Hello.

Third David: Jim, David. David, Jim. Jim, David. David, Jim. David here
wrote a book, David here compares languages, David here reduces code size,
and David here writes useful Ruby modules. Oh, and David over there helps
out all the newbies.

First David: Is your name not David?

Jim: No, it's Jim.

Second David: Mind if we call you David to keep it clear?

Third David: Right, I just want to tell you the news group/ML rules. Rule
One:

Everybody: Everybody is welcome!

Third David: Rule Two: no member of the group is to abuse anybody using any
other language -- if there's anybody watching. Rule Three?

Everybody: Everybody is welcome!

Third David: Rule Four: now this term, matz doesn't want anybody to be
caught drinking. ... Rule Five:

Everybody: Everybody is welcome!

Third David: Rule Six: there is NO ... Rule Six! Rule Seven:

Everybody: Everybody is welcome!

First David: Right, that concludes the reading of the rules, David.

Second David: This here's the Ruby, it's the emblem of our land. You can
stick it on a book, you can compile it and run it on a computer in your
hand.

Everybody: Amen!

[With apologies to Monty Python]

Jim
--

As a math major, I don't have to be able to add -- I just have to be able
to PROVE that I can add.



Wed, 14 Jan 2004 21:57:08 GMT  
 How to collect question

Quote:

> It seems a little sideways to add these methods to Array.  I think it
> might be more logical to have the HTMLtable objects know how to
> assemble themselves.

  It does:

    HTMLtable.new(some_array)

  and

    some_array.table

  and

     table (some_array)  # Differs from HTMLtable in that it returns
                         # a string of HTML instead of a table object.

  all work.

  I guess I'm into overkill a little (which probably explains why
  one of my coworkers keeps asking "Why don't you actually finish
  a program someday?" - I spend too much time trying to cover all
  possible cases.  I just read the section on making software
  "good enough" in _The_Pragmatic_Programmer_ and I should probably
  give that some thought)

<various snippage>

Quote:
> Another way: if you don't mind destroying "format", you could do:

>   ...
>   row.map { |c|
>     tcell(c, format.shift)
>   }
>   ...

  I like that and should have thought of it.

Quote:
> (Sorry, this got longer than I'd expected.)

  Not a problem for me.

  -Dave

--
David Tillman                      |  System Administrator



Wed, 14 Jan 2004 23:04:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. collecting questions about J

2. collected questions on J

3. Collecting questions about J

4. Collecting questions about J

5. Is there someone out there collecting J bugs?

6. Collecting favorite bug stories

7. DBResultSet>>#collect:

8. GP Fault using collect:

9. ways to collect data from two collections of the same size

10. Collection of collections, how to collect?

11. How to make sure objects get garbage collected?

12. segmentation fault on gabage collect

 

 
Powered by phpBB® Forum Software