mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #19892 from owen-mc/fix-markdown-query-help-formatting
Fix markdown query help formatting
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
# Method call on `nil`
|
||||
|
||||
## Description
|
||||
## Overview
|
||||
In Ruby, it is not necessary to explicitly initialize variables.
|
||||
If a local variable has not been explicitly initialized, it will have the value `nil`. If this happens unintentionally, though, the variable will not represent an object with the expected methods, and a method call on the variable will raise a `NoMethodError`.
|
||||
|
||||
@@ -11,7 +9,9 @@ This can be achieved by using a safe navigation or adding a check for `nil`.
|
||||
|
||||
Note: You do not need to explicitly initialize the variable, if you can make the program deal with the possible `nil` value. In particular, initializing the variable to `nil` will have no effect, as this is already the value of the variable. If `nil` is the only possible default value, you need to handle the `nil` value instead of initializing the variable.
|
||||
|
||||
## Examples
|
||||
## Example
|
||||
|
||||
### Incorrect Usage
|
||||
|
||||
In the following code, the call to `create_file` may fail and then the call `f.close` will raise a `NoMethodError` since `f` will be `nil` at that point.
|
||||
|
||||
@@ -24,6 +24,8 @@ ensure
|
||||
end
|
||||
```
|
||||
|
||||
### Correct Usage
|
||||
|
||||
We can fix this by using safe navigation:
|
||||
```ruby
|
||||
def dump(x)
|
||||
@@ -36,6 +38,5 @@ end
|
||||
|
||||
## References
|
||||
|
||||
- https://www.rubyguides.com/: [Nil](https://www.rubyguides.com/2018/01/ruby-nil/)
|
||||
- https://ruby-doc.org/: [NoMethodError](https://ruby-doc.org/core-2.6.5/NoMethodError.html)
|
||||
|
||||
- RubyGuides: [Everything You Need To Know About Nil](https://www.rubyguides.com/2018/01/ruby-nil/).
|
||||
- Ruby-Doc.org: [NoMethodError](https://ruby-doc.org/core-2.6.5/NoMethodError.html).
|
||||
|
||||
Reference in New Issue
Block a user