Ruby How to create ranges from custom Docs?

I tried to figure out how to create ranges from custom objects in Ruby. I am very new to Ruby, and I found the Range documentation not useful.

The error I received while trying to create a range from my objects was simply "Bad value for the range."

In the end, I realized that in order to create a range from my object, I had to define the functions "succ" and "<=>".

My question is that. Is there a good resource that would tell me that I need to define the above 2 functions in order to use my object in a range? I would like to avoid such problems in the future.

Sorry for the unconventional question. Thank you for your time.

+4
source share
1 answer

The Pickaxe Book (AKA "Programming Ruby") has this to say about the range :

So far, we have shown ranges of numbers and strings. However, as you would expect from an object-oriented language, Ruby can create ranges based on the objects you define. The only limitations are that objects must respond to succ , returning the next object in the sequence, and objects must be matched using <=> , a common comparison operator.

The emphasis is mine. You have to be careful, although Pickaxe, which you find on the Internet, is quite old, and sometimes it does not agree with the current state of Ruby. There is an updated version for Ruby 1.9 , but I don’t think it is available on the Internet, so you have to buy a copy.

I usually end up digging through the Ruby source to figure out a lot of these things. This is doubly applicable to Rails.

+3
source

Source: https://habr.com/ru/post/1388241/


All Articles