How can I duplicate the Ruby core class name and still use that main class in my class?
I am creating a very limited time class in which I want to use the time class parsing method. So I get 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 of the Time Class without renaming my class?
+2
cfeduke
source
to share
1 answer
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
You can now refer to the old parsing method using Time.orig_parse
+6
khelll
source
to share