Enumerating Coordinates In Ruby
Here we are using Point object for Enumerating coordinates in ruby. We might be able to cycle through the two elements of a Point object as we might with a real array if it can act like an array with two components. The each iterator for our Point class is defined here. Our iterator doesn't need to loop because a Point always has precisely two elements; it can only make two calls to yield:
# The X coordinate is passed by this iterator to the connected block,
# followed by the Y coordinate and a return. It enables us to enumerate
# a point as though it were a two-element array. The Enumerable module
# requires this each method.
def each
yield @x
yield @y
end

You can also try this code with Online Ruby Compiler
Run Code
After defining this iterator, we can create the following code:
p = Point.new(2,4)
p.each {|x| print x }

You can also try this code with Online Ruby Compiler
Run Code
Output
"24"
The ability to mix in the methods of the Enumerable module, which are all specified in terms of each, is made possible by defining each iterator. By adding a single line, our class gets almost 20 additional iterators:
include Enumerable

You can also try this code with Online Ruby Compiler
Run Code
This will allow us to create intriguing code like this:
# Is the point P at the origin?
p.all? {|x| x == 0 } # The block is true for all elements if it is true.

You can also try this code with Online Ruby Compiler
Run Code
Check out this problem - Maximum Product Subarray
Frequently Asked Questions
What does Enumerating Coordinates in Ruby mean?
The concept of Enumerating coordinates in ruby is based on the premise that If a Point object can behave like an array with two elements, then perhaps we ought to be able to iterate through those elements as we can with a true array.
Can we redefine [] ( square bracket ) in Ruby?
We can redefine [] ( square bracket ), to implement Array and Hash Access with [ ].Ruby supports array and hash access with [] and permits any class to define a [] method and use these brackets on its own (Square Bracket).
In Ruby, what do objects mean?
A Ruby object can be anything. All objects have an identity, the ability to maintain a state, and the capacity to exhibit behavior in response to signals. Typically, method calls are used to send these messages.
In Ruby, is class an object?
A Ruby class is an instance of the class Class, which includes all object-related objects as well as a list of methods and a reference to a superclass (which is itself another class). In Ruby, each method call specifies a recipient (which is by default self, the current object).
Is Ruby a computer language used for programming?
It's a very flexible programming language. Ruby programmers have the ability to alter the way the language functions. Instead of being compiled like C or C++, it is an interpreted language like Python.
Conclusion
In this article, we have discussed everything you need to know about Array and Hash Access with [ ] and Enumerating Coordinates in Ruby. Here are more articles to the rescue:
Recommended problems -
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!
