You can also represent numbers in hexadecimal, octal, and binary notation:
0b0101010 # => binary for 42
0700 # => octal for 448
0xFE_FE # => hex for 65278
Float
The Ruby Float class is internally represented as a Java double, and it also works in mostly the
same way as Java doubles. You can use all the regular numeric operations on Floats, and you
can define the literal values like this:
0.0002323
23234.233
-0.42e3
1E-132
Array
One of the most important types in Ruby, an Array contains the equivalent of java.util.List,
and some more useful operations too. A Ruby Array can contain any values you want, and
nothing stops you from mixing and matching. The Ruby Array is zero indexed, and you can
use numbers and ranges to get and set information in it. If you try to get a value from a larger
index than the size of the Array, it will return nil. If you set a value at a higher index, the Array
will expand to that point, and will insert nil values at the intermediate points.
The literal syntax uses square brackets:
[:abc, "foo", 123, bar]
There is a shortcut for the case of creating an array of strings, where each string is one
word:
%w(hello there my friend)
The preceding code is the same as this:
['hello', 'there', 'my', 'friend']
Ruby Arrays can be recursive:
a = []
a[0] = a
APPENDIX A ?– RUBY FOR JAVA PROGRAMMERS 293
You use square brackets to retrieve and set values in an Array:
a = [1, 2, 4, 8, 16, 234]
puts a[1]
puts a[1..3]
puts a[1, 2]
a[423] = 13
a[2.
Pages:
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430