Hi,
I am new to ruby programming. I would like to know how to create a document and add elements like texts and tables. I also need to modify those and then write them into a file.
Is it possible to do it with Ruby?
Thanks,
K
| SitePoint Sponsor |
Hi,
I am new to ruby programming. I would like to know how to create a document and add elements like texts and tables. I also need to modify those and then write them into a file.
Is it possible to do it with Ruby?
Thanks,
K

All things are possible through Ruby. (or just about any other language for that matter).
Here's an article on creating and editing text files with Ruby: http://www.techotopia.com/index.php/..._Files_in_Ruby
You can add tables within views using Rails just like you would on any other website. Or, you can use CSS instead and pop your collar like all the other cool kids do.
"Be kind, for everyone you meet is fighting a
hard battle." -Plato
Hi,
Thanks for your suggestion. But I am purely using only RUBY. Could you please tell me how to create elements like (text, tables) in memory, then edit them in memory and finally print it on a file.
Thanks,
K



At its simplest form this does what you are asking for:
However, that is extremely simplistic and fails to use Ruby's object orientated splendidness. So although it works and is useful to demonstrate that what you ask for can be done, in reality you would package this up into classes to manage the process and make the code easier to maintain.Code:# A table is basically an array of arrays. So start by creating the # host array. table = Array.new # Then create some lines. Each line is itself an array line1 = ['name', 'colour'] line2 = ['George', 'blue'] line3 = ['mary', 'grean'] # Then put the line arrays in the table array table << line1 table << line2 table << line3 # Alter the second cell in the third line # (note first line = 0, and first cell = 0) third_line = 2 second_cell = 1 table[third_line][second_cell] = 'black' # Create an output file file_name = "output.txt" File.delete(file_name) if File.exist?(file_name) output_file = File.new(file_name, 'w+') # Add the table info, and close for line in table output_file.puts line.join(", ") end output_file.close



This is better:
Code:class DataTable attr_accessor :current_line, :current_column attr_reader :file_name def initialize @table = Array.new @current_line = 0 @current_column = 0 end def add_line(*entries) @table << entries end def alter_current_cell(entry) @table[@current_line][@current_column] = entry end def output_to_file(file_name) @file_name = file_name create_file put_data_into_file end private def create_file File.delete(@file_name) if File.exist?(@file_name) File.new(@file_name, 'w+') end def put_data_into_file output_file = File.open(@file_name, 'w+') for line in @table output_file.puts line.join(", ") end output_file.close end end data_table = DataTable.new data_table.add_line('Shape', 'Sides') data_table.add_line('square', 'four') data_table.add_line('triangle', 'six') data_table.current_line = 2 data_table.current_column = 1 data_table.alter_current_cell('three') data_table.output_to_file('output2.txt')
Thanks! That code worked brilliant.
Is there a way to delete one of the elements / row of elements from the table?
Could you please explain how well OO concepts is implemented in the code.
Thanks in advance
Is there a way to create doc in memory and then add the table elements to it?



Add this method to the class DataTable
Then you can do this:Code:def remove_line(line_number) @table.delete_at(line_number) end
Note again that the first line is line 0. Line 1 is the second line.Code:data_table = DataTable.new data_table.add_line('Shape', 'Sides') data_table.add_line('square', 'four') data_table.add_line('triangle', 'six') data_table.current_line = 2 data_table.current_column = 1 data_table.alter_current_cell('three') data_table.output_to_file('output.txt') data_table.remove_line(1) data_table.output_to_file('output_without_line_1.txt')
Thanks you so much. I was wondering whether it is possible to include HTML feature into the existing code.
1. I need to provide formatting options to change the color and font of HTML text
2. HTML support in Tables elements should support the following:
Primary and Alternate row background colors.
Border on or off.
Color/Font settings for the text in the optional header.
Color/Font settings for the data rows.
3. Write the document to an HTML file.
Thanks in advance



Yes. All that is possible. You just need to invest some time in learning Ruby, HTML and CSS.
I'd recommend you either invest in the latest copy of Dave Thomas' "Programming Ruby" or at least read one of the on line original additions.
As for the HTML stuff: both of sitepoint's HTML and CSS references are a good place to start:
http://reference.sitepoint.com/html
http://reference.sitepoint.com/css
Does CGI script helps for performing what I asked for or Using a module such as Haml or ERb help better?



If you want to serve web pages via a Ruby system, the simplest place to start is Rails.
You may find in the future that you use a different Ruby base technology to serve up web pages, but at the moment there is no better supported Ruby based web page server than Rails. Therefore, there is no better place to gain experience of Ruby for web page delivery than Rails.
Bookmarks