Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Ruby Programming
Previous Page Home Next Page

Sample Application

Here's a slightly longer example, showing a genuine application---a ``pig Latin'' generator. Type in the phrase such as ``Ruby rules,'' and the ``Pig It'' button will instantly translate it into pig Latin.

require 'tk'

class PigBox   def pig(word)     leadingCap = word =~ /^A-Z/     word.downcase!     res = case word       when /^aeiouy/         word+"way"       when /^([^aeiouy]+)(.*)/         $2+$1+"ay"       else         word     end     leadingCap ? res.capitalize : res   end

  def showPig     @text.value = @text.value.split.collect{|w| pig(w)}.join(" ")   end

  def initialize     ph = { 'padx' => 10, 'pady' => 10 }     # common options     p = proc {showPig}

    @text = TkVariable.new     root = TkRoot.new { title "Pig" }     top = TkFrame.new(root)     TkLabel.new(top) {text    'Enter Text:' ; pack(ph) }     @entry = TkEntry.new(top, 'textvariable' => @text)     @entry.pack(ph)     TkButton.new(top) {text 'Pig It'; command p; pack ph}     TkButton.new(top) {text 'Exit'; command {proc exit}; pack ph}     top.pack('fill'=>'both', 'side' =>'top')   end end

PigBox.new Tk.mainloop

Sidebar: Geometry Management

In the example code in this chapter, you'll see references to the widget method pack. That's a very important call, as it turns out---leave it off and you'll never see the widget. pack is a command that tells the geometry manager to place the widget according to constraints that we specify. Geometry managers recognize three commands:

Command Placement Specification
pack Flexible, constraint-based placement
place Absolute position
grid Tabular (row/column) position

As pack is the most commonly used command, we'll use it in our examples.

Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire