未初始化的常数LatexCurriculumVitae

时间:2015-10-20 04:24:59

标签: ruby

我有一个主要模块LatexCurriculumVitae:

module LatexCurriculumVitae
   LatexCurriculumVitae::Email.create_email_smtp('Heribert Hunz', 'saigkill@googlemail.com', 'Feger', 'male', 'no', 'yes')
end

也是

module LatexCurriculumVitae::Email
   def self.create_email_smtp(contact, emailaddress, jobtitle, contact_sex, proactive, letter)
   [...]
end

如果我正在运行我正在运行的主模块:

未初始化的常量LatexCurriculumVitae(NameError)

但是wat发生了吗?

2 个答案:

答案 0 :(得分:1)

使用嵌套的模块在文件中定义方法:

module LatexCurriculumVitae
  module Email
    def self.create_email_smtp(contact, emailaddress, jobtitle, contact_sex, proactive, letter)
    [...]
    end
  end
end

使用双冒号定义它将失败,因为先前未定义外部模块。

当您想要调用该方法时,您不需要将其包装在模块块中:

LatexCurriculumVitae::Email.create_email_smtp('Heribert Hunz', 'saigkill@googlemail.com', 'Feger', 'male', 'no', 'yes')

答案 1 :(得分:0)

FYI

module B
  def self.print
    puts 'its outer B print'
  end
end

 module A
   def self.get_print_of_inner_B
     B.print
   end

   def self.get_print_of_outer_B
     ::B.print
   end
 end


 module A::B
  def self.print
    puts 'its inner B of A  print'
  end
 end

当你在IRB中粘贴上面的代码时,你得到了

2.2.1 :01 > A.get_print_of_inner_B
its inner B of A  print
 => nil 
2.2.1 :02 > A.get_print_of_outer_B
its outer B print
 => nil 

<强>结论:
::B.print此处::用于访问全局/外部范围中定义的constants,而非当前module/class