How can't i modify the to_s method in Integer Class?

J

Joel VanderWerf

Grab said:
class Integer

def to_s
'9'
end

end

puts 5.to_s

I'm still getting 5.

#to_s is defined in Fixnum, a subclass of Integer. Try this:

class Fixnum
def to_s
'9'
end

end

puts 5.to_s
 
D

David Vallner

Grab said:
class Integer

def to_s
'9'
end

end

puts 5.to_s

I'm still getting 5.

There's also the gotcha of redefining core methods - other core methods
just might call directly their implementations in C without going
through the interpreter.

Try not to rely on core method redefining unless you know you'll always
be calling the redefined methods directly.

David Vallner
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top