From: ptkwt@... (Phil Tomson) Date: 2004-04-08T02:24:18+09:00 Subject: Re: New variable types and assorted lunacy In article , T.Oakley wrote: >On Wed, 07 Apr 2004 15:34:38 GMT, gabriele renzi > wrote: > >> il Wed, 07 Apr 2004 12:40:34 +0300, "T.Oakley" 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. 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). Phil