Skip to content
Related Articles

Related Articles

Ruby | Numeric nonzero? function
  • Last Updated : 06 Jan, 2020

The nonzero?() is an inbuilt method in Ruby returns self if the number is non-zero, else it returns nil.

Syntax: num.nonzero?()

Parameters: The function needs a number which is to be checked.

Return Value: It returns self or nil.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for nonzero?
# method in Numeric
  
# Initialize a number 
num1 = 12
  
# Prints if nonzero or not 
puts num1.nonzero?()

chevron_right


Output:

12

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program for nonzero?
# method in Numeric
  
# Initialize a number 
num1 = 0
  
# Prints if nonzero or not 
puts num1.nonzero?()

chevron_right


Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :