New variable types and assorted lunacy

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 ;)
 
G

gabriele renzi

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.

<snipeed code>

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 :)
 
G

gabriele renzi

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

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

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

T.Oakley


Thank You, I've been enjoying my stay so far :)
sure and you can even override the default behaviour for existing
ones.

<snipeed code>

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.
 
P

Phil Tomson

Thank You, I've been enjoying my stay so far :)


Ahh, well, I guess that's something I'll have to live with. Or hack my
way around it.


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
 
T

T.Oakley

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

You think overriding assignment operators is evil? Oh dear, I suppose you won't like what I did with the bitwise operators I overrode. I... well... I made them unary.

Oh, and stay clear of INTERCAL :)
(http://www.muppetlabs.com/~breadbox/intercal-man/)



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

-Mark

And you're right on that one. It's not quite what I'm looking for, though, I think.


Anyhow, my question is, how can I create a new variable that's an "extension" of an existing one (see my example code snippet in the original post) so that it can, among other things, check which value it has been assigned, and if needed, change its own value.

Please, don't think I'm whining for complete code, just a whack (or even a light tap) with a clue stick. Strangely enough, I'm much more at home with esoteric oddities like INTERCAL, Brainf*ck and Perl (ha!).

Hey, my idea of a solid basis is using unary bitwise operators and calculated COME FROM statements ;)


Thanks,

Thomas
 
F

Florian Gross

T.Oakley said:
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.

Hope this helps.

Regards,
Florian Gross
 
T

T.Oakley

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)


Well, this is pretty much what I wrote, but the thing is I want to do away with the variable.assign(number) style and do it directly with variable = number,
and still be able to check what was assigned to the number and do various things with it.

Something like the pseudocode (?)


class Wrapped
def initialize(blablabla...
end

def self=(value)
raise Hell, "Run for your lives!" if (value > self.range.last || value < self.range.first)
self = value
end
end
 
D

Daniel.

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/
 
K

Kristof Bastiaensen

il Wed, 07 Apr 2004 12:40:34 +0300, "T.Oakley" <[email protected]> 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 :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top