T
T.Oakley
Hello!
I'm more or less a newcomer to Ruby ("found" it 4 days ago). Having mostly dealt with functional/procedural
languages in the past, I'm having a little trouble adjusting to the OO-paradigm, but nevertheless, Ruby's got me hooked.
Anyhow, here's the problem:
Can create a class that extends the existing variable types?
Here's some pseudocode to clarify:
class Wrapped < Numeric
attr_reader :range
def range=(range)
raise "Wrapped given #{range.class} not Range." if range.class != Range
@range = range
end
def initialize(range)
self.range=(range)
self
end
#this is a feature I'd like. Redefining the assignment operator just sounds so perversely gratifying
def =(value)
if value === self.range
self = value
elsif value < self.range.first
#Wraps around to the "right" end of the range
value = self.range.last
else
#Wraps around to the "left" end of the range
value = self.range.first
end
end
def +(val)
frobobnitz.baz(qoox)
bar(floop).bloop!
end
def -(val)
and so on
end
Now, after having a class like this, it'd be possible to write something like the following:
num = Wrapped.new(4..17)
num = 15
num += 4
num would now be 5
num -= 2
num would now be 17
As it is, this snippet wouldn't work (and I know it), but I'm wondering if something like it would be possible.
The reason why I'd like to implement something like Wrapped is that I'd just like to see if it can be done, that's all.
So, help me out, o Gurus. How do I go about doing something like this without having to resort to (*shudder*) C?
Hey, after all, I started using Ruby to get _away_ from all the other muck
I'm more or less a newcomer to Ruby ("found" it 4 days ago). Having mostly dealt with functional/procedural
languages in the past, I'm having a little trouble adjusting to the OO-paradigm, but nevertheless, Ruby's got me hooked.
Anyhow, here's the problem:
Can create a class that extends the existing variable types?
Here's some pseudocode to clarify:
class Wrapped < Numeric
attr_reader :range
def range=(range)
raise "Wrapped given #{range.class} not Range." if range.class != Range
@range = range
end
def initialize(range)
self.range=(range)
self
end
#this is a feature I'd like. Redefining the assignment operator just sounds so perversely gratifying
def =(value)
if value === self.range
self = value
elsif value < self.range.first
#Wraps around to the "right" end of the range
value = self.range.last
else
#Wraps around to the "left" end of the range
value = self.range.first
end
end
def +(val)
frobobnitz.baz(qoox)
bar(floop).bloop!
end
def -(val)
and so on
end
Now, after having a class like this, it'd be possible to write something like the following:
num = Wrapped.new(4..17)
num = 15
num += 4
num would now be 5
num -= 2
num would now be 17
As it is, this snippet wouldn't work (and I know it), but I'm wondering if something like it would be possible.
The reason why I'd like to implement something like Wrapped is that I'd just like to see if it can be done, that's all.
So, help me out, o Gurus. How do I go about doing something like this without having to resort to (*shudder*) C?
Hey, after all, I started using Ruby to get _away_ from all the other muck