tags:

views:

51

answers:

1

I am creating a very limited Time class in which I want to make use of the core Time class's parse method. So I end up with something like this...

class Time
    def parse(str)
         @time = # I want to use Time.parse here
    end
end

How can I break out of my newly defined Time class and access the core Time class without renaming my class?

+3  A: 
require 'time'
class Time
#Opening the singleton class as Time.parse is a singleton method
  class << self
    alias_method :orig_parse, :parse
    def parse(str)
      @time = orig_parse str
    end
  end
end

Now you can still reference the old parse method using Time.orig_parse

khelll
Hmm yeah that's essentially what I have, but RSpec is complaining that 'parse' is an undefined method for 'Time' when I try to run my spec.
cfeduke
Ignore my last, just refreshed, will try that.
cfeduke
That's because you need to require 'time'
khelll