Information Technology Reference
In-Depth Information
The results of those expressions can also be negated using the
not
keyword or the bang (
!
)
operand:
!
true
#=> false
not
not
true
#=> false
not
not
false
#=> true
!!
true
#=> true
not
not
nil
#=> true
Arrays
Ruby's native array support allows you to create lists of items. Ruby's arrays use the bracket
notation as shown in the following example. Arrays are
zero-indexed
and the Ruby interpret-
er automatically allocates memory as new items are added; there is no need to worry about
dynamically resizing arrays:
types
= [
'crispy'
,
'raw'
,
'crunchy'
,
'grilled'
]
types
.
length
#=> 4
types
.
size
#=> 4
types
.
push
'smoked'
#=> ["crispy", "raw", "crunchy", "grilled", "smoked"]
types
<<
'deep fried'
#=> ["crispy", "raw", "crunchy", "grilled",
"smoked"
,
"deep fried"
]
types
[
0
]
#=> "crispy"
types
.
first
#=> "crispy"
types
.
last
#=> "deep fried"
types
[
0
.
.
1
]
#=> ["crispy", "raw"]
The
length
method tells us how many items are in the array.
either.