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

class Integer

  def to_s
    '9'
  end

end

puts 5.to_s

I'm still getting 5.

···

--
Posted via http://www.ruby-forum.com/.

You shoud use Fixnum class instead of Integer.

···

On 8/22/06, Grab Mail <grabmail@gmail.com> wrote:

class Integer

  def to_s
    '9'
  end

end

puts 5.to_s

I'm still getting 5.

--
Posted via http://www.ruby-forum.com/\.

--
Kent
---

Grab Mail wrote:

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

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Grab Mail wrote:

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