override []

R

ruby talk

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
 
R

ruby talk

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


How can i over ride [] in a class?

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

Robert Klemme

ruby talk said:
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
called [1]=2
=> 2

robert
How can i over ride [] in a class?

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

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top