Concept Flow - Nil as the absence of value
Start
Assign nil
Check if value is nil?
No→Use value
Yes
Handle absence of value
End
This flow shows assigning nil to a variable, checking if it is nil, and handling the absence of value accordingly.
value = nil if value.nil? puts "No value assigned" else puts "Value is: #{value}" end
| Step | Action | Variable 'value' | Condition 'value.nil?' | Output |
|---|---|---|---|---|
| 1 | Assign nil to value | nil | N/A | N/A |
| 2 | Check if value.nil? | nil | true | N/A |
| 3 | Since true, execute puts 'No value assigned' | nil | true | No value assigned |
| 4 | End of if-else | nil | true | No value assigned |
| Variable | Start | After Step 1 | After Step 4 |
|---|---|---|---|
| value | undefined | nil | nil |
Nil in Ruby means 'no value'. Assign nil to a variable to show absence. Use .nil? method to check if a variable is nil. If true, handle absence; else use the value. Nil is an object, not a keyword. Useful for optional or missing data.