Ruby 在不初始化对象的情况下存储变量?红宝石宝石&x27;邮寄';
在使用时,我对如何在不初始化对象的情况下存储变量感到困惑?例如:Ruby 在不初始化对象的情况下存储变量?红宝石宝石&x27;邮寄';,ruby,class,object,initializer,Ruby,Class,Object,Initializer,在使用时,我对如何在不初始化对象的情况下存储变量感到困惑?例如: Mail.defaults do retriever_method :pop3, :address => "pop.gmail.com", :port => 995, :user_name => '<username>',
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username>',
:password => '<password>',
:enable_ssl => true
end
Mail.do
检索器_方法:pop3,:address=>“pop.gmail.com”,
:端口=>995,
:user_name=>“”,
:密码=>'',
:enable_ssl=>true
结束
之后,您可以调用方法,例如Mail.first
,并让它以配置的默认值返回邮箱中的第一封邮件
我意识到Ruby中的所有东西都是一个对象,甚至是一个类,所以当调用
require'mail
时,包含class-mail
的对象是否真的被创建并可供程序使用?这里到底发生了什么?将mail.rb
的内容加载到包含require'mail'
语句的文件中
查看gem后,mail.rb
包含mail
模块,该模块又包含许多其他require语句
mail.rb
module Mail
## skipped for brevity
# Finally... require all the Mail.methods
require 'mail/mail'
end
module Mail
## skipped for brevity
# Receive the first email(s) from the default retriever
# See Mail::Retriever for a complete documentation.
def self.first(*args, &block)
retriever_method.first(*args, &block)
end
end
mail/mail.rb
module Mail
## skipped for brevity
# Finally... require all the Mail.methods
require 'mail/mail'
end
module Mail
## skipped for brevity
# Receive the first email(s) from the default retriever
# See Mail::Retriever for a complete documentation.
def self.first(*args, &block)
retriever_method.first(*args, &block)
end
end
这样,这些方法就可供您的程序使用。包含类Mail的对象是否真的被创建并可供程序使用?这里到底发生了什么?“因此mail.rb将包含mail类并使其可用于当前文件。”,当前文件是什么<代码>邮件类,你是说
邮件
?加载
和要求
之间的差异与此问题无关。。。你能在一个正在运行的程序的上下文中表达你的答案吗?编辑过的答案,如果不是你想要的,我认为我没有正确理解这个问题。