1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Fail as a ruby til

This commit is contained in:
jbranchaud
2016-02-16 17:08:39 -06:00
parent d6b5719322
commit 5f1500dfad
2 changed files with 23 additions and 0 deletions

22
ruby/fail.md Normal file
View File

@@ -0,0 +1,22 @@
# Fail
The `fail` method is synonymous with `raise` in Ruby. Consider the following
code snippet:
```ruby
def is_odd?(num)
num.odd?
rescue
fail StandardError, 'you cannot use odd on this'
end
> is_odd?(1)
#=> true
> is_odd?('hello')
#=> StandardError: you cannot use odd on this
```
Nevertheless, I believe the `raise` method is preferred to the `fail`
method.
[source](http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-fail)