Reputation: 221
I am trying to implement Integer#times, but don't know how to know the value of an Integer object. So I am doing it like this:
class Integer
def times
n = (self.next) -1
x = 1
while x <= n
yield(x)
x += 1
end
end
end
I am using (self.next) -1 to get the value of the object:) I know it is not the right way of doing it. How to get the value of the Integer object?
Upvotes: 0
Views: 287
Reputation: 2623
you can just use self
as mentioned by messanjah.
Note: if you try object.methods
in ruby console for an int like 5.methods
, you can find all the methods the object supports like to_i
and to_int
which also returns the value of the int object
so, you can try self.to_int
Upvotes: 1