0

Is there a similar syntax to Java's .hasNext() method in Ruby? I've been trying to get inputs in one line and then making it as integers and getting the absolute value.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303

2 Answers2

1

It sounds like you want to see if there are more elements left in an iteration. Ruby's equivalent to that is peek:

From the docs:

Returns the next object in the enumerator, but doesn’t move the internal position forward. If the position is already at the end, StopIteration is raised.

But, in Ruby we usually rely on each or map to walk an iterable collection. There's no "figuring out" whether there's another element remaining, because Ruby does that for us.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
-2

ansh0l is right, gets is the most likely equivalent to hasNext() assuming that you're reading from the keyboard or any other I/O stream.

AndyV
  • 3,696
  • 1
  • 19
  • 17
  • `gets` is not the same thing as `hasNext()`. At all. – Mark Thomas Nov 18 '13 at 01:38
  • @MarkThomas: The examples that I found for hasNext dealt with the input stream in particular. Since this question also dealt with receiving input from the command line there seemed to be greater correspondence than you suggest. – AndyV Nov 18 '13 at 13:33