Prev |
Contents |
Next |
Ruby user's guide | Arrays |
You can create an array by listing some items within
square brackets ([]
) and separating them with
commas. Ruby's arrays can accomodate diverse object types.
ruby> ary = [1, 2, "3"] [1, 2, "3"] |
Arrays can be concatenated or repeated just as strings can.
ruby> ary + ["foo", "bar"] [1, 2, "3", "foo", "bar"] ruby> ary * 2 [1, 2, "3", 1, 2, "3"] |
We can use index numbers to refer to any part of a array.
ruby> ary[0] 1 ruby> ary[0,2] [1, 2] ruby> ary[0..1] [1, 2] ruby> ary[-2] 2 ruby> ary[-2,2] [2, "3"] ruby> ary[-2..-1] [2, "3"] |
(Negative indices mean offsets from the end of an array, rather than the beginning.)
Arrays can be converted to and from strings, using join
and split
respecitvely:
ruby> str = ary.join(":") "1:2:3" ruby> str.split(":") ["1", "2", "3"] |
An associative array has elements that are accessed not by
sequential index numbers, but by keys which can have any sort
of value. Such an array is sometimes called a hash or
dictionary; in the ruby world, we prefer the term
hash. A hash can be constructed by quoting pairs of items
within curly braces ({}
). You use a key to find
something in a hash, much as you use an index to find something in an
array.
ruby> h = {1 => 2, "2" => "4"} {1=>2, "2"=>"4"} ruby> h[1] 2 ruby> h["2"] "4" ruby> h[5] nil ruby> h[5] = 10 # appending value 10 ruby> h {5=>10, 1=>2, "2"=>"4"} ruby> h.delete 1 # deleting value 2 ruby> h[1] nil ruby> h {5=>10, "2"=>"4"} |
Prev |
Contents |
Next |