-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
In programming languages that do not support exceptions, errors are generally handled by using a return value that indicates failure. Ruby itself is written in C, and in C, functions that can fail will often use a return value that is zero on success, and non-zero on failure. While Ruby has exceptions, there are instances where methods return a value on failure instead of raising an exception, even in cases where other programming languages raise an exception. Typically, the returned value is nil in these cases.
For example, in Python, if you have a hash (called a dictionary in Python), and you try to access a member in the hash that doesn't exist, Python raises an exception:
# Python code:
{'a': 2}['b']
# KeyError: 'b'
Ruby takes a different approach, returning nil:
{'a' => 2}['b']
# => nil
This shows the different philosophies between the languages. In Ruby, it is expected that when you...
Change the font size
Change margin width
Change background colour