Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
number
number.next
communitycreator

What is number.next in Ruby?

okwudili onyejiaku

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.

In Ruby, the .next function returns the next or immediate successor of a number. This method returns the next integer that succeeds an integer value. In other words, the function returns number+1number + 1.

Note: when used with a float, an error is thrown.

Syntax

number.next

Parameters

This function accepts no parameters. The only thing it needs is the number which calls it.

Return value

This function returns the next integer following number.

Code

In the code below, we print the next successive integers of some numbers using the .next method.

# create integer numbers
num1 = 1
num2 = 20
num3 = 301
num4 = 12_000 # same as 12000
num5 = 9*3/2
puts "next of #{num1} is #{num1.next}"
puts "next of #{num2} is #{num2.next}"
puts "next of #{num3} is #{num3.next}"
puts "next of #{num4} is #{num4.next}"
puts "next of #{num5} is #{num5.next}"

Calling the .next function on a float number such as 3.45, 5.6, 0.1 throws an error.

puts "next integer of 3.5 is #{3.5.next}"

RELATED TAGS

ruby
number
number.next
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.

Keep Exploring