override []

Discussion in 'Ruby' started by ruby talk, Nov 24, 2004.

  1. ruby talk

    ruby talk Guest

    How can i over ride [] in a class?

    i have a binary class where it is stored in a string and i want to
    beable to do this
    a=Binary.new("00101001")
    p a[0] >> 1

    i can provide what i have so far if you would like.
    thank you for your time.
    Becker
    ruby talk, Nov 24, 2004
    #1
    1. Advertising

  2. On Nov 23, 2004, at 7:15 PM, ruby talk wrote:
    > How can i over ride [] in a class?


    class Foo
    def []( n )
    #...
    end
    end
    Gavin Kistner, Nov 24, 2004
    #2
    1. Advertising

  3. ruby talk

    ruby talk Guest

    def = (n)
    @f = n if n.type==String

    end

    C:/1134re.rb:20: syntax error
    def = (n)
    ^
    C:/1134re.rb:20: warning: don't put space before argument parentheses
    C:/1134re.rb:207: syntax error

    sorry this is my first class


    On Wed, 24 Nov 2004 11:56:48 +0900, Gavin Kistner <> wrote:
    > On Nov 23, 2004, at 7:15 PM, ruby talk wrote:
    > > How can i over ride [] in a class?

    >
    > class Foo
    > def []( n )
    > #...
    > end
    > end
    >
    >
    ruby talk, Nov 24, 2004
    #3
  4. "ruby talk" <> schrieb im Newsbeitrag
    news:...
    > def = (n)
    > @f = n if n.type==String
    >
    > end
    >
    > C:/1134re.rb:20: syntax error
    > def = (n)
    > ^
    > C:/1134re.rb:20: warning: don't put space before argument parentheses
    > C:/1134re.rb:207: syntax error
    >
    > sorry this is my first class


    You cannot override assignment. You can only override []= like in

    class Foo
    def []=(x,y)
    puts "called [#{x}]=#{y}"
    end
    end

    >> Foo.new[1]=2

    called [1]=2
    => 2

    robert

    >
    >
    > On Wed, 24 Nov 2004 11:56:48 +0900, Gavin Kistner <>

    wrote:
    > > On Nov 23, 2004, at 7:15 PM, ruby talk wrote:
    > > > How can i over ride [] in a class?

    > >
    > > class Foo
    > > def []( n )
    > > #...
    > > end
    > > end
    > >
    > >

    >
    >
    Robert Klemme, Nov 24, 2004
    #4
    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. Guest
    Replies:
    2
    Views:
    551
    Natty Gur
    Jul 13, 2003
  2. Guest
    Replies:
    2
    Views:
    3,101
    Guest
    Jul 16, 2003
  3. Jon
    Replies:
    4
    Views:
    5,645
    Jonathan Williams
    Aug 7, 2003
  4. Gary Chamberlain

    Override the Encryption Key for Forms Auth

    Gary Chamberlain, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    324
    Gary Chamberlain
    Aug 27, 2003
  5. SStory
    Replies:
    3
    Views:
    2,420
    SStory
    Oct 30, 2003
Loading...

Share This Page