[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

override []

RubyTalk@gmail.com

11/24/2004 2:16:00 AM

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


3 Answers

Gavin Kistner

11/24/2004 2:57:00 AM

0

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



RubyTalk@gmail.com

11/24/2004 8:15:00 AM

0

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 <gavin@refinery.com> 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

11/24/2004 9:23:00 AM

0


"ruby talk" <rubytalk@gmail.com> schrieb im Newsbeitrag
news:25094bf704112400141ba74831@mail.gmail.com...
> 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 <gavin@refinery.com>
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
> >
> >
>
>