Ruby’s size method reports how many elements or characters a collection-like object contains. For the string "Ruby is a cool language.", string.size returns 24; length is a synonym. section 8.1.3
The meaning of size follows the receiver:
- A String returns its number of characters. section 8.1.3
- An Array returns the number of elements, which can determine how many times an indexed iteration must yield. section 6.3.6
- A Hash returns the number of key/value pairs; length is a synonym. section 9.3.6
Using size to implement iteration
An indexed iterator can keep a counter, stop when the counter equals size, yield self[c] on each pass, and return the original array. This makes size a practical boundary for traversing every array element. section 6.3.6
>> string = "Ruby is a cool language."
>> string.size
=> 24
Inspect a concrete String#size call and its returned character count. section 8.1.3
Table 9.3 Common hash query methods and their meanings
| Method name/Sample call |
Meaning |
|---|---|
|
|
True if |
|
|
Synonym for |
|
|
Synonym for |
|
|
Synonym for |
| |
True if any value in |
| |
Synonym for |
|
|
True if |
|
|
Number of key/value pairs in |
Compare Hash#size with related state-query methods and see that it counts pairs rather than keys or values separately. section 9.3.6
