SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create doc and add elements like tables and text

    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

  2. #2
    SitePoint Zealot phillipus_rex's Avatar
    Join Date
    Jan 2007
    Location
    Silicon Holler
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skrithikaa View Post
    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

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At its simplest form this does what you are asking for:
    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
    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.

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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')

  6. #6
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to create doc in memory and then add the table elements to it?

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add this method to the class DataTable
    Code:
      def remove_line(line_number)
        @table.delete_at(line_number)
      end
    Then you can do this:
    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')
    Note again that the first line is line 0. Line 1 is the second line.

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does CGI script helps for performing what I asked for or Using a module such as Haml or ERb help better?

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •