Ruby Integer next function with example

The next function in Ruby returns the immediate successor of the number, i.e., it returns number + 1. If a float value is used, it throws an error message.

Syntax: number.next

Parameter: The function takes the integer whose next is to be returned.



Return Value: The function returns the immediate successor of the number, i.e., it returns number + 1

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program of next function
  
# Initializing the numbers 
num1 = 100
num2 = 17
num3 = -90
num4 = -29
      
# Printing the modulo value
puts num1.next
puts num2.next
puts num3.next 
puts num4.next 

chevron_right


Output:

101
18
-89
-28

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program of next function
  
# Initializing the numbers 
num1 = 19
num2 = -17
num3 = -18
num4 = 16
     
# Printing the modulo value
puts num1.next
puts num2.next
puts num3.next 
puts num4.next 

chevron_right


Output:

20
-16
-17
17


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.