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.
Yo u'll sometimes see size used as a synonym for length ; Ruby offers the choice to use
either.
Ad d one or more items to the end of the array with push .
<< is a helpful alias for push when you want to add just one item to the end.
Ar rays are zero-indexed, so we can access the first element using 0 as the index.
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search