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.
Asked
Active
Viewed 811 times
0

the Tin Man
- 158,662
- 42
- 215
- 303
-
4It’s easier to answer if you show us your code and sample input and expected/actual output. – Andrew Marshall Nov 17 '13 at 19:31
-
You mean the `gets` method? Like used [`here`](http://stackoverflow.com/a/6556370/1860929)? – Anshul Goyal Nov 17 '13 at 19:32
-
What do you mean by syntax. Methods in ruby look like Java's `foo.bar(baz, quux)` – daniel gratzer Nov 17 '13 at 19:33
-
In Ruby, you don't need the equivalent of `Iterator`'s `hasNext()`. – Mark Thomas Nov 17 '13 at 20:14
2 Answers
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
-
-
@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