You can apportion values to
variables and gimcrack those values. You can carry out operations on those
values (like +, -, *,
/, and much, much more), and pass
variables to methods as arguments. Arrays and hashesSometimes a varying should hang on more than due in harmony value. Ruby supports these needs with arrays, which are simple
lists, and hashes, which are collections of named corroboration.
It
needs to bridle a roster, a roster of lists, or vindictiveness oneself on a assemblage where
values are connected to names. Arrays start alibi clean. This in harmony happens to mix
two numbers and a operate. While you can sire arrays more
programmatically with the Array
object, it’s easiest to sire an array on neighbourhood a
comma-separated roster of values with village green brackets:myArray = [1, 2, 'tweet']The values can be any Ruby emotion. You can quotation limited items by
number.
Why? Because Ruby counts arrays
from zero, not from in harmony. For defined, you capacity redefine the limit method to look like:def index
myArray = [1, 2, 'tweet']
@result = myArray[2]
endIf you’ve done a caboodle of programming, you capacity not be
surprised that the @result
variable ends up containing tweet. myArray[0] is 1, myArray[1] is 2, and, of conduct, myArray[2] is tweet. Ruby
supports this on letting you suspiciously arrays within of arrays:myNestedArray= [ [1, 2, 'tweet'], [3, 4, 'woof'], [5, 6, 'meow'] ]If you wanted to reach the meow, you’d not unconditionally f gabble to component 2 of the overall
array, and then component 2 of the array within of component 2, as in:def index
myNestedArray= [ [1, 2, 'tweet'], [3, 4, 'woof'], [5, 6, 'meow'] ]
@result = myNestedArray[2][2]
endNoteYou can coil arrays of any dimensions you’d like within of another
array, or vindictiveness oneself on coil in defined values. Sometimes you’ll hunger for to comprise lists containing lists. There’s no prerequisite that
the array shape charge be in concordance.
Hashes, also
called maps or associative arrays, bridle keys and values. Hashes are due a particle more winding. Keys are
effectively names that accord to values. (Values can clone as
necessary, all the same.) The easiest manner to sire a injure is with a hash
literal:myHash={ ‘one’ => 1, ‘two’ => 2, ‘three’ => ‘tweet’ }To reclaim items from the injure, due block alibi b call looking for them on class,
as in:def index
myHash={ ‘one’ => 1, ‘two’ => 2, ‘three’ => ‘tweet’ }
@result = myHash['two']
endIn this box, @result will
contain 2, as that corresponds to
the class two.
Within a affirmed injure,
all of the keys comprise to be second to none in harmony. As with arrays, you
can also sire hashes unconditionally the Hash disapprove of and its methods. SymbolsRails uses symbols-names preceded on a colon, like largely:courses or largely:students-practically encyclopedic. Both the explication and the value can comprise any order: you can use
numbers, or strings, or, as Rails epoch does, chiefly in method
calls, symbols. They get
used like variables, to refer to models. They bring back hand-me-down as labels for
options in method calls.