class Set

 

Parent: Object

Class Index

Methods: new, clear, contains?, delete, each, empty?, include?, insert, length, size, to_a

 

Sample Code:

 

  The set class represents a collection of unique objects.
 

Class Methods


 

new

The new method is used to create a new set.

Syntax:

set = Set.new

Arguments:

 

Return Value:

set - a new Set object if successful

Comments:

 

Example:

set = Set.new
if (set)
UI.messagebox set
else
UI.messagebox "Failure"
end

 

Instance Methods

clear

The clear method is used to clear all objects out of the set.

Syntax:

set = set.clear

Arguments:

 

Return Value:

set - an empty Set object

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"      
set = set.insert toolbar
if (set.include? toolbar)
UI.messagebox "Success: Contains Toolbar Object"
else
UI.messagebox "Failure"
end
set = set.clear        
if (set.include? toolbar)
UI.messagebox set
else
UI.messagebox "Set is empty"
end


 

contains?

The contains? method is an alias for include?. See also Set.include?

Syntax:

status = set.contains? object

Arguments:

object - a Ruby object of any type

Return Value:

status - true if the set contains the object, false if the set does not contain the object.

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
if (set.contains? toolbar)
UI.messagebox "Success: Contains Toolbar Object"
else
UI.messagebox "Failure"
end


 

delete

The delete object is used to delete or remove an object from the set.

Syntax:

object = set.delete object

Arguments:

object - the object to be deleted.

Return Value:

object - the object that was deleted.

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"      
set = set.insert toolbar
if (set.include? toolbar)
UI.messagebox "Success: Contains Toolbar Object"
else
UI.messagebox "Failure"
end
set = set.delete toolbar
if (set.include? toolbar)
UI.messagebox set
else
UI.messagebox "Set is empty"
end


 

each

The each method is used to iterate through all of the objects in the set.

Syntax:

set.each {| item |...}

Arguments:

 

Return Value:

item – variables that will hold each object as it is found.

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
set.each { | item | UI.messagebox item }


 

empty?

The empty? method is used to determine whether the set is empty.

Syntax:

status = set.empty

Arguments:

 

Return Value:

status - true if the set is empty, false if it is not empty.

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
status = set.empty?
if (status)
UI.messagebox "Success: Set is Empty"
else
UI.messagebox "Failure: Set has an Item"
end


 

include?

The include? method is used to determine if the set includes a particular object. This method is the same as the contains? method.

Syntax:

status = set.contains? object

Arguments:

object - a Ruby object of any type

Return Value:

status - true if the set contains the object, false if the set does not contain the object.

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
if (set.include? toolbar)\
UI.messagebox "Success: Contains Toolbar Object"
else
UI.messagebox "Failure"
end


 

insert

The insert method is used to insert an object into the set.

Syntax:

size = set.insert object

Arguments:

object - the object to be inserted into the set

Return Value:

size - the number of objects in the set

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
if (set.include? toolbar)
UI.messagebox "Success: Contains Toolbar Object"
else
UI.messagebox "Failure"
end


 

length

The length method is an alias for size. See also Set.size.

Syntax:

length = set.length

Arguments:

 

Return Value:

length - the length (number of objects) in the set

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"      
set = set.insert toolbar
length = set.length
if (length)
UI.messagebox length
else
UI.messagebox "Failure"
end


 

size

The size method is used to determine the number of objects in the set.

Syntax:

size = set.size

Arguments:

 

Return Value:

size - the number of objects in the set

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
size = set.size
if (size)
UI.messagebox size
else
UI.messagebox "Failure"
end


 

to_a

The to_a method is used to convert the array into an Array class.

Syntax:

array = set.to_a

Arguments:

 

Return Value:

array - an Array object representing the set

Comments:

 

Example:

set = Set.new
toolbar = UI::Toolbar.new "Test"
set = set.insert toolbar
a = set.to_a
if (a)
UI.messagebox a
else
UI.messagebox "Failure"
end