CSC/ECE 517 Summer 2008/wiki1 6 jm: Difference between revisions
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
Both the Ruby and Java | Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. In this article I will compare the use of Arrays and hashtables in Ruby and Java. | ||
== Hashes in Ruby == | == Hashes in Ruby == |
Revision as of 18:23, 6 June 2008
Introduction
Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. In this article I will compare the use of Arrays and hashtables in Ruby and Java.
Hashes in Ruby
Arrays in Ruby
Creating Arrays in Ruby
Creating an array in Ruby is as simple as
a = []
Initializing an Array in Ruby
a = ["one",45,"hello"]
Array Comparison
Array Function | Ruby | Java |
Create | h = [] or
|
todo |
Initialize | h = ["one",1,"two"] | todo |
Element Access | h[0]
|
h[0] |
Hashtable Comparison
Hashtable Function | Ruby | Java |
Create | h = {} or
|
h = new Hashtable();
|
Initialize | h = {"key1" => "value1", "key2" => "value2" }
|
Not Available |
Add Key to Existing Hashtable | h["key3"] = "value3"
|
h.put("key3",new String("value3"));
|
Remove a Key - Value pair | h.delete("key1")
|
h.remove
|
Check if Key Exists | h.key?("key1")
|
h.containsKey("key1")
|
Check if Value Exists | h.value?("value1")
|
h.contains(new String("value1"));
|
Retrieve a Value Given a Key | h["key1"]
|
h.get("key1")
|