mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 17:48:01 +00:00
Add Fail as a ruby til
This commit is contained in:
@@ -242,6 +242,7 @@ _336 TILs and counting..._
|
|||||||
- [Editing Code In Pry](ruby/editing-code-in-pry.md)
|
- [Editing Code In Pry](ruby/editing-code-in-pry.md)
|
||||||
- [Evaluating One-Off Commands](ruby/evaluating-one-off-commands.md)
|
- [Evaluating One-Off Commands](ruby/evaluating-one-off-commands.md)
|
||||||
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
|
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
|
||||||
|
- [Fail](ruby/fail.md)
|
||||||
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
||||||
- [Identify Outdated Gems](ruby/identify-outdated-gems.md)
|
- [Identify Outdated Gems](ruby/identify-outdated-gems.md)
|
||||||
- [If You Detect None](ruby/if-you-detect-none.md)
|
- [If You Detect None](ruby/if-you-detect-none.md)
|
||||||
|
|||||||
22
ruby/fail.md
Normal file
22
ruby/fail.md
Normal 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)
|
||||||
Reference in New Issue
Block a user