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.
welcome!
Anyhow, here’s the problem:
Can create a class that extends the existing variable types?
sure and you can even override the default behaviour for existing
ones.
You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors
it seem to me you just want a circular array or range, with a cursor.
Am I wrong?
PS
it would be cool anyway to be able to override assignment
···
il Wed, 07 Apr 2004 12:40:34 +0300, “T.Oakley” root@127.0.0.1 ha scritto::
You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors
it would be cool anyway to be able to override assignment
No, no, no, no, no! Eeeeeevil! You could never be sure of anything if
x = y might do something other than make x an alias for the object pointed to
by y. Even the most flexible system needs some solid basis.
Anyway, you can override =, which is good enough for arrayish things.
You want some sort of n-ary (ternary in this case) type.
Why not do something like:
class OutOfRangeException < Exception
end
class Nary
attr_accessor :value
def initialize(range,value=range.first) @range = range #should be a range
check_value(value) @value = value
end
def assign(value)
check_value(value) @value = value
end
def to_i @value.to_i
end
def to_s @value.to_s
end
private
def check_value(value)
if value < @range.first || value > @range.last
raise OutOfRangeException, “value must be between #{@range.first}
and #{@range.last}”
end
end
end
ternary = Nary.new(0…2)
puts “value is: #{ternary}” #=> 0
ternary.assign(2)
puts “value is: #{ternary}” #=> 2
ternary.assign(22) #=> Raises OutOfRangeException “value must be between
0 and 2”
binary = Nary.new(0…1)
I’m developing some Ternary Logic classes for a Quantum Computing class
(as in course - the word ‘class’ has two meanings in this sentance
I’m taking that basically do what I’ve shown here (in addition they have
+,- modulo N operators defined for the class).
il Wed, 07 Apr 2004 12:40:34 +0300, “T.Oakley” root@127.0.0.1 ha
scritto::
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.
welcome!
Thank You, I’ve been enjoying my stay so far
Anyhow, here’s the problem:
Can create a class that extends the existing variable types?
sure and you can even override the default behaviour for existing
ones.
You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors
Ahh, well, I guess that’s something I’ll have to live with. Or hack my
way around it.
it seem to me you just want a circular array or range, with a cursor.
Am I wrong?
Not an array, no, just an Integer. So that if I type, say
aNumber = Wrapped.new(1…3)
the variable aNumber would only get values that === 1…3
So, for example if aNumber was 1 and I added 3 to it, it would == 1, and
if I added 4, it would == 2 and so on. It just wraps around. Why do I
want this? Why not? I’m just trying to see what I can and can’t do.
it seem to me you just want a circular array or range, with a cursor.
Am I wrong?
Not an array, no, just an Integer. So that if I type, say
aNumber = Wrapped.new(1…3)
the variable aNumber would only get values that === 1…3
So, for example if aNumber was 1 and I added 3 to it, it would == 1, and
if I added 4, it would == 2 and so on. It just wraps around. Why do I
want this? Why not? I’m just trying to see what I can and can’t do.
You can overload + to return bound values.
Or you can replace aNumber with self.aNumber= and do a def
self.aNumber=(to); …; end.
The reason why assignment is not a method, is because it
doesn’t operate on an object. Instead it operates on the
variable that contains the object. As far as I know, variables
are not objects.
Anyway it wouldn’t be useful to override assignment, because you
can’t change an object itself (self). Once created it will remain
the same object (like in procedural languages like scheme).
But you can change the contents of an object. If you like to
have a value that can contain something, use an array,
Cheers,
Kristof
···
On Wed, 07 Apr 2004 17:34:38 +0200, gabriele renzi wrote:
il Wed, 07 Apr 2004 12:40:34 +0300, “T.Oakley” root@127.0.0.1 ha
scritto::
You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors
PS
it would be cool anyway to be able to override assignment
You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors
it would be cool anyway to be able to override assignment
No, no, no, no, no! Eeeeeevil! You could never be sure of anything if
x = y might do something other than make x an alias for the object pointed to
by y. Even the most flexible system needs some solid basis.