Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
math
communitycreator

What is num.nonzero? in Ruby?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The num.nonzero? function returns numself if the number is nonzero; otherwise, it returns nilnothing.

Figure 1 shows the visual representation of the num.nonzero? function.

Figure 1: Visual representation of the num.nonzero? function

Syntax

num.nonzero?
# where num is the number which is to be checked for nonzero

Parameter

This function does not require a parameter.

Return value

If the number is non-zero, the return type of nonzero? will be the number itself. Otherwise, it returns nil.

Code

#zero number
print "(0).nonzero? : ",(0).nonzero? , "\n"
print "(0.0).nonzero? : ",(0.0).nonzero? , "\n"
#non-zero number
print "(4).nonzero? : ", (4).nonzero?, "\n"
print "(4.5).nonzero? : ", (4.5).nonzero?, "\n"
Implementation of num.nonzero? in Ruby

RELATED TAGS

ruby
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring