New variable types and assorted lunacy

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!

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 :slight_smile:

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 :slight_smile:

···

il Wed, 07 Apr 2004 12:40:34 +0300, “T.Oakley” root@127.0.0.1 ha scritto::

gabriele renzi surrender_it@remove.yahoo.it writes:

You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors :slight_smile:
it would be cool anyway to be able to override assignment :slight_smile:

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. :slight_smile:

Anyway, you can override =, which is good enough for arrayish things.

-Mark

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 :slight_smile:
I’m taking that basically do what I’ve shown here (in addition they have
+,- modulo N operators defined for the class).

Phil

···

In article opr53haaupxses1n@news.elisa.net, T.Oakley root@127.0.0.1 wrote:

On Wed, 07 Apr 2004 15:34:38 GMT, gabriele renzi >surrender_it@remove.yahoo.it wrote:

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 :slight_smile:

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 :slight_smile:

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? :slight_smile: I’m just trying to see what I can and can’t do.

T.Oakley wrote:

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? :slight_smile: 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.

Hope this helps.

Regards,
Florian Gross

Hello,

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 :slight_smile:

PS
it would be cool anyway to be able to override assignment :slight_smile:

hey, that’s ooold thinking man! get in the third millennium! You think
in fortran man!
Ok, I know that this is plain evil :slight_smile:

But, there could be funny thing done with this, say …dunno… static
type checking or enforced variable declaration a-la VB :sunglasses:

···

il Wed, 07 Apr 2004 15:50:04 GMT, Mark J. Reed mreed@thereeds.org ha scritto::

gabriele renzi surrender_it@remove.yahoo.it writes:

You can’t ovverride = sorry. It’s not a message so we are stuck with
stupid accessors :slight_smile:
it would be cool anyway to be able to override assignment :slight_smile:

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. :slight_smile:

Strangely enough, I’m much more at home with esoteric oddities like INTERCAL,
Brainf*ck and Perl (ha!).

You should post your brainfuck programs to the archive at
http://esoteric.sange.fi/brainfuck/
(though the maintainer may take a while to update it).

Good luck.
-Daniel Cristofani.

++[<++++++++[<[<++>-]>>[>>]+>>+[-[->>+<<<[<[<<]<+>]>[>[>>]]]<[>>[-]]>[>[-
<<]+<[<+<]]+<<]<[>+<-]>>-]<.[-]>>]http://www.hevanet.com/cristofd/brainfuck/