1. Home
  2. Computing & Technology
  3. Ruby

Edit Boxes

By Amanda & Michael Morin, About.com

2 of 3

A Practical Application: The To-Do List

Knowing how to use edit boxes, button and links, means you can now create a complete application that does something useful. The following application will allow you to add items to a list and, when the link is clicked in the list item, remove that item from the list. This application could easily be used as a variation of a To-Do list.

Shoes.app :width => 400, :height => 340 do
  def draw_list
    @list_box.clear

    @list.each do|l|
      @list_box.append do
        done_link = link("Done") do
          @list.delete l
          draw_list
        end

        para l, " ", done_link
      end
    end
  end
  @list = []

  @e = edit_line
  @e.focus

  button "Add" do
    @list << @e.text
    draw_list
    @e.text = ''
  end

  @list_box = stack
end

At first, this program looks a lot more complicated than it really is. However, almost all of the code is the draw_list method. This method takes the strings in @list, creates paras with links and adds them all to @list_box.

Below draw_list is where all the edit_line code happens. As in the previous example, an edit_line is created and stored in @e. The focus method is called, which puts the keyboard cursor in the edit_line so the program can be started and you can just start typing. The button does almost the same thing as in the previous example as well. When the button is clicked, the text in @e is stored in @list (an array of strings), @e is cleared and the draw_list method is called.

Explore Ruby
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. GUI
  5. Shoes
  6. A Practical Application: The To-Do List

©2009 About.com, a part of The New York Times Company.

All rights reserved.