R
Rasputin
From the Pickaxe book:
class VU
include Comparable
attr :volume
def initialize(volume) # 0..9
@volume = volume
end
def inspect
'#' * @volume
end
# Support for ranges
def <=>(other)
self.volume <=> other.volume
end
def succ
raise(IndexError, "Volume too big") if @volume >= 9
VU.new(@volume.succ)
end
end
irb(main):001:0> require 'vu'
=> true
irb(main):002:0> r = VU.new(0)..VU.new(9)
=> ..#########
irb(main):003:0> r.to_a
IndexError: Volume too big
from ./vu.rb:20:in `succ'
from (irb):3:in `each'
from (irb):3:in `to_a'
from (irb):3
Shouldn't the 0 to 9 range be possible?
Apparently 0 to 8 etc. works.
The succ method should only be raising the exception when @volume is 9
but to me it looks like it's raising the error at 8.
I'm not sure if it's a bug or something I'm doing wrong.
class VU
include Comparable
attr :volume
def initialize(volume) # 0..9
@volume = volume
end
def inspect
'#' * @volume
end
# Support for ranges
def <=>(other)
self.volume <=> other.volume
end
def succ
raise(IndexError, "Volume too big") if @volume >= 9
VU.new(@volume.succ)
end
end
irb(main):001:0> require 'vu'
=> true
irb(main):002:0> r = VU.new(0)..VU.new(9)
=> ..#########
irb(main):003:0> r.to_a
IndexError: Volume too big
from ./vu.rb:20:in `succ'
from (irb):3:in `each'
from (irb):3:in `to_a'
from (irb):3
Shouldn't the 0 to 9 range be possible?
Apparently 0 to 8 etc. works.
The succ method should only be raising the exception when @volume is 9
but to me it looks like it's raising the error at 8.
I'm not sure if it's a bug or something I'm doing wrong.