本文介绍了我的类名称与Ruby的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的模块中有一个类叫做Date。但是当我想使用与ruby一起打包的Date类时,它使用我的Date类。
I have a class in my module that is called "Date". But when i want to utilize the Date class packaged with ruby, it uses my Date class instead.
module Mymod
class ClassA
class Date < Mymod::ClassA
require 'date'
def initialize
today = Date.today # get today's date from Ruby's Date class
puts "Today's date is #{today.to_s}"
end
end
end
end
Mymod::ClassA::Date.new
运行此输出的输出是
test.rb:7:in `initialize': undefined method `today' for Mymod::ClassA::Date:Class (NoMethodError)
有什么办法我可以引用ruby的Date类从我自己的类也叫Date?
Is there any way I can reference ruby's Date class from within my own class also called "Date"?
推荐答案
def initialize
today = ::Date.today # get today's date from Ruby's Date class
puts "Today's date is #{today.to_s}"
end
这篇关于我的类名称与Ruby的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!