gpt4 book ai didi

ruby - 为什么我可以使用像 `puts` 这样的内核单例方法?

转载 作者:数据小太阳 更新时间:2023-10-29 07:04:58 25 4
gpt4 key购买 nike

在 Ruby 中,方法 putsKernel 的单例方法模块。

通常,当一个模块是 include d 或 extend由另一个模块编辑,该模块(但不是它的单例类)被添加到继承树中。这有效地使模块的实例方法可用于模块或其单例类(分别用于 includeextend )......但混合模块的单例方法仍然无法访问,因为单例类从未将模块添加到继承树中。

那么为什么我可以使用 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

然而,出于某种原因,它们显示为每个对象的私有(private)方法。

Object.puts "asdf"

# NoMethodError (private method `puts' called for Object:Class)

如果 #<Class:Kernel>,方法查找是如何找到这些方法的?没有出现在祖先链中?

有关的:
  • Ruby method lookup path for an object
  • Class, Module, their eigenclasses, and method lookup
  • 注意:这与我要问的不同,因为这是类继承,所以 #<Class:Class>继承自 #<Class:Module>
  • Why a module's singleton method is not visible in downstream eigenclasses where it gets mixed?
  • 最佳答案

    你找错地方了。

    方法,如 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!'

    因为读者会被误导,认为 privateself 做了一些事情,而实际上,它完全忽略了它。 (这尤其适用于打印系列方法,因为也存在 ojit_a 和 friend ,它们确实关心他们的接收者。)

    因此,为了防止您使用接收器误导性地调用这些方法,它们被制作为 Kernel ,这意味着它们只能在没有显式接收器的情况下被调用。 (显然,它们仍然会在 private 上被调用,但至少在视觉上不会那么明显。)

    从技术上讲,这些根本不是真正的方法,它们的行为更像过程,但是 Ruby 没有过程,因此这是“伪造”它们的最佳方式。

    它们也被定义为单例方法的原因是,您仍然可以在 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 并且 ObjectKernel 的一个实例,它是 private 的子类,因此 Module#module_function 是它自身的一个间接实例。然而,这些方法是 Math,因此你会得到一个

    NoMethodError: private method `puts' called for Kernel:Module

    反而。因此,它们需要单独复制。实际上有一个辅助方法可以做到这一点: ojit_a 。 (这也用于 ojit_a ,您可以在其中调用例如 Math.sqrt(4)include Math; sqrt(4) 。在这种情况下,您可以选择 include ing Math 与否,而 Kernel 始终是 include 中的 pre- Object d。)

    因此,总而言之:这些方法被复制为 privateKernel 实例方法以及 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com