I was trying to find out how to create Ranges from custom objects in Ruby. I am very new to Ruby and I found the documentation on Range not to be helpful.
The error I was receiving when trying to create a Range from my objects was simply “Bad Value For A Range”.
I eventually figured out after far to long, that in order to create a range from my object, I must define the “succ” and “<=>” functions.
My question is this. Is there a good resource that would have told me that I needed to define the above 2 functions in order to use my object in a range? I’d like to avoid problems like this in the future.
Sorry for the unconventional question. Thank you for your time.
The Pickaxe Book (AKA “Programming Ruby”) has this to say about Range:
Emphasis mine. You have to be careful though, the Pickaxe that you’ll find online is rather old and sometimes it doesn’t agree with the current state of Ruby. There is an updated version for Ruby 1.9 but I don’t think that one is freely available online so you’d have to buy a copy.
I usually end up digging through the Ruby source to figure out a lot of these things. That applies doubly so to Rails.