- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在 Ruby 中,方法 puts
是 Kernel
的单例方法模块。
通常,当一个模块是 include
d 或 extend
由另一个模块编辑,该模块(但不是它的单例类)被添加到继承树中。这有效地使模块的实例方法可用于模块或其单例类(分别用于 include
和 extend
)......但混合模块的单例方法仍然无法访问,因为单例类从未将模块添加到继承树中。
那么为什么我可以使用 puts
(和其他内核单例方法)?
Kernel.singleton_methods(false)
# => [:caller_locations, :local_variables, :require, :require_relative, :autoload, :sprintf, :format, :Integer, :Float, :String, :Array, :Hash, :test, :warn, :autoload?, :fork, :binding, :exit, :raise, :fail, :global_variables, :__method__, :__callee__, :__dir__, :URI, :eval, :iterator?, :block_given?, :catch, :throw, :loop, :gets, :sleep, :proc, :lambda, :trace_var, :untrace_var, :at_exit, :load, :Rational, :select, :Complex, :syscall, :open, :printf, :print, :putc, :puts, :readline, :readlines, :`, :p, :system, :spawn, :exec, :exit!, :abort, :set_trace_func, :rand, :srand, :trap, :caller]
puts
似乎不是
Kernel
上的实例方法:
Kernel.instance_methods.grep(/puts/)
# []
Object
不包括
Kernel
Object.included_modules
# [Kernel]
Kernel
的单例类 (
#<Class:Kernel>
) 不会出现在任何对象的祖先中。
is_a?
似乎同意这一点:
Object.is_a? Class.singleton_class # false
Object.is_a? Kernel.singleton_class # false
Object.singleton_class.is_a? Class.singleton_class # true
Object.singleton_class.is_a? Kernel.singleton_class # false
Object.puts "asdf"
# NoMethodError (private method `puts' called for Object:Class)
#<Class:Kernel>
,方法查找是如何找到这些方法的?没有出现在祖先链中?
#<Class:Class>
继承自 #<Class:Module>
最佳答案
你找错地方了。
方法,如 Kernel#Array
, Kernel#Complex
, Kernel#Float
, Kernel#Hash
, Kernel#Integer
, Kernel#Rational
, Kernel#String
, Kernel#__callee__
, Kernel#__dir__
, Kernel#__method__
, Kernel#`
, Kernel#abort
, Kernel#at_exit
, Kernel#autoload
, Kernel#autoload?
, Kernel#binding
, Kernel#block_given?
, Kernel#callcc
, Kernel#caller
, Kernel#caller_locations
, Kernel#catch
, Kernel#eval
, Kernel#exec
, Kernel#exit
, Kernel#exit!
, Kernel#fail
, Kernel#fork
, Kernel#format
, Kernel#gets
, Kernel#global_variables
, Kernel#initialize_clone
, Kernel#initialize_copy
, Kernel#initialize_dup
, Kernel#iterator?
, Kernel#lambda
, Kernel#load
, Kernel#local_variables
, Kernel#loop
, Kernel#open
, Kernel#p
, Kernel#pp
, Kernel#print
, Kernel#printf
, Kernel#proc
, Kernel#putc
, Kernel#puts
, Kernel#raise
, Kernel#rand
, Kernel#readline
, Kernel#readlines
, Kernel#require
, Kernel#require_relative
, Kernel#select
, Kernel#set_trace_func
, Kernel#sleep
, Kernel#spawn
, Kernel#sprintf
, Kernel#srand
, Kernel#syscall
, Kernel#system
, Kernel#test
, Kernel#throw
, Kernel#trace_var
, Kernel#trap
, Kernel#untrace_var
, Kernel#warn
, self
, puts
, foo
, ojit_a他们不调用私有(private)方法,也不访问实例变量,实际上他们完全忽略了 IO#puts
是什么。
因此,如果你这样称呼它们会产生误导:
foo.puts 'Hello, World!'
private
对
self
做了一些事情,而实际上,它完全忽略了它。 (这尤其适用于打印系列方法,因为也存在 ojit_a 和 friend ,它们确实关心他们的接收者。)
Kernel
,这意味着它们只能在没有显式接收器的情况下被调用。 (显然,它们仍然会在
private
上被调用,但至少在视觉上不会那么明显。)
Kernel.puts
不在继承层次结构中的上下文中调用它们,例如像这样:
class Foo < BasicObject
def works
::Kernel.puts 'Hello, World!'
end
def doesnt
puts 'Hello, World!'
end
end
f = Foo.new
f.works
# Hello, World!
f.doesnt
# NoMethodError: undefined method `puts' for #<Foo:0x00007f97cf918ed0>
Object
。如果不是,那么无论如何您都可以简单地调用
Kernel
,因为
Kernel
包括
Module
并且
Object
是
Kernel
的一个实例,它是
private
的子类,因此
Module#module_function
是它自身的一个间接实例。然而,这些方法是
Math
,因此你会得到一个
NoMethodError: private method `puts' called for Kernel:Module
Math.sqrt(4)
或
include Math; sqrt(4)
。在这种情况下,您可以选择
include
ing
Math
与否,而
Kernel
始终是
include
中的 pre-
Object
d。)
private
的
Kernel
实例方法以及
public
单例方法(实际上只是
Kernel
的单例类的实例方法)。它们被定义为
private
实例方法的原因是它们不能被显式接收器调用,并且被迫看起来更像过程。它们被复制为
Kernel
的单例方法的原因是,在
Kernel
在继承层次结构中不可用的上下文中,只要显式接收器是
Kernel
,就可以使用显式接收器调用它们。
#ruby --disable-gems --disable-did_you_mean -e'puts Kernel.private_instance_methods(false).sort'
Array
Complex
Float
Hash
Integer
Rational
String
__callee__
__dir__
__method__
`
abort
at_exit
autoload
autoload?
binding
block_given?
caller
caller_locations
catch
eval
exec
exit
exit!
fail
fork
format
gets
global_variables
initialize_clone
initialize_copy
initialize_dup
iterator?
lambda
load
local_variables
loop
open
p
pp
print
printf
proc
putc
puts
raise
rand
readline
readlines
require
require_relative
respond_to_missing?
select
set_trace_func
sleep
spawn
sprintf
srand
syscall
system
test
throw
trace_var
trap
untrace_var
warn
关于ruby - 为什么我可以使用像 `puts` 这样的内核单例方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57872724/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!