New variable types and assorted lunacy

Discussion in 'Ruby' started by T.Oakley, Apr 7, 2004.

  1. T.Oakley

    T.Oakley Guest

    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 ;)
    T.Oakley, Apr 7, 2004
    #1
    1. Advertising

  2. 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!

    >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 :)
    gabriele renzi, Apr 7, 2004
    #2
    1. Advertising

  3. il Wed, 07 Apr 2004 15:50:04 GMT, Mark J. Reed <> ha
    scritto::

    >gabriele renzi <> writes:
    >>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. :)


    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:
    gabriele renzi, Apr 7, 2004
    #3
  4. T.Oakley

    T.Oakley Guest

    On Wed, 07 Apr 2004 15:34:38 GMT, gabriele renzi <> 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 :)

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


    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.
    T.Oakley, Apr 7, 2004
    #4
  5. T.Oakley

    Phil Tomson Guest

    In article <>, T.Oakley <root@127.0.0.1> wrote:
    >On Wed, 07 Apr 2004 15:34:38 GMT, gabriele renzi
    ><> 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 :)
    >
    >>> 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 :)

    >
    >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
    Phil Tomson, Apr 7, 2004
    #5
  6. T.Oakley

    T.Oakley Guest

    On Wed, 07 Apr 2004 15:50:04 GMT, Mark J. Reed <> wrote:

    > gabriele renzi <> writes:
    >> 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. :)


    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
    T.Oakley, Apr 7, 2004
    #6
  7. 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? :) 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
    Florian Gross, Apr 7, 2004
    #7
  8. T.Oakley

    T.Oakley Guest

    On 7 Apr 2004 16:56:49 GMT, Phil Tomson <> wrote:

    > 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
    T.Oakley, Apr 8, 2004
    #8
  9. T.Oakley

    Daniel. Guest

    > 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/
    Daniel., Apr 8, 2004
    #9
  10. 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 :)


    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
    Kristof Bastiaensen, Apr 9, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    617
    jeffc
    Oct 24, 2003
  2. John Perks and Sarah Mount

    Assorted JNI questions

    John Perks and Sarah Mount, Oct 2, 2005, in forum: Java
    Replies:
    5
    Views:
    1,261
    Roedy Green
    Oct 6, 2005
  3. Replies:
    24
    Views:
    607
    jeffc
    Oct 24, 2003
  4. Ramon F Herrera
    Replies:
    2
    Views:
    3,587
    Andrew Thompson
    Oct 3, 2007
  5. ccc31807

    to RG - Lisp lunacy and Perl psychosis

    ccc31807, Mar 10, 2010, in forum: Perl Misc
    Replies:
    62
    Views:
    567
    Kyle T. Jones
    Mar 26, 2010
Loading...

Share This Page